If the “Default Template” option is selected from the Template dropdown, then WordPress will set the value of the _wp_page_template
meta to default
and not page.php
, so that’s most likely the reason why your meta box is not showing/added.
So I would use if ( in_array( $pageTemplate, array( 'default', 'page.php' ) ) )
instead of if ( $pageTemplate == 'page.php' )
.
Additionally, I would also use the $post
variable that’s available as the second parameter for the meta box function:
add_action('add_meta_boxes', 'page_add_meta_boxes', 10, 2); // require the 2nd parameter
function page_add_meta_boxes($post_type, $post) { // and define it in the function head
//global $post; // <- no need to do this
// your code
}
Update
If the meta value is empty (''
), then the template will be the default, e.g. page.php
for the page
post type.
So you could use if ( ! $pageTemplate || in_array( $pageTemplate, array( 'default', 'page.php' ) ) )
instead of the one I suggested above.
Or actually, you could use get_page_template_slug()
which is much easier. 🙂 Example based on your updated code:
function page_add_meta_boxes($post_type, $post) {
$pageTemplate = get_page_template_slug( $post );
if(! $pageTemplate || 'page.php' === $pageTemplate) {
add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
}
}