WordPress SEO插件仅对特定用户可见

时间:2014-09-03 22:51:32

标签: wordpress wordpress-plugin

我正在使用WordPress中的Yoast SEO插件,并想知道是否有办法让它只对db或functions.php文件中的某个特定用户可见?不是角色,是真正的用户。

4 个答案:

答案 0 :(得分:1)

我尝试了一种通用解决方案,只需添加“插件名称”并禁用它,但失败了。

但是,要仅向特定用户展示WPSEO(ID等于2),以下内容有效:

add_action( 'plugins_loaded', 'seo_so_25654837' );

function seo_so_25654837() 
{
    if ( '2' ==  get_current_user_id() )
        return;

    remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}

请勿将代码添加到functions.php,将其用作normal plugin

还需要从管理栏中删除SEO菜单:

add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );

function bar_so_25654837() 
{
    if ( '2' ==  get_current_user_id() )
        return;

    global $wp_admin_bar;
    $nodes = $wp_admin_bar->get_nodes();

    foreach( $nodes as $node )
    {
        if( !$node->parent )
        {
            if( 'wpseo-menu' === $node->id )
                $wp_admin_bar->remove_menu( $node->id );
        }           
    }
}

答案 1 :(得分:0)

您可以挂钩pre_current_active_plugins以在显示之前从表中删除元素。在函数中使用get_current_user_id()将允许您有选择地隐藏插件。

function hide_plugins_by_user( $all_plugins=false ) {
    global $wp_list_table;

    // if the current user ID is not 1, hide it.
    if ( 1 != get_current_user_id() ){
        // the active plugins from the table
        $plugins = $wp_list_table->items;
        // loop through them
        foreach ( $plugins as $key => $val ) {
            // use the dir + filename of the plugin to hide
            if ( $key == 'plugindir/plugin.php' ) {
                unset( $wp_list_table->items[$key] );
            }
        }
    }
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );

答案 2 :(得分:0)

此代码工作正常(Credits转到Hislop):

// Returns true if user has specific role 
function check_user_role( $role, $user_id = null ) {
    if ( is_numeric( $user_id ) )
        $user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();
    if ( empty( $user ) )
        return false;
    return in_array( $role, (array) $user->roles );
}

// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
    if( !(check_user_role('seo') || check_user_role('administrator')) ){
        // Remove page analysis columns from post lists, also SEO status on post editor
        add_filter('wpseo_use_page_analysis', '__return_false');
        // Remove Yoast meta boxes
        add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
    }   
}
add_action('init', 'wpse_init');

function disable_seo_metabox(){
    remove_meta_box('wpseo_meta', 'post', 'normal');
    remove_meta_box('wpseo_meta', 'page', 'normal');
}

只需将其放在functions.php文件中即可。

答案 3 :(得分:0)

要为所有用户禁用Yoast并为少数或特定用户启用它,只需将以下代码添加到您的function.php文件中。

            function remove_wpseo(){

                /* if you want to keep it enabled for user with id 2 */
                if ( '2' ==  get_current_user_id() ) {
                    return;
                }

                global $wpseo_front;
                if(defined($wpseo_front)){
                    remove_action('wp_head',array($wpseo_front,'head'),1);
                }
                else {
                    $wp_thing = WPSEO_Frontend::get_instance();
                    remove_action('wp_head',array($wp_thing,'head'),1);
                }

            }

            add_action('template_redirect','remove_wpseo');

参考:https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

相关问题