仅为特定管理页面加载脚本

时间:2013-11-26 15:01:31

标签: wordpress

我正在尝试为当前项目添加选项树框架的日期和时间选择器。我正在使用global $pagenow;,但想知道是否有其他方法可以在某个管理页面上加载脚本?

这是我的代码:

global $pagenow;

//Check if current admin page is Option Tree settings
if ( $pagenow == 'themes.php' && $_GET['page'] == 'ot-theme-options' ) {

    // Register and Enqueue Date & Time Picker Scripts
    function orn_admin_register() {

            wp_enqueue_script( 'jquery-date-timepicker', get_template_directory_uri()."/inc/backend/datetimepicker/jquery.datetimepicker.js", array('jquery'),'1.4.2',true);

            wp_enqueue_style('jquery-date-timepicker-style', get_template_directory_uri().'/inc/backend/datetimepicker/jquery.datetimepicker.css', false);
    }

    add_action( 'admin_enqueue_scripts', 'orn_admin_register' ); 

    //Add Inline script to trigger Date & Timepicker
    function admin_inline_js(){
        echo "<script type='text/javascript'>\n";
        echo "jQuery(document).ready(function($) {";
        echo "\n$('#orn_days_underc').datetimepicker()";
        echo "\n})\n</script>";

    }
    add_action( 'admin_print_scripts', 'admin_inline_js', 20 );

}

1 个答案:

答案 0 :(得分:2)

如果有人需要,可以使用WordPress中推荐的方法对管理脚本进行排队。

function custom_option_tree_admin_scripts($hook) {

      if   ( 'appearance_page_ot-theme-options' == $hook ) { 

          //Enqueue some scripts or styles here only for Option Tree

      }
}
add_action('admin_enqueue_scripts', 'custom_option_tree_admin_scripts');