Skip Navigation

[Résolu] How to pass ID of the person performing the edit

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to capture information about the User who submitted a CRED edit post form so I can moderate submissions efficiently from wp-admin.

Solution: Use a generic field in the form to capture the current User's ID, then use the CRED API to do something with that information.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

This support ticket is created Il y a 6 années et 7 mois. 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)

Ce sujet contient 23 réponses, a 3 voix.

Dernière mise à jour par johnR-22 Il y a 6 années et 6 mois.

Assisté par: Christian Cox.

Auteur
Publications
#880015
Image2.jpg

I have a simple edit form that registered users can go about updating vehicle posts.

I want to be able to capture the person who made the edit, so I can credit them as a contributor. Im guessing I need to pass the user ID or something via a hidden form field???

At the moment, the edits come through pending approval, in which case I approve them as admin, but I have no idea who made the changes.

Thanks guys - this really is a brilliant piece of software!!

#880995

Hi, sure you can do this with a generic hidden field in CRED. You can supply any Views shortcode as the value of a generic field, then capture the value in a cred_save_data hook.

Here's a generic field that captures the current User's ID:

[cred_generic_field field='userid' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-user field='ID']",
"persist":1
}
[/cred_generic_field]

Here's a cred_save_data hook where you can access this information in PHP:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    // do something with the editing User ID
    $editor_user_id = $_POST['userid'];
    // your code goes here
  }
}

Replace 12345 with the numeric ID of this CRED form.

As far as what you do with this information, it depends on what you actually want to accomplish. You can set the editor to be the author of the post, or you can set some custom field value on the post to be the editor's ID, or you can add the editor's ID to a repeating custom field on the post that tracks all the editing Users.

https://toolset.com/documentation/user-guides/views-shortcodes/
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#893176

Thank you Christian.
Sorry mate, Im certainly not a developer, so I will have to ask some pretty simple questions.

1. Where do I put this code? In the actual form itself, or in the JS editor section?

[cred_generic_field field='userid' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-user field='ID']",
"persist":1
}
[/cred_generic_field]

I want to show the person who made the update in the admin back end. Not sure how to do that just yet.

#893189
Image2.jpg

I am probably well out of my depths on this lol

Here's a screen shot to better explain.

#897756

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi John,

As Christian is currently on vacation at the moment, I will be handling this ticket.

A quick question is this being updated on the frontend by a different author ? As if its done by a non-admin person then it should show a different user here under the revisions.

Also you want to store the ID of the last person who performed the edit correct?

Thanks,
Shane

#897757

Hi Shane,
Cheers mate.

A quick question is this being updated on the frontend by a different author ?
Yes. Multiple authors, all on the front end - much like contributors.

As if its done by a non-admin person then it should show a different user here under the revisions.
Correct.

Also you want to store the ID of the last person who performed the edit correct?
I guess so, although Im not quite sure if thats necessary? I just want to see who made the update in revisions as shown. Because Id like to list them as a "contributor" to the content.

#897858

I think I might know why this isnt working.

Because the post is being submitted as "pending review" and not actually going live.
Then of course as the administrator, Im clicking on publish, which is why its probably showing me instead.

Would this be right?

#898232

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi John,

That is actually correct.

The person who publishes the post is the one who will be shown on the review list.

So if your contributor publishes the post then it will show them as the contributor.

Thanks,
Shane

#898239

Thanks Shane.

Is there any way of getting their name without asking them to enter it within the form?

#898829

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi John,

I'm a bit confused right now sorry.

Is there any way of getting their name without asking them to enter it within the form?

There names would show up on the revisions section if the post is published so there shouldn't be any need to get the user's name.

Thanks,
Shane

#899853

Sorry I meant the edit form they're filling out.

Im guessing Id have to ask for their name so I know who made the revision.

#901075

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi John,

I understand what you're trying to do now.

Ok so here is what we can do. We can get their user ID and then get there username from their ID and then insert it into a custom field.

So what you can do is create a custom field called, revision author.

Add that custom field to the form then finally pass this to the custom field as its value attribute.
[wpv-current-user]

This should display their username in the field and then store it once the form has been submitted.

Thanks,
Shane

#901685

Ok so I managed to get the persons name.

But what if there's two people with the same name???

This is what I have....I dont know where to put the code to get their ID?

<div class="form-group">
<label>Making changes as [wpv-current-user]</label>
[cred_field field='revision-author' post='vehicle' value='[wpv-current-user]" urlparam='' readonly='true' class='form-control' output='bootstrap']
</div>

#901759

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi John,

The code here
<div class="form-group">
<label>Making changes as [wpv-current-user]</label>
[cred_field field='revision-author' post='vehicle' value='[wpv-current-user]" urlparam='' readonly='true' class='form-control' output='bootstrap']
</div>

Should already give you the relevant information. To get the ID you need to do [wpv-current-user info='id']

Thanks,
Shane

#901809

Thanks Shane, where do I put that?

Like this?

<div class="form-group">
<label>Making changes as [wpv-current-user]</label>
[cred_field field='revision-author' post='vehicle' value='[wpv-current-user info='id']" urlparam='' readonly='true' class='form-control' output='bootstrap']
</div>