Changing a user's role on post creation

Created by Phil Kurth, Modified on Thu, 22 Dec, 2022 at 3:50 PM by Phil Kurth

If you need to change the user role of the logged in user creating a post, you may use the af/form/editing/post_created hook to run some custom logic as per the following example:


add_action( 'af/form/editing/post_created', function ( $post, $form, $args ) {

  // If we need to run this only on a specific form, check for the form key.
//  if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
//    return;
//  }

  // …add any conditions or logic you need here to safeguard against unwanted user modifications…
  
  // Change the role of the post author
  wp_update_user( [
    'ID' => $post->post_author,
    'role' => 'editor'
  ] );

}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article