2016-11-18 56 views
0

我想添加一个函数来添加一个脚本到wp_head(),但我想这个脚本被加载的任何一个,但管理员。所以我使用条件if(!is_admin)来实现这一点。但它一直为管理员加载。需要做什么修改? 这里是我使用的代码:只有在脚本不是管理员的情况下才能加载脚本?

function add_this_script_footer(){ 
    if(!is_admin()) : 
    ?> 
     <script type="text/javascript"> 
      var message="My massage"; 
      function clickIE4(){ 
       if (event.button==2){ 
        alert(message); 
        return false; 
       } 
      } 

      function clickNS4(e){ 
       if (document.layers||document.getElementById&&!document.all){ 
        if (e.which==2||e.which==3){ 
         alert(message); 
         return false; 
        } 
       } 
      } 

      if (document.layers){ 
       document.captureEvents(Event.MOUSEDOWN); 
       document.onmousedown=clickNS4; 
      } else if (document.all&&!document.getElementById){ 
       document.onmousedown=clickIE4; 
      } 

      document.oncontextmenu=new Function("alert(message);return false") 
     </script> 
    <?php 
    endif; 
} 
add_action('wp_footer', 'add_this_script_footer'); 
+2

要检查,如果用户是管理员,你会使用current_user_can( '管理员') - 或者,甚至更好,检查一个特定的能力。这是你需要的吗?它来自[这里](http://stackoverflow.com/questions/21239226/if-user-is-not-admin-deregister-jquery) – theoretisch

+0

非常感谢'!current_user_can('administrator')' –

回答

1

创建主题文件夹内的js文件。

Mycustomjs.js

 var message="My massage"; 
     function clickIE4(){ 
      if (event.button==2){ 
       alert(message); 
       return false; 
      } 
     } 

     function clickNS4(e){ 
      if (document.layers||document.getElementById&&!document.all){ 
       if (e.which==2||e.which==3){ 
        alert(message); 
        return false; 
       } 
      } 
     } 

     if (document.layers){ 
      document.captureEvents(Event.MOUSEDOWN); 
      document.onmousedown=clickNS4; 
     } else if (document.all&&!document.getElementById){ 
      document.onmousedown=clickIE4; 
     } 

     document.oncontextmenu=new Function("alert(message);return false") 

这段代码添加到function.php

/*add js from template root folder*/ 

function my_custom_scripts() { 
    if(!current_user_can('administrator')){ 
    wp_enqueue_script('my-custom-jquery', get_template_directory_uri() . '/Mycustomjs.js', array('jquery')); 
    } 
} 
add_action('wp_enqueue_scripts', 'my_custom_scripts'); 
相关问题