Skip Navigation

[Resolved] Auto populate post title with author's name

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by richardG-4 4 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1400365

Tell us what you are trying to do?
When a specific form is submitted I want the title to be populated with the author's name and the post date.

Is there any documentation that you are following?
Not that I have found, though I have checked forums.

Is there a similar example that we can see?
This is what I have right now in my functions.php file:

////////
// Display Current Date
add_shortcode('wpv-post-today', 'today_shortcode');
    function today_shortcode() {
    return date( 'F j, Y', current_time( 'timestamp' ));
}

/////////////
// Auto Populate Payment Plan Application Title
add_action('cred_submit_complete', 'auto_populate_payment_title',10,2);
function auto_populate_payment_title($post_id, $form_data){
    // Edit form ID to match form ID
    if ($form_data['id']==11714) {
        $author=get_post_meta($post_id, 'wpv-post-author')
        $today_date = current_time( 'Y-m-d' );
        $payment_plan_title = array(
            'ID'           => $post_id,
            'post_title'   => $author . ' - ' . $today_date,
        );
   wp_update_post( $payment_plan_title );
	}
}

What is the link to your site?

#1400717

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Richard

there are several problems with your code.

I took a look at it and updated it, though I haven't tested it, but you can try the following:

add_action('cred_submit_complete', 'auto_populate_payment_title', 10, 2);
function auto_populate_payment_title($post_id, $form_data)
{
    // Edit form ID to match form ID
    if ($form_data['id'] == 11714) {
        $the_post = get_post($post_id);
        $the_author_id = $the_post->post_author;
        $the_author_name = get_the_author_meta('display_name', $the_author_id);
        $today_date = date('Y-m-d');
        $payment_plan = array(
            'ID' => $post_id,
            'post_title' => $the_author_name . ' - ' . $today_date,
        );
        wp_update_post($payment_plan_title);
    }
}

You will want to update your custom shortcode, too, there is no such function as current_time in PHP.

#1401027

There is no current_time function?? That's weird, the functions work for the other form title I am auto populating, haha. Here is the other code, can you tell me why it is working if there is no "current time" function?

////////
// Display Current Date
add_shortcode('wpv-post-today', 'today_shortcode');
    function today_shortcode() {
    return date( 'F j, Y', current_time( 'timestamp' ));
}

/////////////
// Auto Populate Repair Title
add_action('cred_submit_complete', 'auto_populate_title',10,2);
function auto_populate_title($post_id, $form_data){
    // Edit form ID to match form ID
    if ($form_data['id']==888) {
        $parentpost_id = toolset_get_related_post($post_id, 'membership-vehicle-repair' );
        $parent_title = get_the_title($parentpost_id);
        $today_date = current_time( 'Y-m-d' );
        $childpost_title = array(
            'ID'           => $post_id,
            'post_title'   => $parent_title . ' - ' . $today_date,
        );
   wp_update_post( $childpost_title );
	}
}

------------------------------------------------------------------

The code you shared gave me this error: Parse error: syntax error, unexpected '$today_date' (T_VARIABLE) in /wp-content/themes/oceanwp-child/functions.php on line 113

It is also not changing or updating the title for the Payment Plan Application form.

#1401397

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I looked through my code again and spotted a typo, the last line should read

wp_update_post($payment_plan);

Sorry about that.

I then checked the code on my own site and it worked correctly.

As for current_time, it seems it's a WordPress function, although it seems somewhat redundant as it is doing the same as the native PHP date function, so either should work.

#1402095

That worked perfectly, thank you!!

#1402097

I changed the function a little so it grabs their first and last name instead of the display name:

/////////////
// Auto Populate Payment Plan Title
add_action('cred_submit_complete', 'auto_populate_payment_title', 10, 2);
function auto_populate_payment_title($post_id, $form_data)
{
    // Edit form ID to match form ID
    if ($form_data['id'] == 11714) {
        $the_post = get_post($post_id);
        $the_author_id = $the_post->post_author;
        $the_author_first_name = get_the_author_meta('first_name', $the_author_id);
        $the_author_last_name = get_the_author_meta('last_name', $the_author_id);
        $today_date = date('Y-m-d');
        $payment_plan = array(
            'ID' => $post_id,
            'post_title' => $the_author_first_name . " " . $the_author_last_name . ' | ' . $today_date,
        );
        wp_update_post($payment_plan);
    }
}
#1402099

My issue is resolved now. Thank you!

Edit: I think I accidentally marked this as "Very Hard" because a score of 5 is normally good, but this was actually "Very Easy." Thanks again!

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