Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I adapt this solution to work with my plugin? (Please see the link).

I enqueue my css and js script as follows:

function my_plugin_init() {
 wp_enqueue_script('my_plugin_script', plugins_url('js/the_filepath.js', __FILE__), array('jquery'));
 wp_enqueue_style( 'my_plugin_css', plugins_url( '/css/the_filepath.css', __FILE__ ) );
}

add_action('init', 'my_plugin_init');

I tried putting this in the theme's functions.php, but it didn't work:

function remove_my_plugin_extras() {
    remove_action('init', 'my_plugin_init');
}

if( !is_page('My_Page') ) {
    add_action('wp_head', 'remove_my_plugin_extras');
}

The script and css still loaded. How do I adapt the solution in this case?

share|improve this question
    
Is this admin or front-end script/style enqueueing? If admin enqueueing: what is your add_*_page() call to add your settings page? –  Chip Bennett Jan 24 '12 at 17:17
    
Hi @Chip Bennett, this is front-end script/style enqueueing. –  Laxmidi Jan 24 '12 at 18:48
    
@Laxmidi Any progress on that question? –  kaiser Sep 17 '13 at 12:39
add comment

1 Answer

The right hooks

    // Use both for scripts & styles *)
    wp_enqueue_scripts // (for the frontend)
    login_enqueue_scripts // (for the login screen)
    admin_enqueue_scripts // (for the admin dashboard)

*) Read this article @wpdevel.

Further reading in the Codex about the three hooks

Admin page hooks

When registering a admin (sub)menu page, you can save the result, which is the page hook, into a variable:

function register_admin_page()
{
    // Register (sub)menu page
    $hook_suffix = add_submenu_page( $your_args_array );
    // Add styles to hook
    add_action( "load-{$hook_suffix}", 'callback_function' );
}
// Use one of those hooks to register the page
add_action( 'admin_menu', 'register_admin_page' );
add_action( 'user_admin_menu', 'register_admin_page' );
add_action( 'network_admin_menu', 'register_admin_page' );

// Register your styles & scripts in here
function callback_function()
{
    // do stuff
}

Admin globally available variables to check against

The following

global $hook_suffix, $typenow, $pagenow, $self, $parent_file, $submenu_file

are available on a wide range of admin pages. Use them to check if you're on the requested page that you need and only then do stuff.

// Example
if ( 'edit.php' !== $GLOBALS['pagenow'] )
    return;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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