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.

Hello wordpress users,

I'm stuck with a problem while running 2 self made Wordpress plugins. I'll use the following code :

define('PLUGIN_URL', plugin_dir_url( __FILE__ ));
add_action( 'admin_enqueue_scripts', 'plugin_load_js_and_css' );

function plugin_load_js_and_css() {
        wp_register_style( 'plugin.css', PLUGIN_URL . 'plugin.css', array());
        wp_enqueue_style( 'plugin.css');

        wp_register_script( 'plugin.js', PLUGIN_URL . 'plugin.js', array('jquery'));
        wp_enqueue_script( 'plugin.js' );
    }
}

But it's loading this stylesheet everywhere in the admin panel. Now I found this in the codex:

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugins_url('/myscript.js', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

But this code is not working for my.. Does anyone have a another option? Or maybe know why it's not working for me?

share|improve this question
    
And where your plugin runs? Does it have a custom page? Show the code for this part. –  brasofilo Mar 29 '13 at 17:34
    
Nor working doesn't really tell us anything. Is the script loading on edit.php? –  Chris_O Mar 29 '13 at 17:35

1 Answer 1

When you register a plugin option page you get a hook from the registration function:

$hook = add_menu_page(
    'T5 Demo',        // page title
    'T5 Demo',        // menu title
    'manage_options', // capability
    't5-demo',        // menu slug
    'my_render_page'  // callback function
);

Use this hook to enqueue the scripts and styles:

add_action( "admin_print_styles-$hook", "my_enqueue_style" );
add_action( "admin_print_scripts-$hook", "my_enqueue_script" );

See my plugin T5 Admin Menu Demo for an example.

Do not define a constant PLUGIN_URL. You will run into collisions with other code.

share|improve this answer

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.