providing access to post_id or post inside functions.php

admin2025-06-02  0

I'm trying to add metaboxes to the bottom of any post which is created from a specific post template. ie I don't want metaboxes on all my posts, just certain posts.

At the point when I decided to add the actions to draw and save metabox data, if I had access to the post I could look to see what template it uses and either continue to draw the metaboxes or quit.

add_action( 'load-post.php', 'post_metabox_setup');

function post_metabox_setup() {
  //decide whether to add draw and save actions here
}

How do I access either $post or $post_id in my 'post_metabox_setup function'. I've tried just doing this :

add_action( 'load-post.php', 'post_metabox_setup');

function post_metabox_setup($post_id) {
  //but here $post_id an empty string when the post loads in the editor
}

Does anyone know how to do this?

I'm trying to add metaboxes to the bottom of any post which is created from a specific post template. ie I don't want metaboxes on all my posts, just certain posts.

At the point when I decided to add the actions to draw and save metabox data, if I had access to the post I could look to see what template it uses and either continue to draw the metaboxes or quit.

add_action( 'load-post.php', 'post_metabox_setup');

function post_metabox_setup() {
  //decide whether to add draw and save actions here
}

How do I access either $post or $post_id in my 'post_metabox_setup function'. I've tried just doing this :

add_action( 'load-post.php', 'post_metabox_setup');

function post_metabox_setup($post_id) {
  //but here $post_id an empty string when the post loads in the editor
}

Does anyone know how to do this?

Share Improve this question asked Mar 10, 2019 at 12:20 RichardRichard 1835 bronze badges 5
  • 1 Why don't you use the standard add_meta_boxes hook which gives you the proper post object via the second argument? – Sally CJ Commented Mar 10, 2019 at 12:39
  • @SallyCJ I didn't know it existed! I'm new to wordpress. All the examples of adding metaboxes use load-post. I'll try it. Thanks. – Richard Commented Mar 10, 2019 at 12:55
  • Ah, okay. :) But that's the hook you should be using for registering custom meta boxes. Or you can also use add_meta_boxes_{$post_type} where the first (and the only) argument is the post object. – Sally CJ Commented Mar 10, 2019 at 13:15
  • @SallyCJ While I have you, do you know how to add an image picker metabox.. So when creating blocks you can add wp.editor.MediaUpload to the 'InspectorControls'. Can I somehow use MediaUpload as a metabox do you know? – Richard Commented Mar 10, 2019 at 13:21
  • I don't think so.. but you can use the wp.media from within a standard meta box (non block). – Sally CJ Commented Mar 10, 2019 at 15:52
Add a comment  | 

1 Answer 1

Reset to default 1

When you add an action via add_action(), the variables that are available in the callback function are determined by the specific hook you're using. The load-post.php hook does not pass any values through to the callback function.

However, when adding meta boxes the correct hook to use is add_meta_boxes, and that hook provides 2 values to the callback function $post_type and $post.

So if you want to conditionally add a meta box based on the post's page template you can use this $post variable with get_page_template_slug():

add_action( 'add_meta_boxes', 'prefix_post_metabox_setup', 10, 2 ); // The 2 here is required to get both arguments.

function prefix_post_metabox_setup( $post_type, $post ) {
    if ( 'my-template.php' == get_page_template_slug( $post ) ) {
        add_meta_box(); // etc.
    }
}

Just be aware that this means that your meta boxes won't appear until after the page/post is saved with the new template. In the block editor (née Gutenberg) this might require a manual refresh.

Also, it's best practice to prefix your functions with something unique to your project. post_metabox_setup is too generic a name.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748823867a314023.html

最新回复(0)