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?
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.
add_meta_boxes
hook which gives you the proper post object via the second argument? – Sally CJ Commented Mar 10, 2019 at 12:39add_meta_boxes_{$post_type}
where the first (and the only) argument is the post object. – Sally CJ Commented Mar 10, 2019 at 13:15wp.media
from within a standard meta box (non block). – Sally CJ Commented Mar 10, 2019 at 15:52