WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding javascript from plugin only on plugin page (4 posts)

  1. Hi,

    I have been trying to add a few javascript files to my plugin. One's that will only fire when on my plugin settings page.

    Here is what I have so far:

    function my_plugin_admin_init() {
        // what your plugin needs in its <head>
    	wp_register_script( 'jwl_sliding_script', plugins_url('js/jwl_admin_sliding.js', __FILE__) );
    }
    add_action( 'admin_init', 'my_plugin_admin_init' );
    
    function my_plugin_admin_styles() {
    	wp_enqueue_script( 'jwl_sliding_script' );
    }
    add_action('admin_print_styles-ultimate-tinymce', 'my_plugin_admin_styles');

    But I think I have something wrong, as the script is not doing what it's supposed to do.

    Am I on the right track here?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    When you add your plugin page do it like this:

    $page = add_submenu_page( ...);

    Now you can use $page in the "admin_print_styles-" action hook:

    add_action('admin_print_styles-' . $page, 'my_plugin_admin_styles');

    It's better explained here:
    https://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages

    or try it with get_current_screen():
    https://codex.wordpress.org/Function_Reference/get_current_screen

  3. Thank you, kind friend. That worked perfectly.

    I was under the assumption you could use the admin_print_styles in the following manner; with ultimate-tinymce being the admin settings page.

    admin_print_styles-ultimate-tinymce

    I don't fully understand how assigning it to a variable makes it work... but I'm certainly not going to argue :)

    If you see this, and have time, would you mind taking a look at this thread also?
    http://wordpress.org/support/topic/plugin-exportimport-options-settings?replies=1

    Thanks again, kindly!!

  4. Forgot to mark this as resolved... My bad :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.