Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Display Custom Id
- 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.
- Author: Mahamad Sayed
- */
- // generste random customer ID
- function generate_custom_id (){
- $new_id = rand(1, 1000);
- return $new_id;
- }
- // Assign generated ID to user meta
- function assign_custom_id() {
- $custom_id = generate_custom_id();
- $meta_key= 'custom_id';
- // We need to get all users
- $wp_user_query = new WP_User_Query( array( 'role' => '' ) );
- $users = $wp_user_query->get_results();
- foreach ($users as $user)
- {
- add_user_meta( $user->id, $meta_key, $custom_id, true );
- }
- }
- register_activation_hook( __FILE__, 'assign_custom_id' );
- function retrieve_custom_id(){
- $result = get_user_meta(get_current_user_id(), 'custom_id', true);
- echo "<h5>Your custom Id is: user-" . $result . "</h5>";
- }
- add_shortcode( 'getcustomid', 'retrieve_custom_id' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement