A simple example would be to get all users for a specific role, iterate over the returned results and apply the delete_user_meta
function for the given meta_key
. Stir and let simmer for a fraction of a second and all user meta for that key shall be gone.
function say_goodby_to_the_meta(){
$role = 'subscriber';
$users = get_users('role='.$role);
foreach ($users as $user) {
delete_user_meta($user->ID, 'ref_credit');
}
}
This example is void of any error checking or return response but at its core this will do what you what you need. Associate the function with the click of a button or conditionally call and run this function in plugin or theme files based upon your use-case.
Taking the above function one step further and associating it with the click of a button within the dashboard you can add this function to admin_init
hook.
add_action('admin_init', 'say_goodby_to_the_meta');
function say_goodby_to_the_meta(){
//check if admin, if not, lets get out of here - don't run remainder of function
if ( !current_user_can('activate_plugins') )
return;
//check that $_POST is not empty & that correct wp_nonce is supplied
//checl that $_POST['delete_credit'] is also set and passed with $_POST variables
if ( !empty($_POST)
&& check_admin_referer('delete_key','nonce_name')
&& isset( $_POST['delete_credit'] ) )
{
//set your role to apply this action to
$role = 'subscriber';
//get all users of this role (i.e. Subcribers)
$users = get_users('role='.$role);
//iterate of each user returned applying our delete_user_meta function
foreach ($users as $user) {
delete_user_meta($user->ID, 'ref_credit');
}
} else {
//return your error conditions here if you like or do further processing
echo "I say I don't even know you, I say that you're not authorized,\n
I don't understand you, so why do I judge your vars?\n
Armand Van Administrator";
}
}
This assume you are using a wp_nonce_field in your form and that your form also contains $_POST['delete_key']
as one of the submitted $_POST
vars, whether thats a hidden input or not. Calling check_admin_referrer
is a convenient way to check whether your request has originated from an administrative page in the dashboard.