[wp-trac] [WordPress Trac] #56718: register_post_meta not being initialized default value right away

WordPress Trac noreply at wordpress.org
Sun Oct 2 23:43:52 UTC 2022


#56718: register_post_meta not being initialized default value right away
--------------------------+-----------------------------
 Reporter:  kaimaniiii    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  6.0.2
 Severity:  critical      |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I am trying to utilize a custom post meta which I called
 isVisibleFeatureImage and then using ToggleControl WP Gutenberg Component
 to toggle by showing whether the feature image is on or not. The issue is
 that the default value isVisibleFeatureImage doesn't get the initialized
 value as true on my page.php file. I just get an empty string instead.

 What I had to do is going to the Gutenberg Block editor, use the
 ToggleControl to toggle the feature image that is not visible, update the
 page, and then ToggleControl to be visible again and update. Then it will
 work.

 Here is the code snippet below for using register_post_meta() function to
 register the feature image is visible or not:

 {{{#!php
 <?php

 add_action( 'rest_api_init',
 'autoload_register_meta_post_core_feature_image', 9999 );

 function autoload_register_meta_post_core_feature_image() {
     // Configuration setup to get all custom post types
     $args = [
         'public'   => true,
         '_builtin' => false
     ];

     $output = 'names'; // 'names' or 'objects' (default: 'names')
     $operator = 'and'; // 'and' or 'or' (default: 'and')

     $post_types = get_post_types( $args, $output, $operator );
     array_push($post_types, 'post', 'page');

     $meta_args = array(
         'type'         => 'boolean',
         'description'  => 'Meta key to ensure value is toggled or not for
 featureImage',
         'auth_callback' => function() {
             // ! Need to return true, otherwise we get status 403
 forbidden error to update the meta key.
             return true;
         },
         'single'        => true,
         'default'       => true, // set the ToggleControl WP Component
 default value to false
         'show_in_rest'  => true,
     );

     foreach ($post_types as $post_type) {
         register_post_meta( $post_type, 'isVisibleFeatureImage',
 $meta_args );
     }
 }
 }}}


 This is a simple code snippet for trying to check if the my custom post
 meta feature image is toggled or not:

 {{{#!php
 <?php
   global $post;

   $postId = $post->ID;
   $is_visible_feature_image = get_post_meta($postId,
 'isVisibleFeatureImage', true);

   var_dump(get_post_meta($is_visible_feature_image));
 }}}

 Is this some sort of bug???

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56718>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list