0

I'm having an issue with one of my meta box. I want the meta box displayed only on the default page template editing page.php.

The meta box doesn't show up on the editing page.

add_action('add_meta_boxes', 'page_add_meta_boxes', 1);
function page_add_meta_boxes() {
  global $post;
  if(!empty($post)) {
    $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
    if($pageTemplate == 'page.php') {
      add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
    }
  }
}

Thank you.

EDIT

Code updated following the comments of @SallyCJ

add_action('add_meta_boxes', 'page_add_meta_boxes', 10, 2);
function page_add_meta_boxes($post_type, $post) {
  if(!empty($post)) {
   $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
   if(in_array( $pageTemplate, array( 'default', 'page.php' ) ) ) {
     add_meta_box( 'page-repeatable-fields', 'Index', 'page_repeatable_meta_box_display', 'page', 'normal', 'low');
   }
 }
}

After checking with var_dump( $pageTemplate ); the result is:

"String(0) "" "

Note: If use a custom template page to display the metabox, it works.

1

2 Answers 2

2

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');
 }
}
6
  • Thank you, unfortunately the meta box still doesn't show up in the editor. Jul 17, 2021 at 14:45
  • In that case, then do a error_log( $pageTemplate ); (or just as a quick debugging, try var_dump( $pageTemplate );) before your conditional, and see what the meta value is. Also, it might be better if you edit your question and include your updated code?
    – Sally CJ
    Jul 17, 2021 at 14:54
  • And if the block editor is enabled, you may also need to set __block_editor_compatible_meta_box to true, although it actually defaults to true.
    – Sally CJ
    Jul 17, 2021 at 15:09
  • I updated the question with your code. Jul 17, 2021 at 15:33
  • 1
    Sorry for the late answer. Just tried the code in the update and It works like a charm, thanks a lot for your help. Jul 19, 2021 at 13:52
0

try this code i guess it should work correctly

add_action('add_meta_boxes', 'page_add_meta_boxes');
function page_add_meta_boxes(){
    global $post;
    if(!empty($post)):
        $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
        $pageTemplate = ($pageTemplate == "page.php" || empty($pageTemplate) || $pageTemplate == " " || $pageTemplate == "default" )?true:false;
        if($pageTemplate):
            add_meta_box(
                    'page-repeatable-fields',
                    'Index',
                    'page_repeatable_meta_box_display',
                    'page',
                    'normal',
                    'low'
                )
        endif;
    endif;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.