SHARE
TWEET

Untitled

a guest Oct 21st, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Display Custom Id
  4. Description: This plugin assigns a random custom id to each registered user, you can show your custom id by inserting a short code into your posts.
  5. Author: Mahamad Sayed
  6. */
  7. // generste random customer ID
  8. function generate_custom_id (){
  9.     $new_id = rand(1, 1000);
  10.     return $new_id;
  11. }
  12.  
  13. // Assign generated ID to user meta
  14. function assign_custom_id() {
  15.     $custom_id = generate_custom_id();
  16.     $meta_key= 'custom_id';
  17.  
  18.     // We need to get all users
  19.     $wp_user_query = new WP_User_Query( array( 'role' => '' ) );
  20.     $users = $wp_user_query->get_results();
  21.     foreach ($users as $user)
  22.     {
  23.         add_user_meta( $user->id, $meta_key, $custom_id, true );
  24.     }
  25. }
  26. register_activation_hook( __FILE__, 'assign_custom_id' );
  27.  
  28. function retrieve_custom_id(){
  29.     $result = get_user_meta(get_current_user_id(), 'custom_id', true);
  30.     echo "<h5>Your custom Id is: user-" . $result . "</h5>";
  31. }
  32. add_shortcode( 'getcustomid', 'retrieve_custom_id' );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top