Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading scripts only for certain admin page

Tags:

wordpress

I am trying to add a date and time-picker for option-tree framework on my current project. I am using global $pagenow; but was wondering if there is another way of loading the scripts on certain admin page?

Here is my code:

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 );

}
like image 565
Jeton R. Avatar asked Aug 19 '26 11:08

Jeton R.


1 Answers

Best way to en-queue admin scripts using the recommended method from WordPress if anyone needs it.

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');
like image 114
Jeton R. Avatar answered Aug 21 '26 07:08

Jeton R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!