1

I'd like to insert a script and if possible some PHP for a custom post type. I'm looking for a hook to do this on the add new or edit screens. Any idea what I'd use for this? I tried "edit_post" but that didn't seem to work.

closed as unclear what you're asking by Nicolai, Pieter Goosen, s_ha_dum Oct 3 '14 at 15:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • possible duplicate of Load a script just to custom post type in admin – Howdy_McGee Sep 26 '14 at 18:29
  • That's helpful but it's not just scripts I want to load, but PHP as well if possible. – paper_robots Sep 26 '14 at 18:32
  • I did the same mistake as @Howdy_McGee with his duplicate suggestion with my answer. You probably should specify your question, but reading beforehand How do I ask a good question? won't hurt. – Nicolai Sep 26 '14 at 18:51
  • There are numerous hooks that will allow you to add PHP to a page, but without knowing where exactly you want to add the code your question can't really be answered with any specificity. – s_ha_dum Oct 3 '14 at 15:51
4

There is admin_enqueue_scripts():

admin_enqueue_scripts is the first action hooked into the admin scripts actions. This hook provides a single parameter, the $hook_suffix for the current admin page. And it's an action it can only be used to callback a specified function.

Usage like:

add_action( 'admin_enqueue_scripts', 'function_name' );

The codex page provides even an example on how to target a specific admin page:

function wpse162680_enqueue_script( $hook ) {
    $screen = get_current_screen();
    if ( $hook == 'post.php' && $screen->post_type != 'your-custom-posttype' ) {
        return;
    }
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'wpse162680_enqueue_script' );

Read Admin Screen Reference on how to target the right screen.


Note: About the other part of your question, maybe Post Status Transitions will help to there, but actually I'm not sure what you want to achieve.

  • This might work, is there a way to target a specific post type? – paper_robots Sep 26 '14 at 19:00
  • 1
    You can test the current screen id against the post type slug, e.g. if ( 'post_type_slug' == get_current_screen()->id ) { ... } – DrewAPicture Sep 26 '14 at 19:07
  • I adapted my answer to help you out, but note this is only for scripts too. – Nicolai Sep 26 '14 at 19:12
  • Or as @DrewAPicture said, btw DrewAPicture you're right about your edit, this means the codex page is wrong ;) Do you want to update the codex or should I? – Nicolai Sep 26 '14 at 19:14
  • 1
    Actually, I'm also updating the Code Reference article, which the Codex article will soon redirect to :) – DrewAPicture Sep 26 '14 at 19:41

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