doremifasodo0008008 2016-12-23 13:43
浏览 197
已采纳

在Wordpress中的某些管理页面上自定义css和JS

I created a plugin to export some specific content in Wordpress ( called cav and located in /wp-content/plugins/cav ) and I added its admin menu link in the Tools main menu as follows:

define( 'CAV_DIR', plugin_dir_path( __FILE__ ) );
define( 'CAV_URL', plugins_url()."/cav" );

function cavlfo_admin_enqueue($hook) {
global $post;
if ( $hook == 'post-new.php' || $hook=='tools.php?page=CAV' ) {

        wp_enqueue_style( 'cavlfo_style', CAV_URL. '/css/cav.css' );
  wp_enqueue_style(  'cavlfo_targetted', CAV_URL.'/css/targettedcss.css' );
        wp_enqueue_script(  'cavlfo_targetted', CAV_URL.'/js/targetted.js', array("jquery") );
}
}
add_action( 'admin_enqueue_scripts', 'cavlfo_admin_enqueue' );

All this works fine when I used

if( $hook == 'post-new.php' || $hook=='tools.php' ){ . . .}

but I want it to work ONLY when it's actually

if( $hook == 'post-new.php' || $hook=='tools.php?page=CAV' ){ . . .} (Only on the page is CAV )

Any suggestion?

  • 写回答

1条回答 默认 最新

  • doudai8783 2016-12-23 16:01
    关注

    After some digging I got this:

    function CAV_options_page()
    {
      $submenu= add_submenu_page(
            'tools.php',
            'Cav Options',
            'Cav Options',
            'manage_options',
            'CAV',
            'CAV_options_page_html'
        );
    add_action( 'admin_print_styles-' . $submenu, 'CAV_custom_enqueue' );
    add_action( 'admin_print_scripts-' . $submenu, 'CAV_custom_enqueue' );
    }
    add_action('admin_menu', 'CAV_options_page');
    

    Then I put the styles and scripts that need to be enqueued in a function

    function CAV_custom_enqueue(){
        wp_enqueue_style(  'cav_targetted', CAV_URL.'/css/targettedcss.css' );
        wp_enqueue_script(  'cav_targetted', CAV_URL.'/js/targetted.js', array("jquery") );
    }
    

    I hope it helps someone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥100 华为ens111111111111111111111111111111111111111111
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题