Default Meta Values not working
- This topic has 5 replies, 2 voices, and was last updated 7 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
February 27, 2018 at 8:37 PM #8637
tsquez
ParticipantHi there,
Hope you had a great holiday.
I seem to be having a problem with default meta values not doing what they are supposed to do.
OK, so as an example let's say I have this metabox select field for the title of the post or page.
There are two options the user can chose from:
1. display title in the post (default, set with std)
2. display title in page headerHere is the code for that select field:
array ( 'id' => 'xxxxxx_page_header_title_position', 'name' => esc_html__('Post/Page Title Position','xxxxxx'), 'type' => 'select', 'column' => 'column-1', 'options' => array ( 'xxxxxx_default_position' => esc_html__('Display Title in Post','xxxxxx'), 'xxxxxx_page_header_position' => esc_html__('Display Title in Page Header','xxxxxx'), ), 'tooltip' => array( 'icon' => 'help', 'content' => esc_html__('Select where you would like to display the title. Default position is in the post/page or you can change the position to display in the Page Header.','xxxxxx'), 'position' => 'top', ), 'tab' => 'xxxxxx_featured_image_options_tab', 'std' => array ( 'xxxxxx_default_position', ), ),
(xxxxxx represents the name of the plugin, I changed it to add to this post)
Now in the theme I have the following:
<?php if ($xx_title_position === 'xxxxxx_default_position'): ?> display the title <?php endif; ?>
For some reason, once the metabox is activated the default option is not being set and the title disappears.
If I hit "update" in the editor, then go back to the post or page, the title will display
Trying to figure out why its not being set, how can I have it set automatically? Is it because it is an if statement?
February 28, 2018 at 8:54 AM #8643Anh Tran
KeymasterHello,
Do you mean that when getting the field value, if the meta box hasn't been saved yet, the
std
value is not returned?Well, if the meta box hasn't been saved yet, there's actually no values are saved in the post meta. Thus, the helper functions return empty string. Because the helper functions are just the wrap of
get_post_meta()
, which connects to post meta to get the saved value.I'd suggest you do a check to see if the title is empty first.
The reason we don't set the
std
when retrieving field value is it's nearly impossible to differentiate a saved empty string (if users really want to save an empty string) to non-saved field value.February 28, 2018 at 10:41 PM #8649tsquez
ParticipantDo you mean that when getting the field value, if the meta box hasn’t been saved yet, the std value is not returned?....yes that is what I mean.
So how can I get it to do what I want? Any idea?
March 1, 2018 at 5:26 PM #8659Anh Tran
KeymasterYou can create a helper function for that:
function your_helper( $field_id ) { $field = rwmb_get_field_settings( $field_id ); $value = rwmb_meta( $field_id ); return $value ? $value : $field['std']; } // In your template echo your_helper( 'your_title_field' );
March 1, 2018 at 10:37 PM #8663tsquez
ParticipantSo what exactly will this do?
March 2, 2018 at 8:20 AM #8669Anh Tran
KeymasterIt gets the stored valued in the post meta with the helper function
rwmb_meta
. If the value is empty, then get the default value fromstd
. -
AuthorPosts
- You must be logged in to reply to this topic.