Skip Navigation

[Resolved] Cred Form to select author

This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 7 replies, has 2 voices.

Last updated by Dat Hoang 7 years, 11 months ago.

Assigned support staff: Dat Hoang.

Author
Posts
#272360

I am trying to: add a cred form field to select author. if one user in creating the post can it be created as another author?

For example:

[cred_field field="_wpcf_belongs_author_id" value="" order="title" ordering="desc" required="true" select_text="--- Select Author ---" validate_text="Author must be selected"]

is this possible?

#272469

Please note that "author" here is a custom post type. And I see your shortcode , there is no problem at all.

However, I think you may misunderstand between the custom post type "author" and the users of WordPress site.

If possible, tell us more about your case and take some screenshots.

#272471

Thank you for the reply...

I want a user to have the ability to assign a post to another user.... making that selected user the author of the post.

#272508

You will need to use a CRED generic field wich will be filled for a View with authors. After you will need to use some CRED hook – https://toolset.com/documentation/user-guides/cred-api/ – in this case cred_save_data.

1) Create a View (named "View Name") to list all authors, the Layout HTML section should be:

[wpv-layout-start]
    [wpv-items-found]   
        <wpv-loop>[wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="nickname"]"}[wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="nickname"]"}</wpv-loop>
    [/wpv-items-found]
[wpv-layout-end]

2) Now you need to insert a CRED generic field which will get this View value. See the code below:

[cred_generic_field field="my_author_select" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view  name="View Name"] ]
}
[/cred_generic_field]

3) Insert the following code into the theme file functions.php. Make sure you set the correct ID of your form. My example is 12.

add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
	// if a specific form
	if ( $form_data[ 'id' ] == 12 ) { //Make sure you set the correct ID of your form
		$my_post = array(
			'ID'           => $post_id,
			'post_author' => $_POST['my_author_select']
		);
		  
		// Update the post author ID into the database
		wp_update_post( $my_post );
	   
	}
}

If you follow the steps above it should works. The process includes:
+ A View to query all authors
+ A CRED generic field to get this View result
+ A hook to store the selected author in the database.

#273304

I did as instructed... this is the error I get:

Fatal error: Cannot redeclare my_save_data_action() (previously declared

Not sure what to do now?

#273389

That means you are using the name my_save_data_action() for one of your existing functions. You need to change the name a bit. I've just added "_8433" (a random number) to make sure there is no duplicate.

add_action( 'cred_save_data', 'my_save_data_action_8433', 10, 2 );
function my_save_data_action_8433($post_id, $form_data) {
    // if a specific form
    if ( $form_data[ 'id' ] == 12 ) { //Make sure you set the correct ID of your form
        $my_post = array(
            'ID'           => $post_id,
            'post_author' => $_POST['my_author_select']
        );
           
        // Update the post author ID into the database
        wp_update_post( $my_post );
        
    }
}
#273590

Thank you the error is no longer an issue, however the generic form is not showing up? Not sure why is there any settings in the View I need to ensure is in place?

#273729

You need to replace the shortcode generic cred field for the cred_field shorcode like this:

		[cred_field field="post_content" post="employee" value="" urlparam=""]

Does that help?