Custom Fields
Re-Envisioned
Support General Default Value – pre propulate – filter ?

This topic contains 6 replies, has 2 voices, and was last updated by  Anh Tran 3 years ago.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #4119

    Anh Tran
    Keymaster

    Hi,

    I think normalize filter or field_meta filter both works. However, using normalize filter seems to be better.

    But why don’t you just use a condition check when defined meta boxes? I think it’s faster. Something like this:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $field = array(
            'id' => 'test',
            'type' => 'text',
            'std' => 'Default value',
        );
        if ( is_admin() ) {
            $post_id = isset( $_GET['post'] ) ? $_GET['post'] : ( isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : false );
            if ( 1 == $post_id ) {
                $field['std'] = 'Another default value';
            }
        }
        $meta_boxes[] = array(
            // ...
            'fields' => array(
                $field,
                // ...
            ),
        );
        return $meta_boxes;
    } );

    Or you can filter directly to rwmb_meta_boxes to change the std value of a field:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        if ( is_admin() ) {
            $post_id = isset( $_GET['post'] ) ? $_GET['post'] : ( isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : false );
            if ( 1 == $post_id ) {
                $meta_boxes['fields'][0]['std'] = 'Another default value'; // First field
            }
        }
        return $meta_boxes;
    }, 9999 ); // High priority to make sure this function run last
    #4140

    Sinusite
    Participant

    Hmmm thank you. My context is a little bit more complex…

    I am using add_filter(‘wp_get_object_terms’ but it’s not easy to perform query sometimes.

    I’ll try the second choice you gave me – the problem with this one is you have to care about the fields order.

    Thank you!

    #4141

    Sinusite
    Participant

    It would be great to have $meta_boxes[‘fields’][‘field_id‘][‘std’]

    #4154

    Anh Tran
    Keymaster

    There’s nothing stop you doing that. Simply add keys to fields param, like this:

    ’fields’ => array(
        ‘phone’ => array(
            ‘id’ => ‘phone’,
            ’type’ => ’text’,
            ’name’ => ‘Phone’,
        ),
    )
    #4200

    Sinusite
    Participant

    Great !
    But is it working with Taxonomy fields ?
    (Please see this issue)

    #4217

    Anh Tran
    Keymaster

    It should. The taxonomy field needs to work as normal fields. I will check and fix it on Github.

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.