0

I am trying to replace the publish button text to Submit for review only for specific user role. But I can't find anything which can specify current user role and apply the filter. Here's my code, it's not working.

 add_filter( 'gettext', 'change_publish_button', 10, 2 );
function change_publish_button( $translation, $text ) {
if( !current_user_can('administrator') )  {
if ( $text == 'Publish' )
    return 'Submit for review';
};
return $translation;
}
I also tried using
    $current_user = wp_get_current_user();
if (in_array('level-1-contributor', $current_user->roles))
It didn't work either. With my little knowledge, I could understand that calling capabilities.php would require code like this -
add_action('init','do_stuff');
But I don't understand how can I achieve what I want. Need your help.

Edit for people trying to make stupid comments - gettext is used to change wordpress dashboard text's without hard-coding that. You can use gettext as functions.php function or in a plugin. Wordpress publish button in the dashboard - it's the universal post/page/custom post type publish button. So what I want - I want to use gettext filter so that I can replace publish button text to something else without editing wordpress core files. I could achieve that. But I am trying to make this function work only for specific user role. Suppose "Author". I tried adding

if (current_user_can('author'))
but it gives me error. I can't call anything that gives me user information inside gettext filter. How can I bypass that or do it in different way so that I can achieve what I want?

  • Changing the text will not affect the behaviour of the button. If the user doesn't have the publish_posts capability then the button and behaviour will automatically. – Jacob Peattie Nov 29 '17 at 3:01
  • I restricted that with - $current_user = wp_get_current_user(); if (in_array('level-1-contributor', $current_user->roles) && $post->post_status=='publish') { $my_post = array( 'ID' => $post_id, 'post_status' => 'pending', ); remove_action('save_post', 'submit_for_review_update', 25); wp_update_post($my_post); add_action('save_post', 'submit_for_review_update', 25); } – Asifur Rahman Nov 29 '17 at 3:02
  • It's strange that even if the user role doesn't have publish post capability, they get publish button on a specific post type. It doesn't occur in any other post types but one. imgur.com/a/X9nhI – Asifur Rahman Nov 29 '17 at 3:06
1

Everything you have tried should have worked perfectly, I am not sure why is this not working,

I am modifying your code a little bit to check for post type, check if it can help you,

function rename_publish_button_to_review( $translation, $text ) {

    if( !current_user_can('administrator') && 'post' == get_post_type() )  {

        if( 'Publish' == $text ) {
            return "Submit for review";
        }
    }
    return $translation;
}
add_filter( 'gettext', 'rename_publish_button_to_review', 10, 2 );

Other wise try to see if this function working when no role specified.

WILL THIS WORK ?

function rename_publish_button_to_review( $translation, $text ) {

    if( 'Publish' == $text ) {
        return "Submit for review";
    }
    return $translation;
}

function wpse_53371_bulk_actions()
{
    if ( ! is_admin() )
        return;

    if ( ! current_user_can( 'administrator' ) )
    {
        add_filter( 'gettext', 'rename_publish_button_to_review', 10, 2 )
    }
}
add_action( 'wp_loaded', 'wpse_53371_bulk_actions' );
  • Doesn't work. I get this screenshot - imgur.com/a/0u1se Can't call any user role thing inside gettext. I don't know why. It works without user level specification. – Asifur Rahman Nov 30 '17 at 6:56
  • can you try the recent edit – dipak_pusti Nov 30 '17 at 7:29
  • That is a tricky one! But it worked! You forgot a semicolon on this line - add_filter( 'gettext', 'rename_publish_button_to_review', 10, 2 ) – Asifur Rahman Dec 7 '17 at 11:39
  • You have right. – kierzniak Dec 7 '17 at 12:19

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.