Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  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' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement