skip to Main Content

How can i add to every new user who register this meta key…instead of updating all users to value 0 again..just to assign this meta key and value 0 to the new user who register

global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
if( $users ) {
  foreach ( $users as $user ) {
    update_user_meta( $user->ID, 'dospjeli_dug', 0 );
  }
}

2

Answers


  1. you can use hook for it. user_register hook will do it.

    add_action(‘user_register’,’addneumeta’, 10, 1);

    function addneumeta($user_id){
      update_user_meta( $user_id, 'dospjeli_dug', 0 );
    }
    
    Login or Signup to reply.
  2. If you want to add it to only new register user then according to documentation you need to use this function.

    add_user_meta( $user_id,  'dospjeli_dug',  0);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search