I need to add fields to a metabox based on whether the user is on the Admin Add/Edit Post screen rather than the Admin Add/Edit Page screen?

I've tried using get_current_screen() but due to the framework I am using for my options, this isn't available during the hook that I need to use.

Are there any other WordPress globals or functions that can be used to detect this?


I believe you want something like this:


function function_name( $hook ) {
    global $post;

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'post' === $post->post_type ) {
            // Do stuff

Script is hooked via:

add_action( 'admin_enqueue_scripts', array( $this, 'function_name' ) );

Is the metabox you are targeting created by a plugin? You will need to locate the appropriate hook or filter. There is likely an opportunity to filter the metabox markup before output.

  • I'll give that a shot. This might be a dumb question but is $hook a global variable or a placeholder ? Thanks. – Phill Healey Mar 1 '16 at 10:10
  • 1
    Not a dumb question at all. I pasted part of a larger script from a recent project. In my case, I'm hooked to admin_enqueue_scripts. How are you calling your code? See edit above. – jdm2112 Mar 1 '16 at 14:57
  • Unfortunately it appears that the function is loaded too early to get the $post global too. I get 'Trying to get property of non-object' on 'post' === $post->post_type . – Phill Healey Mar 1 '16 at 19:03
  • How is the metabox you wish to modify being created? – jdm2112 Mar 2 '16 at 16:20
  • I'm using the titan framework for creating the MetaBoxes. – Phill Healey Mar 9 '16 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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