2013-10-28 22 views
0

我創建在WordPressWordPress的jQuery的加僅限

我添加幾行jQuery的覆蓋基本WordPress的功能插件,包括通過使用wp_register_script和wp_enqueue_script這個文件的插件。

但我希望只有當管理員在我的插件頁面上時,這個文件纔會包含,因此這些行只會執行我的插件頁面。

我需要知道是否有任何方式,所以我可以知道管理員是否在我的插件頁面上。

請幫忙,如果您需要更多關於問題的信息,請發表評論。

更新:

我知道我可以通過添加

if($_GET['page']=='my_plugin_page') { 
    //my code 
} 

這樣做,但爲了這個,我不得不寫,如果我的所有頁面發言,我想用比這一些不錯的方法..

回答

1

檢查current_user是否可以manage_options。這將表明他們是管理員。

PHP:

if (current_user_can('manage_options')) { 
    /* A user with admin privileges */ 
} else { 
    /* A user without admin privileges */ 
} 
+0

搶我不想檢查無論用戶是否是管理員,而是我想只有當管理員在我的插件頁面上才執行jquery .. –

+0

那麼你會包括ja vascript文件,如果他們是管理員。 –

+0

插件不會在其他後端頁面上執行 - 所以這種解決方案非常接近您的要求。 – feeela

0
$mypage = add_management_page('myplugin', 'myplugin', 9, __FILE__, 'myplugin_admin_page'); 
add_action("admin_print_scripts-$mypage", 'myplugin_admin_head'); 

function myplugin_admin_head() { 
    // what your plugin needs in its <head> 
} 

希望這個作品!

+0

什麼是add_management_page方法? –

+0

它將子菜單頁面添加到工具主菜單 –

1

你可以嘗試這樣的事情來檢查管理記錄在

<? 
get_currentuserinfo() ; 
global $user_level; 
if ($user_level > 9) { 
    //user is admin and add your js file 
} 
//

OR

if (current_user_can('administrator')) { 
    /* Your code */ 
    } 

// OR

if(is_admin() === true) { 
/*your code*/ 
} 
?>