<?
// Check:
// 1. If you are editing post, CPT, or page
// 2. If post type IS NOT SET
if( 'post.php' == basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']) && !isset($_GET['post_type']) ) {
// get post ID
$postid = $_GET['post'];
// check the template file name
if ('my_template.php' == get_page_template_slug($postid) ) {
// add your metabox here
add_action( 'add_meta_boxes', 'my_metabox' );
}
}
I don’t remember why I was checking post type, not post ID, but you can change
!isset($_GET['post_type'])
to check if post ID is set:
isset($_GET['post'])
Note: meta box will be available only after you save your post (page) using appropriate template.
Related Posts:
- How to show a custom meta box on the “Quick Edit” screen?
- How to move page template files like page-{slug}.php to a sub-directory?
- Reset positions of metaboxes in admin
- How to move page templates to custom folder?
- Create “File-less” Page Template in Functions.php
- Is it possible to manipulate the list of page templates?
- Adding items to page template dropdown on Page Edit Screen
- Adding Content Areas to Custom Page Template
- Theme elements not translating
- Trouble with conditional tags
- Why is save_post triggered even when I havent saved the post
- Pagination & get_pages?
- Returning Variables back into a template
- Page attribute template dropdown not displayed even the syntax is correct
- Do I really need the div class entry?
- Appending a value to metabox before and after saving
- Page template across themes
- How do I obtain the post content via a custom meta box inside the editor?
- More than one meta field in a single meta box?
- wp_editor() usage in custom meta box
- Where can I access my custom page template?
- Is there a need for nonce with Post Metabox?
- WordPress page templates in a directory
- Disable front-page.php template
- Can I show all the template files that are being used on my site?
- Implement content-nosidebar.php / content-leftsidebar.php
- get_page_template returning nothing
- Multiple Page Templates & CSS
- Highest number of WordPress Custom Page Templates?
- Single Page theme [closed]
- Why is the Page Template dropdown menu using the wrong file name?
- How to add “Template” option in page attributes panel?
- Custom Field created in add_meta_boxes reappearing itself again in the default Custom Metabox
- How to create a custom page(not a template) in a theme
- Template selection area is not visible on page editor sidebar in wordpress twentty sixteen default theme
- Move default page templates to sub directory
- Custom page template with custom fields
- how to setup custom content structures
- How can I conditionally show different home page templates based on whether or not the user is logged in?
- Load sidebar template just once to prevent multiple animations
- Where WordPress Stores The Custom Fields Values
- Need help with adding templates (archives and sitemap) to WordPress child theme
- How to create first post, fifth post full width, rest in three columns and so on
- Wpautop in wp_editor not working
- Issue On Displaying Pages with Post Name Permalink
- Why in the static pages of my theme are showing these information?
- How to set Post meta-box defaults based on the choices made by user in Customizer?
- Modify available templates (in dropdown)
- What is the best way to build home pages with a lot of sections for distributable themes [closed]
- Templates dropdown not appearing using _s theme (underscores)
- Where to put custom page templates in theme?
- Load different single.php templates according to post layout selection
- How to add pages in wordpress using codes?
- Injecting pre-defined text from custom page template to editor
- Output richtext metabox value
- Custom theme template files
- Is it possible to have a Theme with built-in physical page files?
- How to use shortcode inside of shortcode in theme
- Trouble in creating a custom template on twentyeleven theme
- show_option_none not working in meta box
- Custom search template is showing 404 when adding query string
- ACF get_sub_field ALT TAG in the repeater doesn’t show
- A Reviews Page is Showing root Index.php instead of Template-Page
- No templates for condition page.php Elementor
- Show Yoast SEO meta tags in Custom WordPress Templates (Theme)
- How to show metabox on page if it is using a template
- Template for front page (latest posts)
- Find callback function of custom meta box
- Group several custom global page templates in sub-folder
- Problem with Displaying Custom Theme Page’s Content
- How to show children pages as array
- Different stylesheet for different pages not working fully
- Create a variable with string, array or multiple values
- Access meta box checked value in another file
- Create new sidebars trough admin post/page metabox
- Why WordPress gets old templates code instead of the last updated ones
- Can you create a custom page with content in the center already defined?
- Get all Custom Post Types
- Adding Metabox Value Using the content filter
- Support Multiple featured images
- Add custom field to existing meta box?
- Paginated WP_Query doesn’t return 404’s, even when posts don’t exist
- Loop inside page template not working
- How can i display a 4 diferent themplate for the archive page
- How to split the site’s layout without damaging this layout? [closed]
- DIV containing iframe disappears below break point of 992 in Oxygen Builder
- Is there a way to have WordPress autodetect page templates in the page-templates directory and any sub-folders?
- What is the point of using the front-page.php template? [closed]
- How to automatically set a Template Page Name next to a page in menu screen such as WooCommerce pages, front page, or posts page in wordpress?
- For best DB performance should I serialize all theme options together or by type?
- Save Widget State Between Theme Swaps?
- Getting php_network_getaddress error : No such host is known?
- Combining CSS files into a single cached one
- How to set a page as homepage in stead of the newspages?
- Get the amount of posts on a given page
- Second menu has the first menu ID
- If I remove Category base from my URLs, how difficult will rollback be?
- Is there a list of default generated Gutenberg block CSS? Unable to align video blocks
- On wordpress, how do i require specific dimensions from user uploads only
- How to Remove Sidebar when get_sidebar() is not get called in page?