How to trigger an action when a specific option on the settings page is changed

Support MB Settings Page How to trigger an action when a specific option on the settings page is changedResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #35243
    RodrigoRodrigo
    Participant

    Hi Long,

    I want to trigger some code after an option in my settings page changes. I know that in general I can trigger any code when an option change using pre_update_option_{$option} (see below).

    But how do I use this filter with a custom field in a settings page created with Meta Box?

    
    function check_option_change( $new_value, $old_value, $option ) {
    
        if ( $new_value !== $old_value && ! empty( $new_value ) ) {
    
        	// I want to trigger some code here.
        }
    
        return $new_value;
    }
    
    add_filter( 'pre_update_option_my-settings', 'check_option_change', 10, 3 );
    

    Thank you.

    #35252
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the filter rwmb_{$field_id}_value to modify the value of a specific field.
    Or the action rwmb_{$field_id}_after_save_field to do something after saving a specific field.

    Read more on the documentation
    https://docs.metabox.io/filters/#rwmb_field_type_value
    https://docs.metabox.io/actions/#rwmb_after_save_field

    #35263
    RodrigoRodrigo
    Participant

    Thanks, that will do it.

    #35280
    RodrigoRodrigo
    Participant

    I think I found a bug in both rwmb_{$field_id}_after_save_field and rwmb_{$field_type}_after_save_field actions.

    The below code does not work with either action, but if I use rwmb_after_save_field instead, the my_other_field_id value updates just fine.

    
    add_action( 'rwmb_my_field_id_after_save_field', function( $null, $field, $new_value, $old_value ) {
    
        if ( $new_value !== $old_value ) :
    
            rwmb_set_meta( MY_OPTIONS, 'my_other_field_id', date('y-m-d h:i:s'), [ 'object_type' => 'setting' ] );
    
        endif;
    }, 99, 4 );
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.