Skip Navigation

[Gelöst] Display last modified author in views

This support ticket is created vor 4 Jahre, 4 Monate. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 7 Antworten, has 2 Stimmen.

Last updated by Nigel vor 4 Jahre, 4 Monate.

Assisted by: Nigel.

Author
Artikel
#1406345

I need to display the last modified author/user for a post.

I have tried this method in a content template but the output is blank. https://toolset.com/forums/topic/need-shortcode-to-view-the-user-that-last-modified-a-post/

My code:
add_shortcode('last-modified-author', function( $atts = [], $content=null ) {
return get_the_modified_author();
});

In my content template:
Last edited by [last-modified-author] on [wpv-post-date type="modified"]

My output:
Last edited by on 10/12/2019

Thanks for help.

#1406951

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

How are the posts being published or edited?

I just tested this locally, and I see it working for content published from the backend, but seemingly not for content published through front-end forms.

Is that the case on your site?

#1407935

I need this to work for content created with front end forms. This should show the last person who edited this post, be it create (first time) or any subsequent edits, all from front end.

#1408023

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

I've escalated this to see whether it is something the developers will add to Forms, though it's essentially a feature request and I'm not sure how long it may take to be implemented assuming it is accepted.

So in the meantime you would need to use the Forms API, such as the cred_save_data hook, to add/update that hidden custom field "_edit_last" with the ID of the current user submitting the form.

See https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1408051

I'm curious because the last thread (https://toolset.com/forums/topic/need-shortcode-to-view-the-user-that-last-modified-a-post/) seems to work with a native wordpress code, why is it not now. In any case this looks like a basic thing for a content management system.

#1408173

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

It's not the native WordPress function that is the problem, that works.

The problem is that when you publish content from the backend WordPress adds a hidden custom field "_edit_last" to the post which records the user that last made an edit, which is what the function retrieves in order to display the last editor of the post.

But content created programmatically, using wp_insert_post, for example, doesn't add that hidden custom field.

So content submitted through front-end forms—which would typically use wp_insert_post—don't get that hidden custom field added.

I just checked and it's the same with Gravity Forms, for example, and I suspect you would find the same with all other form plugins.

I already had word back that this would be a feature request, so could you please post such a request at https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

In the meantime you can use the Forms API to add the hidden custom field yourself.

Here's an example of the code you would need:

add_action('cred_save_data', 'ts_add_edit_last',10,2);
function ts_add_edit_last($post_id, $form_data)
{
    $target_forms = array( '123', '456', '789' );
    if ( in_array( $form_data['id'], $target_forms ) )
    {
        $user_id = get_current_user_id();
        if ($user_id != 0) {
           update_post_meta($post_id, '_edit_last', $user_id);
        }
    }
}
#1409319

Thanks for the explanation. So I guess this can't be done with a hidden cred generic field as well?

Can you tell what needs to be replaced in the array (123,456 etc)? the post id?

#1411591

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

The problem with using a hidden generic field with that slug is that you could provide the ID of the current user as the default value for the post publish form, but it wouldn't work in post edit forms if the post was edited by someone else.

The API hook is the way to go.

That array is an array of form IDs of the forms this should be applied to.

If you go to Toolset > Post Forms you will see the IDs of each of your forms.

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