Need a filter before save field

Support General Need a filter before save field

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #9004
    pluginovenpluginoven
    Participant

    We now have a new after_save_field referenced in this post that has access to all the meta field attributes, new and old meta values. The problem I am having is that this filter is triggered after the field is saved. So any values that need to be adjusted (or not saved at all) have already been submitted to the database.

    Is there a way to include a before_save_field filter that would provide the same access (all field meta, including custom attributes, new and old values)?

    I have currently done a test:

    RWMB_Field::filter( 'before_save_field', null, $field, $new, $old, $post_id, $field );
    // Call defined method to save meta value, if there's no methods, call common one.
    RWMB_Field::call( $field, 'save', $new, $old, $post_id );
    RWMB_Field::filter( 'after_save_field', null, $field, $new, $old, $post_id, $field );

    And then added the following to my class:

    add_action( 'rwmb_after_save_field', array( $this, 'pi_moderated_save' ), 10, 5 );
    function pi_moderated_save ( $null, $field, $new, $old, $post_id ) {
    	if($field['id'] == 'truthiness' && !empty( $field['attributes']['mod_lock'] ) ){
    		$new = '';
    	}
    }

    But the $new variable is not being filtered.
    I have tried using add_filter instead of add_action and also return $new; with no success.

    #9005
    pluginovenpluginoven
    Participant

    It turns out the values can be easily filtered using the ´rwmb_value´ filter like so:

    add_filter( 'rwmb_value', array( $this, 'uncheck_thruthiness' ), 20, 3 );
    public function uncheck_thruthiness ( $new, $field, $old ) {
    	if($field['id'] == 'truthiness' && !empty( $field['attributes']['mod_lock'] ) ){
    		$new = '';
    	}
            return $new;
    }

    I have a different issue now... This can be marked as resolved. I'll open a new thread.

    #9014
    Anh TranAnh Tran
    Keymaster

    You're becoming a master of Meta Box's hooks 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Need a filter before save field’ is closed to new replies.