WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Enqueueing Scripts for Theme Admin Pages (11 posts)

  1. Greg Priday
    Member
    Posted 2 years ago #

    According to Make WordPress Themes all themes must use the action admin_enqueue_script-appearance_page_$menu_slug to enqueue scripts and styles for theme pages in the admin.

    For some reason, I can't get this to work. I've created an admin page with the slug origin-styles. I've added an action for it using add_action('admin_enqueue_script-appearance_page_origin-styles','origin_enqueue_function') but origin_enqueue_function is never called.

    I did a full regex search of wp-admin (WordPress 3.4 Beta 4) and the only place an action is called with the string "admin_enqueue_script" in it is on line 68 of admin-header.php. To test, I added the following line to admin-header.php.

    do_action("admin_enqueue_script-$hook_suffix");

    As expected, origin_enqueue_function started being called after I added this.

    Am I missing something here, or is using the action admin_enqueue_scripts still the only way to enqueue scripts for a theme admin page?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Double check the slug on your theme options page. That's thrown me a few time because I've used admin_print_styles-appearance_page_theme_option instead of admin_print_styles-appearance_page_theme_options (missing 's').

  3. Greg Priday
    Member
    Posted 2 years ago #

    I've checked for typos and the like. I think everything is fine. As far as I can tell, the issue is just that the "admin_enqueue_script(s)-$hook_suffix" action is never called. When I added that manually to admin-head.php, everything worked as expected.

    The only solution I can think of is to just use "admin_print_scripts-$hook_suffix" instead, but I thought using this hook for enqueueing scripts was discouraged.

  4. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    admin_enqueue_script != admin_enqueue_scripts

    The actual hook name is the latter; your example above uses the former.

    Thanks for catching that typo; I've fixed it on the page you linked.

  5. Greg Priday
    Member
    Posted 2 years ago #

    Hi Chip, I actually thought that might be the issue so I did test it with admin_enqueue_scripts_$hook_name, but it still didn't work. To test the issue I created a simple theme with the following functions.php

    function hooktest_admin_menu(){
    	add_theme_page('Test Page', 'Test Page', 'edit_theme_options', 'hooktest', 'hooktest_admin');
    }
    add_action('admin_menu', 'hooktest_admin_menu');
    
    function hooktest_admin(){
    	print 'This is the hooktest page';
    }
    
    function hooktest_admin_enqueue(){
    	error_log('hooktest_admin_enqueue');
    }
    add_action('admin_enqueue_scripts-appearance_page_hooktest', 'hooktest_admin_enqueue');
    
    function hooktest_admin_print_scripts(){
    	error_log('hooktest_admin_print_scripts');
    }
    add_action('admin_print_scripts-appearance_page_hooktest', 'hooktest_admin_enqueue');

    According to this, hooktest_admin_enqueue() is NOT being called, while hooktest_admin_print_scripts() is being called when I view the hooktest admin page.

    I also can't find any evidence of a do_action that would possibly call "admin_enqueue_scripts-$hook_prefix" (or "admin_enqueue_scripts-appearance_page_$menu_slug") anywhere in WordPress 3.4 Beta 4 or WordPress 3.3.

    My last submission to the theme directory was rejected for (among other factors) not using admin_enqueue_scripts-appearance_page_$menu_slug - that's the only reason I'm trying to get to figure this one out.

  6. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    If admin_print_scripts-$hook_suffix works, feel free to use it!

  7. Greg Priday
    Member
    Posted 2 years ago #

    Thanks Chip, I will do!

  8. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Also, try this?

    function hooktest_enqueue_scripts( $hook_suffix ) {
        if ( 'appearance_page_hooktest' == $hook_suffix ) {
            wp_enqueue_script( ... );
        }
    }
    add_action( 'admin_enqueue_scripts', 'hooktest_enqueue_scripts' );

    The actual call is:

    do_action('admin_enqueue_scripts', $hook_suffix);

    So, you should be able to pass the $hook_suffix as an argument into your callback.

    Either one is entirely acceptable. The point of the guideline is to ensure that a Theme's admin scripts only get enqueued on the Theme's own admin settings page.

  9. Greg Priday
    Member
    Posted 2 years ago #

    I was originally using that method and it was working perfectly. According to my theme submission ticket it was one of the issues I needed to fix though.

    I'll stick to that method and try let the reviewer know that you said it's acceptable.

  10. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    No problem. I found your ticket, and left a comment regarding your original method.

    For future reference, if your question involves the Theme Review guidelines, or a specific Theme review comment, you'll get much quicker, more specific, and more effective responses if you post comments in the review ticket. You're also welcome to post to the theme-reviewers mail list.

    In a case such as this, don't be shy about asking the reviewer why your method wasn't acceptable. We absolutely can, do, and will make mistakes. :)

  11. Greg Priday
    Member
    Posted 2 years ago #

    Thanks for all your help Chip! I appreciate all the hard work you and the review team are putting in.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.