How to create Unique User ID in WordPress - wordpress

I need to add a users from WordPress backend. So, I want to generate unique User IDs for them. But I can achieve it. Can somebody help me out on this.
Here is what I cam so far. It is better if I could append the WordPress default generated unique user id to end of my field.
// Customize the user fields
function custom_user_profile_fields($user) {
$rand = randomGen(1000,5000,1);
$auth_Token = '2021-ABCD-'.$rand[0];
?>
<h3>Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="srn">Student Registration Number</label></th>
<td>
<input type="text" class="regular-text" name="srn" value="<?php echo (get_option('srn') ? esc_attr( get_the_author_meta( 'srn', $user->ID ) ) : $auth_Token); ?>" id="srn" readonly /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id) {
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_usermeta($user_id, 'srn', $_POST['srn']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
When a user added I can't get the previously saved Index Number to the field. It's generating another and showing again. As I am not an expert for this, could anybody help me out with this?
Thanks in advance

You were on the right path to solve the problem, but you made some mistakes.
You can use the working code below:
Creation of fields
add_action( 'user_new_form', 'bks_add_student_reg_number_field' );
add_action( 'edit_user_profile', 'bks_add_student_reg_number_field' );
add_action( 'show_user_profile', 'bks_add_student_reg_number_field' );
function bks_add_student_reg_number_field( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="student_reg_number"><?php _e("Student Registration Number"); ?></label></th>
<td>
<?php if (is_object($user)) { ?>
<input type="text" name="student_reg_number" id="student_reg_number" value="<?php echo esc_attr( get_the_author_meta( 'student_reg_number', $user->ID )); ?>" class="regular-text" readonly disabled="disabled"/><br />
<?php } else { ?>
<input type="text" name="student_reg_number" id="student_reg_number" class="regular-text" /><br />
<span class="description"><?php _e("Please enter Student registration number."); ?></span>
<?php } ?>
</td>
</tr>
</table>
<?php }
This creates a field option at new user generation and edit form.
The field is only enabled when a new user is created. If the registered user has already have a value the value would be shown with no access to change it.
Save field value
add_action( 'user_register', 'bks_student_reg_number_field' );
function bks_student_reg_number_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if(!current_user_can('manage_options'))
return false;
$reg_value = get_user_meta($user_id, 'student_reg_number', true);
if (isset( $_POST['student_reg_number']) && $_POST['student_reg_number'] !== $reg_value) {
update_user_meta($user_id, 'student_reg_number', $_POST['student_reg_number'].'-'.$user_id);
}
}
Only user_register hook is used to prevent updation on profile edit.
You can see the user_id appended with a - infront of it in the field and the field is readonly and disabled.
If you want user to be able to edit the id after registering as well then. do the following.
Add profile_update hook. add_action( 'profile_update', 'bks_student_reg_number_field' ); as well so that the code runs when you update the user as well.
Remove readonly and disabled attribute from the form.
The code is TESTED and WORKS.

Related

Add exrtra user approve status filed in user profile, visible to admin only and send mail if checkbox is checked at first time in wordpress?

I want to give the functionality to admin to approve user status manually. I was able to give this functionality, but having some issue in this functionality. How can I hide this field from user's profile for user role. i.e. Field should be visible to admin only. If I hide this filed from user profile using jQuery then when user hits update profile button at that time value is updated. I want value to be same as per user selected (set by admin). I also want to send mail to user if checkbox is selected from uncheck to check, but currently it is sending mail every time. How can i solve out this issue?
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
$checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : '';
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="1" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
/* Update selected option **/
add_action( 'user_register', 'save_user_fields');
add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );
function save_user_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));
/* Send email to agent to notify about thier account is approved. **/
if (get_user_meta($user_id, 'artwork_approved', true)) {
// sent mail if checkbox is selected from unchecked to checked,and hide this filed from user profile only visible to admin.
}
}
What I understand from your question is, you have added a custom field on user profile which is a checkbox. You want that field to be editable only by admins and not by the users.
If this understanding is correct, you can check the user capabilities of current user while rendering the custom field on user profile page.
That way, if current user has administrator privileges only then the custom field will be rendered. If a non admin user is logged in then the custom field will not be rendered.
Also, in your function to add custom field, you are setting the value of checkbox to '1' irrespective of whether it is checked or not. I guess that is causing the email to be sent every time. You should set the value of checkbox based on your condition.
So your function to add custom field will become as follows.
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
if( !current_user_can( 'manage_options' ) ){
return;
}
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
if( isset($user->artwork_approved) && $user->artwork_approved ){
$checked = 'checked="checked"';
$value = '1';
}else{
$checked = null;
$value = null;
}
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="<?php echo $value; ?>" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
Hope this helps.

