[theme-reviewers] Admin Script Enqueueing

Chip Bennett chip at chipbennett.net
Tue Sep 27 13:26:45 UTC 2011


Good morning, reviewers,

I thought this morning would be a good time to start a discussion on the
guidelines/best-practices regarding enqueueing arbitrary scripts in the
WordPress Admin area.

As you know, current review generally focuses on ensuring that scripts
intended for the front-end are not enqueued in the admin area, generally by
wrapping the wp_enqueue_script() calls in a if ( ! is_admin() ) conditional.
However: what about Themes that *do* need to enqueue scripts in the admin
area, e.g. for their Theme settings page?

I would like to get your thoughts on requiring the use of the
admin_print_scripts-{$hook_suffix}<http://adambrown.info/p/wp_hooks/hook/admin_print_scripts-%7B$hook_suffix%7D>for
such scripts.

For example, assuming the Theme uses add_theme_page(), for which the fourth
argument is $menu_slug, this hook would be constructed as follows:

Base: admin_print_scripts-
Context: appearance_page_
Page: $menu_slug


Or, put all together:

admin_print_scripts-appearance_page_$menu_slug


For example, in Oenology, my $menu_slug is "oenology-settings":

admin_print_scripts-appearance_page_oenology-settings


So, the entire thing would look like:

function oenology_enqueue_admin_scripts() {
    wp_enqueue_script( 'some-arbitary-script', get_template_directory_uri()
. '/js/script-name.js' );
}
add_action( 'admin_print_scripts-appearance_page_oenology-settings',
'oenology_enqueue_admin_scripts' );


I would like to propose this implementation as a "best practice" as a
minimum; however, given the known issues regarding interaction between Theme
and Plugin scripts in the Admin area (Plugin developers - rightly so -
generally don't appreciate Themes outputting their scripts on every single
admin page, because it often results in breakage of the Plugin if it relies
on certain scripts being loaded/not loaded), I believe this is worthy of
consideration as a requirement. But for now, I just want to get the
discussion started.

So: what are your thoughts?

Chip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wordpress.org/pipermail/theme-reviewers/attachments/20110927/1810144a/attachment.htm>


More information about the theme-reviewers mailing list