1

I'm using the following to enqueue a script in admin.

function add_admin_scripts( $hook ) {

    global $post;

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'custom' === $post->post_type ) {     
            wp_enqueue_script(  'myscript', get_stylesheet_directory_uri().'/js/custom.js' );
        }
    }
}
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );

But I want to enqueue when action=edit is in the URL parameter.

http://mysite/wp-themes/my-theme/wp-admin/post.php?post=185&action=edit

Is this possible?

1 Answer 1

0

There are a couple of ways to do this. The easiest way is to use $_GET:

if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && $_GET['action'] == 'edit' ) {
    // Enqueue your scripts here
}

You can also use the get_current_screen(); function instead of using a global variable.

2
  • Thanks! I tried get_current_screen() but it does not store the action parameter
    – CyberJ
    Commented Aug 18, 2017 at 13:17
  • You're welcome. I was talking about the URL base, as I mentioned "instead of global".
    – Johansson
    Commented Aug 18, 2017 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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