#56718 closed defect (bug) (worksforme)
register_post_meta not being initialized default value right away
Reported by: | kaimaniiii | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 5.5 |
Component: | Options, Meta APIs | Keywords: | dev-feedback |
Focuses: | Cc: |
Description
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 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 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???
Change History (5)
#1
follow-up:
↓ 2
@
2 years ago
- Component changed from General to Options, Meta APIs
- Keywords dev-feedback added
- Severity changed from critical to normal
- Version changed from 6.0.2 to 5.5
#2
in reply to:
↑ 1
;
follow-up:
↓ 3
@
2 years ago
Replying to spacedmonkey:
Thanks for your ticket @kaimaniiii.
However, I think the issue here is a very simply one.
Consider this line.
add_action( 'rest_api_init', 'autoload_register_meta_post_core_feature_image', 9999 );
This only registers the meta on the rest api init. If you try to load meta in another context like on the front end or in a rss, this meta would be registered. If you can it to simple be the following, it should fix the issue.
add_action( 'init', 'autoload_register_meta_post_core_feature_image', 9999 );
I will await your response, but otherwise I believe there is no issue here.
Oh wow! You are right! It seems like I didn't use the correct hook name! Thank you very much for your help!
Another question! How come is that the default value is returning as "true", but when I am toggling off and back on again, then I get the value "1" instead?
Is there some sort of way I can make it always return "true" instead of "1"?
#3
in reply to:
↑ 2
@
15 months ago
Replying to kaimaniiii:
Is there some sort of way I can make it always return "true" instead of "1"?
You could do something like this
(bool) get_post_meta($postId, 'isVisibleFeatureImage', true);
Thanks for your ticket @kaimaniiii.
However, I think the issue here is a very simply one.
Consider this line.
add_action( 'rest_api_init', 'autoload_register_meta_post_core_feature_image', 9999 );
This only registers the meta on the rest api init. If you try to load meta in another context like on the front end or in a rss, this meta would be registered. If you can it to simple be the following, it should fix the issue.
add_action( 'init', 'autoload_register_meta_post_core_feature_image', 9999 );
I will await your response, but otherwise I believe there is no issue here.