-
-
Save bitumin/d6c8b361642bf35c89f51fc76f65fc7d to your computer and use it in GitHub Desktop.
How to Hook in WordPress Metadata | http://www.ibenic.com/hook-wordpress-metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Should this data be added? | |
apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); | |
// Do something before we add the data | |
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); | |
// Do Something after the data has been added | |
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( "add_user_meta", "notify_on_nickname", 20, 3 ); | |
add_action( "added_user_meta", "change_not_allowed_nickname", 20, 4 ); | |
/** | |
* Let's notify our admin on nickname change | |
* @param int $object_id Object ID. | |
* @param string $meta_key Meta key. | |
* @param mixed $meta_value Meta value. | |
*/ | |
function notify_on_nickname( $object_id, $meta_key, $_meta_value ) { | |
if( 'nickname' == $meta_key && $_meta_value == 'admin' ) { | |
$user = get_userdata( $object_id ); | |
wp_mail( get_option('admin_email'), 'A user trying to add admin nickname', 'The user ' . $user->user_login . ' tried to change the nickname to "admin"' ); | |
} | |
} | |
/** | |
* Let's change the nickname | |
* @param int $mid Meta ID | |
* @param int $object_id Object ID. | |
* @param string $meta_key Meta key. | |
* @param mixed $meta_value Meta value. | |
*/ | |
function change_not_allowed_nickname( $mid, $object_id, $meta_key, $_meta_value ) { | |
if( 'nickname' == $meta_key && $_meta_value == 'admin' ) { | |
update_user_meta( $object_id, $meta_key, 'adminwannabe' ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'add_user_metadata', 'check_to_add_user_data', 20, 5 ); | |
/** | |
* Check if the current user can have such data | |
* @param null|bool $check Whether to allow adding metadata for the given type. | |
* @param int $object_id Object ID. | |
* @param string $meta_key Meta key. | |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. | |
* @param bool $unique Whether the specified meta key should be unique | |
* for the object. Optional. Default false. | |
*/ | |
function check_to_add_user_data( $check, $object_id, $meta_key, $meta_value, $unique ) { | |
// If $check != null, then this won't be added | |
if( ! current_user_can( 'have_this_saved' ) && get_current_user_id() === $object_id ) { | |
$check = false; | |
} | |
return $check; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Should this data be added? | |
apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); | |
// Do something before we add the data | |
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); | |
// An action added for post meta in the core | |
do_action( 'delete_postmeta', $meta_ids ); | |
// Do Something after the data has been added | |
do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); | |
// An action added for post meta in the core | |
do_action( 'deleted_postmeta', $meta_ids ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'delete_user_metadata', 'block_deleting_all_nicknames', 20, 5 ); | |
/** | |
* We don't want to delete all user nicknames at once | |
*/ | |
function block_deleting_all_nicknames( $check, $object_id, $meta_key, $meta_value, $delete_all ) { | |
if( 'nickname' === $meta_key && $delete_all ) { | |
return false; | |
} | |
return $check; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'get_user_metadata', 'retrieve_user_nickname', 20, 4 ); | |
/** | |
* Return random nickname | |
*/ | |
function retrieve_user_nickname( $check, $object_id, $meta_key, $single ) { | |
if( 'nickname' == $meta_key ) { | |
return get_random_nickname(); | |
} | |
return $check; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example of an infinite loop. AVOID | |
*/ | |
add_action( 'updated_user_meta', 'change_user_nickname', 20, 4 ); | |
function change_user_nickname( $meta_id, $object_id, $meta_key, $_meta_value ) { | |
// Possible Infinite Loop! | |
update_user_meta( $object_id, $meta_key, 'new_value_something' ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Should this data be added? | |
apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); | |
// Do something before we add the data | |
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); | |
// An action added for post meta in the core | |
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); | |
// Do Something after the data has been added | |
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); | |
// An action added for post meta in the core | |
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment