Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bitumin
Forked from igorbenic/add.php
Created July 8, 2019 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitumin/d6c8b361642bf35c89f51fc76f65fc7d to your computer and use it in GitHub Desktop.
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
<?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 );
<?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' );
}
}
<?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;
}
<?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 );
<?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;
}
<?php
apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
<?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;
}
<?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' );
}
<?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