Add custom fields in custom taxonomy meta box in wordpress-3.5.2

Unable to add custom fields in custom taxonomy meta box in wordpress-3.5.2.
I have checked solution in various blogs but Unable to solved this problem. I am using wordpress-3.5.2
What I am trying is :-
// A callback function to add a custom field to our "adtag" taxonomy
add_action( 'adtag_edit_form_fields', 'adtag_callback_function', 10, 2);
// A callback function to save our extra taxonomy field(s)
add_action( 'edited_adtag', 'save_taxonomy_custom_fields', 10, 2 );
I have tried solution from below link:-
http://www.codehooligans.com/2010/07/07/custom-meta-for-new-taxonomies-in-wordpress-3-0/
http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies
http://www.wpbeginner.com/wp-tutorials/how-to-add-additional-custom-meta-fields-to-custom-taxonomies/
http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
Take a look at the Tax-meta-class developed to add extra fields to taxonomies: WordPress Taxonomies Extra Fields the easy way
1) Include the main class file
require_once("Tax-meta-class/Tax-meta-class.php");
2) Configure taxonomy custom fields
$config = array(
'id' => 'demo_meta_box',
'title' => 'Demo Meta Box',
'pages' => array('category'),
'context' => 'normal',
'fields' => array(),
'local_images' => false,
'use_with_theme' => false
);
3) Initiate your taxonomy custom fields
$my_meta = new Tax_Meta_Class($config);
4) Add fields
//text field
$my_meta->addText('text_field_id',array('name'=> 'My Text '));
//textarea field
$my_meta->addTextarea('textarea_field_id',array('name'=> 'My Textarea '));
5) Finish Taxonomy Extra fields Deceleration [important!]
$my_meta->Finish();
6) Getting Saved data
$saved_data = get_tax_meta($term_id,'text_field_id');
echo $saved_data;
To add a custom field to your custom taxonomy, add the following code to your theme's functions.php:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
Next, we'll create a callback function that we'll use to save our custom fields. Add the following code to your theme's functions.php:
// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ){
if ( isset( $_POST['term_meta'][$key] ) ){
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}
The code above will work "as is" for one or more custom taxonomies, no changes needed.
now let's associate these callback functions to the "edit" screen for our custom taxonomies. To do that, we'll use two of the WordPress action hooks that are available for each custom taxonomy that we create. Add the following code to your theme's functions.php:
// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
// Save the changes made on the "presenters" taxonomy, using our callback function
add_action( 'edited_presenters', 'save_taxonomy_custom_fields', 10, 2 );
To access a custom field added to your custom taxonomy
add the following code inside your custom taxonomy template (example, taxonomy-presenters.php), within the PHP block at the top:
// Get the custom fields based on the $presenter term ID
$presenter_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );
// Return the value for the "presenter_id" custom field
$presenter_data = get_userdata( $presenter_custom_fields[presenter_id] ); // Get their data
For this example to work, be sure that you have saved a value in the custom field for the term you are working with.
<?php
echo '<pre>';
print_r( $presenter_custom_fields );
echo '</pre>';
?>
I was able to follow the directions on http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies to create custom fields within a custom taxonomy.
It looks like you are not including the steps after adding the action. Make sure you are working in the functions.php file and that you include the html markup for how the custom field should appear. That is, this section from the SabraMedia instructions:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
The creation / edition of an existing taxonomy, whether customized or not, has two panels.
The first panel to create a new term for the taxonomy normally consisting of meta fields
Name
Slug
And the second panel to edit an existing term
To add custom fields to the taxonomy term creation panel, first panel, use:
<?php
// Key of your custom taxonomy goes here.
// Taxonomy key, must not exceed 32 characters.
$prefix_taxonomy = 'category';
/**
* This will add the custom meta field to the add new term page.
*
* #return void
*/
function wporg_prefix_add_meta_fields(){
?>
<div class="form-field term-meta-wrap">
<label for="term_meta[custom_term_meta]">
<?php esc_html_e( 'Example meta field', 'textdomain' ); ?>
</label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="" />
<p class="description">
<?php esc_html_e( 'Enter a value for this field.', 'textdomain' ); ?>
</p>
</div>
<?php
}
add_action( sprintf( '%s_add_form_fields', $prefix_taxonomy ), 'wporg_prefix_add_meta_fields' );
https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/
To add custom fields to the edit panel, second panel, use:
<?php
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
https://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies

