[英]Wordpress add jquery limited to plugin only
我在wordpress中創建一個插件
我添加了一些jQuery行來覆蓋基本的wordpress功能,並使用wp_register_script和wp_enqueue_script包含此文件。
但我希望只有管理員在我的插件頁面上才能包含此文件,因此這些行僅針對我的插件頁面執行。
我需要知道有沒有辦法讓我知道管理員是否在我的插件頁面上。
如果您需要有關問題的更多信息,請提供幫助,評論。
更新:
我知道我可以通過添加來實現
if($_GET['page']=='my_plugin_page') {
//my code
}
但為此我必須為我的所有頁面寫if語句,我想使用一些比這更好的方法..
檢查current_user是否可以manage_options。 這表明他們是管理員。
PHP:
if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
} else {
/* A user without admin privileges */
}
您可以嘗試這樣的操作來檢查管理員是否已登錄
<?
get_currentuserinfo() ;
global $user_level;
if ($user_level > 9) {
//user is admin and add your js file
}
//要么
if ( current_user_can( 'administrator' ) ) {
/* Your code */
}
//要么
if(is_admin() === true) {
/*your code*/
}
?>
$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>
}
希望這個有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.