Question

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?

Was it helpful?

Solution

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' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top