Default Meta Values not working

Support General Default Meta Values not working

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #8637
    tsqueztsquez
    Participant

    Hi 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 header

    Here 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?

    #8643
    Anh TranAnh Tran
    Keymaster

    Hello,

    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.

    #8649
    tsqueztsquez
    Participant

    Do 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?

    #8659
    Anh TranAnh Tran
    Keymaster

    You 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' );
    #8663
    tsqueztsquez
    Participant

    So what exactly will this do?

    #8669
    Anh TranAnh Tran
    Keymaster

    It gets the stored valued in the post meta with the helper function rwmb_meta. If the value is empty, then get the default value from std.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.