0

On my websites the contributors (user role contributor) are allowed to post posts. All their posts are not published automatically (these posts are with pending status). After approving their first post, i modify their user role to author. This is a manual process and i want to make it automatically. So when their first post gets approved their user role gets changed automatically from contributor to author.

I use a custom post type for this posts. It is called 'portfolio'. I was trying to modify this code snippet, but i can't make it working. What I am doing wrong?

 add_action( 'pending_to_publish'. 'my_function', 10, 1 );
function my_function( $post )
{
    $wp_user_object = new WP_User( $post->post_author );
    if ( in_array('contributor', $wp_user_object->roles ) ) {
        $wp_user_object->remove_role( 'contributor' );
        $wp_user_object->add_role( 'author' );
    }
}

1 Answer 1

0

There is a syntax error in the add_action(). You've used a period . instead of a comma , to separate the action and the callback. Otherwise the callback seems to work as expected when I tested it locally.

The correct syntax would be,

add_action( 'pending_to_publish', 'my_function', 10, 1 );

When doing development work with WordPress it is a good idea to have debugging on. This way you can check the log file for any errors when something is not working as expected. Further reading, Debugging in WordPress.


P.S. If you want the role change to happen only when your custom post type is published, then add a post type check to the top of the callback. Like so,

function my_function( $post )
{
    if ( 'portfolio' !== $post->post_type ) {
        return;
    }
    
    // code...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.