Custom Fields
Re-Envisioned
Support General Programmatically modify field value prior to displaying in admin?

This topic contains 4 replies, has 2 voices, and was last updated by  Denny 1 year ago.

  • Creator
    Topic
  • #11287
    Resolved
    Denny
    Participant

    I’d like to use metabox to create our own simplified set of settings pages. Some of the fields would control standard WP settings, others would be for setting options on our custom themes.

    Setting the values in a metabox and then replicating them out to the master setting doesn’t look to be a problem, but I was curious if there’s a way to modify the data my custom fields return within the editing/admin interface on the backend.

    My goal would be for the standard WP setting to serve as the master record, so when we load up the custom settings page the fields would pull the values from the default WP setting. That way if something was modified programmatically by another plugin or somewhere outside of metabox it would still reflect the current value in the custom settings pages we create.

    I suspect one of the https://docs.metabox.io/filters/ may help with this but was curious if anyone has experience or full examples before I start goofing around aimlessly 🙂

    Thx!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #11306

    Anh Tran
    Keymaster

    Hi Denny,

    Looks like it can be done with rwmb_field_meta filter (https://docs.metabox.io/filters/#rwmb_field_meta). This filter changes the value before showing to users, e.g. it changes the value get from the database. It might be what you want.

    Cheers,
    Anh

    #11308

    Denny
    Participant

    Thanks Anh, that’s the one I’ve been looking at and experimenting with.

    Do you know the proper way to use it?

    If I wanted to create a field that could be used for the site title (ID: site_name), for example, I was trying the following without success:

    apply_filters( 'rwmb_field_meta', 'Title Here', 'site_name', false );
    apply_filters( 'rwmb_site_name_field_meta', 'Title Here');

    I also tried inserting 'Title Here' into an array per the docs but didn’t work for me either.

    Any thoughts are much appreciated!

    #11311

    Anh Tran
    Keymaster

    Hi Denny,

    The code should be like this:

    add_filter( 'rwmb_site_name_field_meta', 'your_filter_function', 10, 3 );
    function your_filter_function( $meta, $field, $saved ) {
        return 'Title here';
    }

    or

    add_filter( 'rwmb_field_meta', 'your_filter_function', 10, 3 );
    function your_filter_function( $meta, $field, $saved ) {
        if ( 'site_name' != $field['id'] ) {
            return $meta;
        }
        return 'Title here';
    }
    #11312

    Denny
    Participant

    Success! Thanks Anh, you rock 🙂

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

You must be logged in to reply to this topic.