I am trying to set post slug and title from a Front End "Publish Post" Form using method I found in other support post.
add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
if ($form_data['id']==1133) {
/*$author=get_post_meta($post_id, 'wpv-post-author');
$post_title = $author['meta']['first_name'] + "-" + $author['meta']['last_name'];*/
$current_user = wp_get_curent_user();
$post_title = $current_user['first_name'] + " " + $current_user['last_name'];
$slug = sanitize_title_with_dashes($post_title);
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title, 'post_name' => $slug));
}
}
Lets say the user submitting the form has the name "Jane Doe". I would expect:
hidden link
Post Title would be "Jane Doe"
I am getting an Ajax 500 error. I am using the toolset 'Custom Code' function. Is it because the post id doesn't exist yet when publishing is happening?
Is it because the post id doesn't exist yet when publishing is happening?
Hi there, actually the post ID has already been created at the point the hook is executed. You have access to it in the first callback parameter $post_id. It looks like there is a spelling error and a syntax error in your PHP code. Check here:
$current_user = wp_get_curent_user();
The method is wp_get_current_user(), so correct your spelling there.
Then check here:
$post_title = $current_user['first_name'] + " " + $current_user['last_name'];
The "+" operator shouldn't be used to concatenate strings in PHP. Use the "." operator instead:
$post_title = $current_user['first_name'] . " " . $current_user['last_name'];
Try those fixes and let me know if the 500 error is not resolved.
Hi Christian,
First off, wow crazy response time! Awesome.
Ha, I guess my Javascript skills handcuff me sometimes when working in PHP.
So I made those tweaks and was still getting a 500 ajax error.
As a test I commented out the user retrieval stuff and hard coded the wp_update_post function as seen below.
add_action('cred_save_data', 'auto_generate_post_meta', 10, 2);
function auto_generate_post_meta ( $post_id, $form_data ) {
if ($form_data['id']==1133) {
//$current_user = wp_get_current_user();
//$post_title = $current_user['first_name']." ".$current_user['last_name'];
//$slug = sanitize_title_with_dashes($post_title);
wp_update_post(array('ID'=>$post_id,'post_title'=>'Jane Doe','post_name'=>'jane-doe'));
}
}
This works as expected, however as soon as I revert back to this I get the 500 error. Seems to be something with retrieving that user info?
add_action('cred_save_data', 'auto_generate_post_meta', 10, 2);
function auto_generate_post_meta ( $post_id, $form_data ) {
if ($form_data['id']==1133) {
$current_user = wp_get_current_user();
$post_title = $current_user['first_name']." ".$current_user['last_name'];
$slug = sanitize_title_with_dashes($post_title);
wp_update_post(array('ID'=>$post_id,'post_title'=>$post_title,'post_name'=>$slug));
}
}
Okay check the documentation here:
https://codex.wordpress.org/Function_Reference/wp_get_current_user
$current_user = wp_get_current_user();
/**
* @example Safe usage:
* $current_user = wp_get_current_user();
* if ( ! $current_user->exists() ) {
* return;
* }
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
So you should use $current_user->user_firstname and $current_user->user_lastname instead of $current_user['first_name'] and $current_user['last_name'].
That did it! Christian you are a legend!
Just curious. To prevent a user creating more than 1 post, I am assuming I can use this hook with a combination of checking count of posts by the author?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data
Not going to be implementing right now, but just curious. I will mark ticket as resolved after this 🙂
I would use the cred_form_validate API since it's fired earlier, and attach an error to the post title or some other known field. For a simple non-PHP approach, use a filtered View of posts as described in this document's FAQ:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
My issue is resolved now. Thank you!