Skip to content

Instantly share code, notes, and snippets.

@amElnagdy
Created October 3, 2019 14:16
Show Gist options
  • Save amElnagdy/914ade0d1a01af3c92687cbd89e3adf7 to your computer and use it in GitHub Desktop.
Save amElnagdy/914ade0d1a01af3c92687cbd89e3adf7 to your computer and use it in GitHub Desktop.
<?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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment