Skip Navigation

[Resolved] Saving user first name/last name as post title + post date/time

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

Problem: I would like to use the post author's name with post date and time to automatically set a new post title.

Solution: Use the save_post hook provided by WordPress to automate the post title. Get author information using get_the_author_meta, concatenate the title string, and use wp_update_post to update the post title. Example code:

function md_checkout_title( $post_id ) {
  if( 'material-check-out' == get_post_type($post_id) ) {
    remove_action( 'save_post', 'md_checkout_title' );
    $date = date( 'Y-m-d H:i:s', time() );
    $author_id = get_post_field( 'post_author', $post_id );
    $first_name = get_the_author_meta( 'first_name', $author_id );
    $updated_data = array(
      'ID' => $post_id,
      'post_title' => $first_name . $date
    );
    wp_update_post( $updated_data );
    add_action( 'save_post', 'md_checkout_title' );
  } else {
    return;
  }
}
add_action( 'save_post', 'md_checkout_title' );

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_update_post/
https://developer.wordpress.org/reference/hooks/save_post/

This support ticket is created 4 years, 4 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 – 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 22 replies, has 3 voices.

Last updated by Arrien 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1409033

Hello,

Previously I had a CPT called members that was used to save post titles under a member's name.
Now I have migrated those CPT's to users.
I want to have an admin login,
search for a user in a form field,
select the user name,
fill in the rest of the form,
submit form.
I want the form to be able to save the user's first name/last name as the post title plus the post date and time.
I also want the post author to be saved as the user that was selected in the search field.

Does that make sense?
Is this possible?

Thank you in advance.

#1410759

Hi, yes I think I understand what you'd like to accomplish. There are a few challenges to overcome here:
1. There is no "User select" or "User search" field in Forms. You could get around this by creating a generic select field that contains an option for each User. The options can be provided by a View of Users. This is probably the most practical solution, but if you have a large number of Users it can be difficult to search for a specific User in a standard select field. There is no autosuggest feature here.
2. If you want to programmatically set the post title using the date, time, and User information, and if you want to set the post author programmatically, you'll need some custom code because none of those features exist in Forms. We have the Forms API available that will let you trigger your own custom PHP code when a Form is submitted: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Click the +More button to see some code samples.

You will use the WordPress API wp_update_post to change the post author and title: https://developer.wordpress.org/reference/functions/wp_update_post/

I can help show you examples here on the site if you'd like to see similar tickets.

#1413919

Hi Christian,

1. So if I understand correctly, I would have to write the user's name properly in order to find them?
How would I go about creating that view?

2. Can you show me some examples so that I can test some options?

Thank you

#1414977

1. So if I understand correctly, I would have to write the user's name properly in order to find them?
Not exactly, you will filter using a "select" field where the options are already written out for you. You would have to select the correct name. If there are a lot of Users, you could spend more time scrolling. Advanced Users may know that you can tab into the select field and then start typing to find the correct User, but just to reiterate this isn't an autosuggest. It's a normal select field.

2. Can you show me some examples so that I can test some options?
Sure, here's one that updates a post title after Form submission:
https://toolset.com/forums/topic/trying-to-grab-parent-title-to-use-as-childs-title/

Here's one that updates post author after Form submission:
https://toolset.com/forums/topic/change-author-on-form-submission/

Here's another one that shows how to use a View and the "List with separators" format to produce options for a generic field:
https://toolset.com/forums/topic/use-toolset-views-inside-toolset-form-generic-field/#post-1153069

Here's WP documentation that shows examples of getting User information like first name, last name, and so on:
https://codex.wordpress.org/Function_Reference/get_user_meta

#1417107

Hi Christian,

1. Views has changed quite a lot since I first started using it so I am not sure how to create a View of users that I can then select from.
I wonder if it would be esier for admins to create a CPT from the back end and be able to select the user as author in the CPT creation process?

2. Going to test this tomorrow.

Thanks

#1417247

1. Views has changed quite a lot since I first started using it so I am not sure how to create a View of users that I can then select from.
The idea is that you will create a generic field in the Form. It will be a "select" type generic field. Here is a general overview of generic fields:
https://toolset.com/documentation/user-guides/front-end-forms/inserting-generic-fields-into-forms/

The options for a generic select field are usually added manually to the generic field shortcode in a Form builder. The generic field shortcode would look something like this:

[cred_generic_field type='select' field='generic-user-select']
{
"required":0,
"default":[],
"options":[{"value":"12","label":"John Doe"},{"value":"23","label":"George Smith"},{"value":"34","label":"Betty Rogers"},{"value":"45","label":"Pat Miller"}]
}
[/cred_generic_field]

However, in your case, the options are not "hard-coded" in the Form builder. Instead, you will place a View in the spot where the options would normally exist:

[cred_generic_field type='select' field='generic-user-select']
{
"required":0,
"default":[],
"options":[[wpv-view name="your-formatted-user-view"]
}
[/cred_generic_field]

That View should be a View of Users that outputs a special format that can be used by the generic field:
https://toolset.com/forums/topic/use-toolset-views-inside-toolset-form-generic-field/#post-1153069

#1422335

Hi Christian,

I am not sure I'm doing this right:

<wpv-loop>
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=other]
{"value":"[wpv-post-id]","label":"[wpv-post-title]"},
[wpv-item index=last]
{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]
</wpv-loop>

[credform]
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">

<label>Member name</label>
[cred_generic_field type='select' field='generic-user-select']
{
"required":0,
"default":[],
"options":[[wpv-view name="member-name"]
}
[/cred_generic_field]
</div>

<div class="form-group">
<label>Material Type(s)</label>
[cred_field field='material-types-out' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Material Weight</label>
[cred_field field='material-weight-out' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Material Value</label>
[cred_field field='material-value' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Check-out notes</label>
[cred_field field='check-out-notes' force_type='field' class='form-control' output='bootstrap']
</div>
[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

Happy Holidays

#1422523

It looks okay at first glance, but I would need to see it in the browser to be sure. Is the select field appearing? Are the options correct? If no, place the View directly in the Form (not in the select field, just in the main form content) as a test so we can inspect the output. Then let me know where I can see the output on the front-end of the site. It would probably be helpful to get login criteria as well, so I have activated private reply fields here.

#1431683

The wpv-loop code should not be placed directly in the Form builder, as you had it. I have removed that code for you, and added the View shortcode just above the generic field so you can see the output it produces while we test.

I'm not able to see anything produced by the member-name View. In fact, I'm not able to see the member-name View at all, because you're using the new Blocks plugin instead of Views, with only the blocks interface enabled. That removes the View item from the Toolset menu. I enabled both the legacy and blocks editor in Toolset > Settings > General so I can see the Views menu item. You may decide to change that later if you want to only display the Blocks editor. Now I can see that there is no member-name View, or any other View:
hidden link

So I created the View for you:
hidden link
You may want to adjust the View settings to determine which Users will appear in the select options.
The following code is in the Loop Editor to produce the format we need:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
		[wpv-item index=other]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]"},
[wpv-item index=last]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]"}
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

Now I can see options appearing in the select field here:
hidden link

I can also see the options in JSON format just before the generic field. I left these so you can see how it should look. When you are ready to remove them, remove the corresponding View shortcode from the Form builder.

#1436499

Hi Christian,

This seems to work for the most part now, but I'm wondering about something essential. I need the user that is selected to become the author of the post and not the logged-in user. Is that possible in this case?

Thank you for your help so far on this case.

#1436669

I need the user that is selected to become the author of the post and not the logged-in user.
Yes, this is what I expected you want to accomplish. To programmatically change the author of the post, you need to know the desired author's User ID and the post ID. Then you can use the wp_update_post method to change the post author: https://wordpress.stackexchange.com/questions/186253/programatically-change-post-author
https://developer.wordpress.org/reference/functions/wp_update_post/

Each option generated by the View includes the author's User ID and the author's first/last name. The person who submits the form will be able to choose an authors name, and in the cred_save_data hook you will be able to access the corresponding User ID in the $_POST superglobal. Assuming the slug of the generic field is generic-user-select, the code to access the selected User's ID will look like this:

$author_id = $_POST['generic-user-select'];

Then to set the post's author using that author's ID:

 
$args = array(
    'ID' => $post_id,
    'post_author' => $author_id,
);
wp_update_post( $args );
#1436977

Hi Christian,

I am not sure I follow.

The form would only be accessible by admin.
They would select the name in the Member Name field.
They would fill in the rest of the form and click Submit.
The form would take the name that was selected at the top of the form and save the Check-out CPT as the user's name, date and time and automatically make the name selected the author.
There should be no secondary operations after the form has been submitted.

I am wondering if it makes more sense to have admin fill out a new Check-out CPT on the backend and use the Author drop-down there?

#1440967

The form would take the name that was selected at the top of the form and save the Check-out CPT as the user's name, date and time and automatically make the name selected the author.
I understand that would be ideal, but Forms cannot do all this automatically. There is no "change the post author" functionality built-in to an Edit Post Form. Beyond that, setting a post title automatically based on the post author's name and publish date isn't built-in to Edit Post Forms. These customizations require extra code.

#1445093

Hi Christian,

I've gone in the direction of using the back end instead to create the CPTs MD CHeck-outs, Shed Check-outs and Check-ins.

In a previous version of the website I built, I had written, with the help of Toolset folks like yourself, this plugi-in:

//Save Check-in and Check-out Member name as post title.
add_action('cred_save_data','item_title',10,2);
function item_title($post_id, $form_data) {
$type = get_post_type($post_id);
if ($type == 'material-check-out') {

$parent_id = $_POST["_wpcf_belongs_member_id"];
$title = get_the_title($parent_id);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status' => 'publish'));
}

if ($type == 'material-check-in') {

$parent_id = $_POST["_wpcf_belongs_member_id"];
$title = get_the_title($parent_id);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status' => 'publish'));
}
}

How do I make it so it saves the CPT titles of the 3 mentioned above?

#1447393

I've gone in the direction of using the back end instead to create the CPTs MD CHeck-outs, Shed Check-outs and Check-ins...How do I make it so it saves the CPT titles of the 3 mentioned above?
You can't really do that because creating and editing CPT posts in wp-admin bypasses the Forms API. This cred_save_data hook is never triggered, so it cannot be used to automate changing the post titles from wp-admin. To automate post details in wp-admin, you would use the WordPress save_post API: https://developer.wordpress.org/reference/hooks/save_post/

Am I misunderstanding what you want to accomplish?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.