Yahya Uddin
Yahya Uddin

Reputation: 28841

Adding a script to the edit & create admin pages for a custom post type

I wish to add a admin script to certain admin pages.

However, the link: https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts

only shows how to add scripts based on a hook. NOT the post type.

How can I add a script on the create and edit admin pages for a custom post type named product?

Thanks!

Upvotes: 2

Views: 784

Answers (1)

vard
vard

Reputation: 4136

Look on your URL in admin menu for your custom post types. They all look like that:

/wp-admin/edit.php?post_type=my_post_type

So you can easily make a condition to check on that GET parameter in admin_enqueue_scripts. If you need to target only certain pages, use the $hook parameter of admin_enqueue_scripts.

For pages that doesn't transport the post type, like the edit page, you can work with the classes on body tag. If you look into your source, you will noticed that <body> have the following class:

post-type-my_post_type

So in your JS, you can do:

if($('body').hasClass('post-type-my_post_type')) {
  // do stuff
}

Upvotes: 3

Related Questions