0

Hello I want to add some js and css files to the WordPress admin panel footer. But I want that these files are only in Widgets page. Guid me pls. Thank you.

1 Answer 1

0

You can use $pagenow global variable for this, which would obviously return widgets.php when in widgets dash.

If you want to enqueue CSS/JS by paths:

add_action( "admin_enqueue_scripts", "wpse_250923_enqueue_scripts" );

function wpse_250923_enqueue_scripts() {
    global $pagenow;
    // check for widgets
    if ( "widgets.php" !== $pagenow )
        return; // nothing to do here

    // enqueue CSS
    wp_enqueue_style( "my-css-handle", "path/to/some/css/file.css" );

    // enqueue JS
    wp_enqueue_script( "my-js-handle", "path/to/some/js/file.js", array('jquery') /*dependencies*/ );
}

Otherwise if all you have is some inline JavaScript and/or inline CSS (not stored in a file) then assign admin_head for the CSS and admin_footer for the JS:

add_action( "admin_init", "wpse_250923_enqueue_scripts" );

function wpse_250923_enqueue_scripts() {
    global $pagenow;
    // check for widgets
    if ( "widgets.php" !== $pagenow )
        return; // nothing to do here

    // enqueue CSS
    add_action( "admin_head", "wpse_250923_print_css" );

    // enqueue JS
    add_action( "admin_footer", "wpse_250923_print_js" );
}

function wpse_250923_print_css() {
    print '<style>/*CSS tweaks*/</style>' . PHP_EOL;
}

function wpse_250923_print_js() {
    print '<script type="text/javascript">/*JS goes here*/</script>' . PHP_EOL;
}

Hope that helps. Happy New Year in advance!

1
  • You're welcome. Best wishes!
    – Ismail
    Commented Jan 1, 2017 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.