Custom Fields
Re-Envisioned
Support General Using Custom Attributes from rwmb_before_save_post action

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

  • Creator
    Topic
  • #8914

    pluginoven
    Participant

    OK, I give up. I need a point in the right direction here.

    First: I very much love this plugin and all the extentinos. Every project I get to use this, I get a little more impressed. Well done! Now, on to my Issue…

    I am attempting to use a custom attribute to control how exactly the meta data is saved. Specifically, I assign a attribute called mod_lock that I want to use from within the rwmb_before_save_post action. To make this work I need both the post_id and the $field array.

    Custom Attribute is defined like-a-so:

    'fields' => array(
                   array(
                       'name'  => __('Favorite Monkey', 'monkey-monk'),
                       'id'    => 'fav_monkey',
                       'type'  => 'text',
                        'attributes' => array(
                            'mod_lock'  => true,
                        ),
                   ),
                ),

    Ultimately I would like to handle the savinging of the metadata something like:

    function monkey_mod_save( $new, $old, $post_id, $field ) {
    	$pre = '';
    	if(!empty($field['attributes']['mod_lock'])){
    		$pre = 'pending_';
    	}
    
    	if($new !== $old){
    		$storage->update( $post_id, $pre.$field['id'], $new);
    	}
    
    	if( empty($new) ){
    		$storage->delete( $post_id, $pre.$field['id'] );
    	}
    }

    But this is the ‘save’ function… so I focused on the pre_save action, however all I have access to there is the $post_id and of course the $_POST data… but none of the delicious $field attributes.

    I need your help, Obi-Anh!

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

    pluginoven
    Participant

    Update!

    A little deeper and some progress.
    I have been able to extend a certain field type like so:

    if ( class_exists( 'RWMB_Field' ) ) {
    	class RWMB_Monkey_Field extends RWMB_Text_Field {
    		public static function save( $new, $old, $post_id, $field ) {
    			$pre = '';
    				if(!empty($field['attributes']['mod_lock'])){
    					$pre = 'pending_';
    				}
    
    				if($new !== $old){
    					$storage->update( $post_id, $pre.$field['id'], $new);
    				}
    
    				if( empty($new) ){
    					$storage->delete( $post_id, $pre.$field['id'] );
    				}
    		}
    	}
    }

    However this would require extending every single field type that is in use. Is there not a way to extend the base RWMB_Field class to use a special save function?

    Any clue is greatly appreciated…

    #8925

    pluginoven
    Participant

    At this point it seems the only way currently to ‘filter’ the save process using a fields custom attributes would be to create a series of new custom fields types. These new ‘extended’ field types would only serve to overwrite the static save method as shown above. There must be a better way to do this.

    #8926

    Anh Tran
    Keymaster

    Hello,

    I’ve just added rwmb_after_save_field hook, that can help you ease the pain 🙂

    Here is the code using it:

    add_action( 'rwmb_after_save_field', function ( $null, $field, $new, $old, $post_id ) {
    	$pre = '';
    	if ( ! empty( $field['attributes']['mod_lock'] ) ) {
    		$pre = 'pending_';
    	}
    	$storage = $field['storage'];
    
    	if ( $new !== $old ) {
    		$storage->update( $post_id, $pre . $field['id'], $new );
    	}
    
    	if ( empty( $new ) ) {
    		$storage->delete( $post_id, $pre . $field['id'] );
    	}
    }, 10, 5 );
    #8927

    pluginoven
    Participant

    HA! I was actually just in the process of modifying filter.php in a similar way.
    Does it make sense to also add a rwmb_before_save_field?
    It looks like the values would have already been saved to the default meta_keys in the dB by the time this filter is triggered.

    Regardless, thank you! I was sure I had missed something.

    #8929

    pluginoven
    Participant

    Anh, that worked perfectly! Thank you, thank you, thank you. You can close this issue as resolved. But please don’t forget to add this to the documentation… it will save the next person some grey hairs :D.

    #8943

    Anh Tran
    Keymaster

    Yes, the documentation is updated 🙂

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

The topic ‘Using Custom Attributes from rwmb_before_save_post action’ is closed to new replies.