WordPress SEO插件仅对特定用户可见
我正在WordPress中使用Yoast SEO插件,想知道是否有办法使它只对db或functions.php文件中的一个特定用户可见?不是角色,而是实际用户。您可以挂接到以在表显示之前从表中删除元素。在函数中使用将允许您有选择地隐藏插件WordPress SEO插件仅对特定用户可见,wordpress,Wordpress,我正在WordPress中使用Yoast SEO插件,想知道是否有办法使它只对db或functions.php文件中的一个特定用户可见?不是角色,而是实际用户。您可以挂接到以在表显示之前从表中删除元素。在函数中使用将允许您有选择地隐藏插件 function hide_plugins_by_user( $all_plugins=false ) { global $wp_list_table; // if the current user ID is not 1, hide it.
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' );
我尝试了一个通用的解决方案,简单地添加“插件名”并禁用它,但失败了 但是,要仅向特定用户显示WPSEO(
ID
equals2
),以下操作有效:
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
,请将其用作一个函数
从管理栏中删除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 );
}
}
}
此代码运行良好(学分归为):
只需将其放在functions.php文件中。要为所有用户禁用Yoast,并为少数用户或特定用户启用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');
参考资料:从我的头顶上,你可以用一个检查当前用户ID的函数来代替检查功能的函数,比如:
获取当前用户ID
,it just and if和ID。太棒了!我用你的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');