As I’ve already wrote in question comments, at the very beginning of creating the new post or page first choose a template and save as draft. Meta box will appear after save if right template is used.
<?php
add_action( 'add_meta_boxes', 'my_add_meta_box' );
function my_add_meta_box() {
// get template file name
$template_basename = wp_basename( get_page_template() );
// check if right template is used
if('my-template.php' == $template_basename) {
add_meta_box(
'my_metabox',
__('Metabox name'),
'my_display_metabox',
'page',
'normal',
'high'
);
}
}
function my_display_metabox($post) {
wp_nonce_field( 'my_metabox', 'my_metabox_noncename' );
// meta box HTML here
}
Related Posts:
- Custom meta box shown when template is chosen [duplicate]
- Choosing a default page tempate (Classic => Gutenberg)
- How to access global $wp_meta_boxes variable on front-end?
- Global Variable vs Local Variable
- Hide Page Title with Post Meta
- Add metabox to pages that use custom template in WordPress
- Create more Meta Boxes as needed
- How to HIDE everything in PUBLISH metabox except Move to Trash & PUBLISH button
- Removing panels (meta boxes) in the Block Editor
- WordPress SEO by Yoast: Hide Meta Boxes in Posts for Non-admins
- How to make open/closed and hidden/shown metaboxes status saved on a per-post basis?
- How To Remove The “+ Add New Category” Link From A Category Metabox
- How can I create a taxonomy meta-box with search suggestions but no new terms input?
- How to Add Reminders/Notes to New Post Meta Boxes
- How to reorder meta box position?
- How do I position meta_box on post edit screen after the title?
- Custom Meta Boxes: multiple fields within a repeatable field
- get registered metaboxes by post type or post ID
- Remove metabox from specific page template in admin
- Is there a filter/action to add content to WP admin metaboxes?
- Attach Files Metabox
- How do I create a meta box for dates?
- Add a meta description to home page?
- Display list of tags as drop down menu or radio buttons in a meta box?
- Set height of the categories meta box in post dashboard
- Get image captions for images on gallery post format metabox
- How do I migrate meta boxes from the Classic Editor to the “side area” in Gutenberg Editor?
- Adding Metaboxes – so much code is there a shorter DRY way?
- I need a Simple image uploader with a simple screen. WP default has too many options.
- Show or hide custom meta box not work with Gutenberg
- TinyMCE with custom buttons on a meta box
- How do i hide the auto-generated custom field that’s created when update_post_meta() is called?
- How to search by metadata using REST API
- Alternative to esc_textarea
- Automatically add a tag according to custom metadata
- How to add categories to page editor?
- Having trouble getting my meta data to save
- Undefined ‘post_type’ error on Add new page
- Display meta box on front end
- Show how many images are attached to a post/page on compose page
- metabox wordpress show in frontend
- Metabox context for page post type
- How to use MarkDown in a custom textarea field?
- How to use get_post_custom function on the blog page?
- Multiplicate entry on update_post_meta
- Remove ShareDaddy meta box from Pages only
- How to modify Publish metabox?
- On click hide group of metabox, similar to screen options checkbox?
- How to make a stand-alone button to the post edit view
- Controlling Meta Box Height
- Show Custom Post Type meta boxes only on Page Edit
- Custom Meta box only returns most recent value on page
- wp_editor loses data, perhaps an html_entity_decode problem?
- How to list all tags as radio buttons in a meta box?
- how can i do metabox [closed]
- How to add meta box for image upload using WordPress media uploader?
- How to position user meta data field
- Display Content if Meta Checkbox is checked?
- add_post_meta() & update_post_meta()
- MetaBox with Editor instead of textarea – html not saved [closed]
- Use meta boxes inside an archive page template for Woocommerce
- Why do Metabox use Nonces?
- conditionally echo in meta box data loop
- Meta box does not save or update
- Meta Box multi checkbox in template
- Show button if meta box has content, else hide
- remove a single post_meta
- Checkbox in post not saving value
- Custom loop attached to link
- post meta – problem : copy the same meta for all the articles
- ACF – Hiding metaboxes through screen settings doesn’t work
- Remove Username and just show Name in Author Metabox in backend
- how can I make this metabox area
- Metabox not saving values
- How to determine wether a post gets just created
- Load meta box inside meta box.. is this possible?
- Update Post Meta in Front End with a form
- Metabox with checkbox is not working true!
- Featured Gallery to custom post type
- Displaying a Metabox linked to post settings for all users
- Save datapicker date into wordpress admin
- Can’t get meta values to save
- Unable to save multiple images in wordpress
- Custom WP Meta Box – Use data from within the post screen inside meta box
- How to show select_advanced content instead of ID?
- Error when adding Meta Boxes, but only when adding 3 with the same callback
- Custom meta box using OOP way doesn’t save data
- Custom meta box repeated field
- A correct hook for saving meta boxes data
- update_post_meta does not work
- Hooking into the Meta Boxes (Name/Web Address/Description) on the ‘Add New Link’ Page
- when saveing $meta_box i get Undefined index error
- Adding a Custom ‘Now’ button under ‘Published on’ header to return current date and time
- Adding meta boxes to custom post type
- Select Options Meta Data is Not Updating in Edit Meta Box
- Meta boxes not showing up
- Metabox doesn’t retain values
- How do I create a Meta box that can be repeated? (with an image section and text area)
- Need help, Passing variables with get_post_meta
- My meta box don’t want to save value