Default Value - pre propulate - filter ?

Support General Default Value - pre propulate - filter ?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #4113
    SinusiteSinusite
    Participant

    Hi !

    I was wondering how i could prefill the custom fields in a new post.
    i know i can use the 'std' parameter but i would like to make it conditionnal and dynamic.

    For example in a new post, be able to get metadata of another custom post type which the POST_ID would be in a hidden field or a parameter in the URL.

    Something like http://www.wpbeginner.com/wp-tutorials/how-to-add-default-content-in-your-wordpress-post-editor/ but for custom fields.

    May be with https://metabox.io/docs/filters/#section-rwmb_normalize_field ?

    What is the best syntax or way to do this ?

    Thank You for your help !

    #4119
    Anh TranAnh 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
    SinusiteSinusite
    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
    SinusiteSinusite
    Participant

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

    #4154
    Anh TranAnh 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
    SinusiteSinusite
    Participant

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

    #4217
    Anh TranAnh Tran
    Keymaster

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

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