Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  userabuser Oct 29 '12 at 14:52
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.