Using Custom Attributes from rwmb_before_save_post action
- This topic has 6 replies, 2 voices, and was last updated 7 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
March 22, 2018 at 6:39 PM #8914
pluginoven
ParticipantOK, 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 therwmb_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!
March 23, 2018 at 12:56 AM #8921pluginoven
ParticipantUpdate!
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...
March 23, 2018 at 2:07 PM #8925pluginoven
ParticipantAt 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.
March 23, 2018 at 2:49 PM #8926Anh Tran
KeymasterHello,
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 );
March 23, 2018 at 3:01 PM #8927pluginoven
ParticipantHA! 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.
March 23, 2018 at 3:20 PM #8929pluginoven
ParticipantAnh, 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.
March 24, 2018 at 10:54 AM #8943Anh Tran
KeymasterYes, the documentation is updated 🙂
-
AuthorPosts
- The topic ‘Using Custom Attributes from rwmb_before_save_post action’ is closed to new replies.