BuddyPress Xprofile fields to display on "wordpress user edit admin panel"

I am building a buddypress/bbpress, website with xprofile registration fields. I am using New User Approve plugin to approve each users manually since we need to check information of each user before they get activated.
Problem here is, I am unable to check/view the xprofile field values in Wordpress user edit admin panel. All I have is the username change password, changing roles etc. I want the admin panel to display the extra information of the registered user so that I can check the info and approve . Anyone can help me out to solve this problem.
Might be similar to this.. haven't tried the code though....
Replace the key value 'xprofile_key_birthday' with actual xprofile keys in Buddypress DB.
Note: This code only displays the values on the edit screen and doesn't insert or update anything.
<?php
add_action( 'show_user_profile', 'showmy_extra_profile_fields' );
add_action( 'edit_user_profile', 'showmy_extra_profile_fields' );
function showmy_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label>xprofile_key_birthday</label></th>
<td>
<?php
if( function_exists( 'xprofile_get_field_data' ) ) {
$xprofile_value = xprofile_get_field_data('xprofile_key_birthday', $user->ID );
}
else {
$xprofile_value = '';
}
?>
<input type="text" name="xprofile_key_birthday" id="xprofile_key_birthday" value="<?php echo esc_attr( $xprofile_value ); ?>" class="regular-text" readonly />
</td>
</tr>
</table>
<?php
}
?>
I use the free Wangguard plugin on my BuddyPress/bbPress website. All I need to do is click on "Users" in the Wangguard menu on Wordpress dashboard sidebar and then click on "BP Profile" under the member username column. I can view and even edit the member profile from there. Hope it helps.

Wordpress to display custom USERMETA field on User Profile Dashboard?

I have added and been using a USERMETA field (called: user_custom_hash) from the wp_usermeta table. Means i've been Insert/Edit/Deleting the field from the code somewhere.
No needed to display it out before.
So now ..
how to DISPLAY this USERMETA field on the User Profile page of Wordpress Admin Dashboard? (I mean the "Edit User" page on the Admin Dashboard.)
For example:
Custom Hash Key: __239cc68ba2423ddf67dcfcd4aa2fa83b__
I mean, to display out at somewhere here:
Note:
Wordpress Version currently: 3.5.0
It's ok now as i got it by myself, like this:
add_action( 'show_user_profile', 'display_user_custom_hash' );
add_action( 'edit_user_profile', 'display_user_custom_hash' );
function display_user_custom_hash( $user ) { ?>
<h3>USERMETA Fields</h3>
<table class="form-table">
<tr>
<th><label>Custom Hash Key</label></th>
<td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_custom_hash', true ); ?>" class="regular-text" readonly=readonly /></td>
</tr>
</table>
<?php
}
Add this snippet at the bottom of functions.php file of my current Theme folder.
Note:After that entire code-above should be then, just the normal ?> for the entire file as normally as before.

wordpress template specific metaboxes?

i have built several diffrent templates for my WP site each calling diffrent metas.
ok, im looking for a way to add a meta-box that will only display if the correct page template is selected.
say i have a homepage.php template, this template calls some specific meta info that only the homepage uses, a call to action of example, so i need the meta-box to appear once the proper page template is selected and disappear if a different page template is selected.
any help is appreciated.
get_page_template will return the template you are looking at. You can switch on this to display the content you want.
ok i finally found a way around it guys and here it is:
<?php
function my_admin() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php'){
add_meta_box( 'product_info',
'Informacion de Producto',
'display_product_info_meta_box',
'ex_producto', 'normal', 'high'
);
}
}
function display_product_info_meta_box( $product_info ) {
$product_price = floatval( get_post_meta( $product_info->ID, 'product_price', true ) );
$product_code = esc_html( get_post_meta( $product_info->ID, 'product_code', true ) );
?>
<table>
<tr style="width: 100%">
<td >Precio Producto</td>
<td><input type="text" size="80" name="product_info_product_price" value="<?php echo $product_price; ?>" /></td>
</tr>
<tr style="width: 100%">
<td >Codigo de Producto</td>
<td><input type="text" size="80" name="product_info_product_code" value="<?php echo $product_code; ?>" /></td>
</tr>
</table>
<?php
}
add_action( 'admin_init', 'my_admin' );
?>
so... yeap i hope that dose someone out there some good :)

Resources