4

I'd like to create a button for admins to be able to delete from table wp_usermeta -> key: 'ref_credit' for all users.

Is there simple resource friendly way to do this? does wp have built in function for this?

I found this: http://codex.wordpress.org/Function_Reference/delete_user_meta

But this is only for single user.

1
  • Where are you intended to run this action from? A custom WordPress admin screen? from within a meta box attached to a particular custom post, from the front end? From the user management screen in the dashboard? Just for determining the ideal place to hook your callback onto which action.
    – Adam
    Commented Oct 29, 2012 at 14:52

2 Answers 2

10

This is a super old post, for all you future readers, you can use the delete_metadata() function to achieve this with a lot less DB overhead:

delete_metadata(
    'user',        // the meta type
    0,             // this doesn't actually matter in this call
    'my_meta_key', // the meta key to be removed everywhere
    '',            // this also doesn't actually matter in this call
    true           // tells the function "yes, please remove them all"
);
2
  • 1
    Thanks! This should be the accepted answer. Commented Jul 10, 2020 at 11:33
  • Thanks a lot, this solution works like a charm! Commented Apr 9, 2021 at 18:03
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.