Skip Navigation

[Resolved] I am trying to set post slug and title from a Front End Form

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that is used to create posts. I would like to set the post title and slug based on the current logged-in User's information.

Solution: Use the Forms API to set the post title and slug:

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->user_firstname." ".$current_user->user_lastname'];
    $slug = sanitize_title_with_dashes($post_title);
    wp_update_post(array('ID'=>$post_id,'post_title'=>$post_title,'post_name'=>$slug));
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/
https://codex.wordpress.org/Function_Reference/wp_get_current_user

This support ticket is created 5 years 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 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by mikaelD 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1374053

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?

#1374079

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.

#1374091

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));
  }
}
#1374097

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'].

#1374101

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 🙂

#1374103

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/

#1374105

My issue is resolved now. Thank you!