If your custom page template filename is foobar.php, you can use get_post_meta():
global $post;
if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
// The current page has the foobar template assigned
// do something
}
Personally, I like calling this inside my add_meta_boxes_page callback, and wrapping it around the add_meta_box() call itself.
function wpse82477_add_meta_boxes_page() {
global $post;
if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
add_meta_box( $args );
}
}
add_action( 'add_meta_boxes_page', 'wpse82477_add_meta_boxes_page' );
You’ll just need to instruct users to save the page after assigning the template, so that the meta box appears.
Related Posts:
- How to enable custom fields for pages (if not a bad practice)?
- ‘Preview Changes’ for custom meta boxes?
- Using TinyMce with textareas in meta boxes on custom post types
- add meta box – custom field : which to choose?
- Add metabox to document tab in gutenberg
- Having different sidebar content for MANY pages?
- Add custom option to Standard Page Attributes Meta Box
- Add multiple images to a page sidebar
- Only show metabox when date-value in other metabox is over?
- Populating meta box with select-list of existing posts, and assigning it to custom post types
- Create meta boxes that don’t show in custom fields
- How To Create a Metabox of HTML Content with Instructions For Editors When Editing a Post or Page?
- Custom Page Template [closed]
- How to load php file for specific page in admin?
- How to set default metaboxes on user creation?
- Metabox nonce PHP notice
- wp_editor in add_meta_boxes does not show gallery
- Calling Specific Pages with wp query Part II
- How do I assign this filter to a variable? (Appending php & markup to the_content)
- Problem with adding exta field in metabox in custom post type
- Add input field to ‘Pages > Edit Page’ through functions.php
- How do I add custom_meta_box_id’s value?
- Metabox image upload and custom field
- My metabox class
- How to construct multiple meta boxes
- Loading scripts to the Post Edit page only
- Force hide custom field metaboxes
- How to Display Custom Meta Box only on Specific Page IDs
- Calling custom fields for pages (not posts)
- Metabox will not save
- How to check for specific meta box value on front end and output accordingly
- custom field not saved
- How to create a Custom Meta Box with Name/Value Admin User Input Fields?
- Create Multiple File Upload Metabox in WordPress
- Users Select inside custom metabox
- Custom Metabox Not Saving
- How to Display pages with a Custom Fields in one page
- Should custom meta boxes be able to output shortcodes the same as WordPress’ native post editor?
- Unsaved changes in metabox inputs not detected in the block editor
- Advanced Custom Fields dynamic update_field
- Get updated meta value after post update or published (custom post type) using hook
- Why is my Custom Meta Box Field Inputs NOT saving?
- custom-meta-box checkboxes from loop won’t save
- I need a “Choose from existing content” popin
- Outputing a metabox textarea and avoid line breaks inside li tags
- shortcode in a custom metabox
- Problem with saving large amount of data in postmeta/usermeta
- Move Title and the Content WYSIWYG editor position
- How can I hide custom field from users used for caching response from external api?
- How to use media upload on metabox post page without breaking TinyMCE?
- Displaying Meta-Box Data Properly
- Let’s Create Custom Field Template Documentation
- Can I save post meta programatically without setting metaboxes?
- Display a different image for each page with editor
- Meta box values are displayed on Custom Fields list. Is it possible to hide them?
- If metabox has content display content
- Custom meta boxes not saving
- HM CMB: Role Select
- Get meta field value of all catgories
- Interrogate a page within a loop to check template type or custom meta data (Pages vs Posts)
- How to store meta field values
- Make separate text boxes for separate WordPress Custom Fields
- Display the output from Custom User Fields from a WordPress user profile on a WordPress Custom Page
- Set class if a meta value is set within post archive
- WordPress Blocks, setAttributes not saving
- Using Customizer API vs Custom Meta Box for custom content
- non-unique #_ajax_nonce id in browser console
- Allow HTML in Custom Metabox area
- Metabox/Custom fields are not saving input data
- Custom field metabox not showing in back-end
- Issue on Creating Checkbox Field On Custom Post type
- WordPress admin area: select box with 12.000+ options
- How to display selected option from dropdown list in a metabox
- Save, update, get and sanitize post meta as HTML not plain
- Why does this javascript search function in the WordPress Admin not work?
- Create Meta boxes dynamically
- Load one page with parent and child pages
- Update custom table on metabox POST
- Save input form on custom field
- Page template is missing fields or is invalid when I updating the template
- Metabox value for post loop?
- how to modify a field’s size in a metabox : ROWS height, and scroll control?
- Get meta value when the page is a blog archive
- Custom Meta Boxes and Fields for WordPress: Change directory upload based on user-edit page
- Custom Field select list is truncated
- wp_postmeta are updated for only one page
- Meta Box Data added to header redirect
- How to use page metadata while using wp_list_pages()?
- Custom metaboxes not saving after switching themes
- Custom Metaboxes, multiple post selection
- added a meta box to post however when saving menu while debug on throws a warning
- Use Metabox to enter Post Title
- Include custom fields into the content of a regular page
- Get fields from metabox array
- Display custom field value into content with hyperlink
- Using CMB2 Plugin to create text_date metabox (datepicker) How do I save multiple dates? [closed]
- Dynamic background image used in css after selector
- How to add new Metadata options (Date, Author, etc.) for Posts?
- Saving multiple custom meta box fields
- Custom fields empty after refreshing page