Want to check if a user exists by a given id in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 | function user_id_exists($user){ global $wpdb; $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = %d", $user)); if($count == 1){ return true; }else{ return false; } } |
Use this in your template:
1 2 3 4 5 | if(user_id_exists(1)){ //it does exists } else { //it doesn't } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.