2014-09-03 286 views
0

我在WordPress中使用Yoast SEO插件,并想知道是否有一种方法可以让它只对数据库中的一个特定用户或functions.php文件可见?不是一个角色,一个实际的用户。只对特定用户可见的WordPress SEO插件

+0

从我的头顶,你可以替换检查与功能检查当前用户的ID,如capabilties功能:'get_current_user_id',它的只是和如果和身份证。 – 2014-09-03 23:01:13

回答

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

下还需要去除从管理栏的搜索引擎优化的菜单:

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); 
     }   
    } 
} 
+0

太棒了!我用你的php代码创建了一个插件,它完美地工作。谢谢你的帮助。 – Brendan 2014-09-04 15:24:37

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'); 
0

此代码工作正常(现金去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文件。

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/

相关问题