Create membership number for existing/new users - wordpress

I have a membership site with 4000+ users currently on it.
I am needing to give both new/existing users an automatic ID number.
its very similar to this: Create Unique ID for user
However, this only works for new users, I need it to work for the current users too. So for instance, user id 3421 would get membership number 3421.
This is what I have so far: (pretty much all from above link)
function fb_add_custom_user_profile_fields( $user ) { ?>
<h3><?php _e('Extra Profile Information', 'Avada'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="memnumber"><?php _e('Membership Number', 'Avada'); ?>
</label>
</th>
<td>
<input type="text" name="memnumber" id="memnumber" value="<?php echo esc_attr( get_the_author_meta( 'memnumber', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'memnumber', $_POST['memnumber'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );
add_action( 'user_register', 'assignuserid');
function assignuserid($user_id) {
global $wpdb;
$latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='memnumber' order by meta_value DESC limit 1;");
update_user_meta( $user_id, 'memnumber', $latestid +1 );
}
The solution would need to be safe for the current users.
Any advice would be appreciated.

from the docs, the update_user_meta function returns the 'Meta ID if the key didn't exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.'
So something like
<?php
$new_user_num = $_POST['memnumber'];
// Assume existing user memnumber update to their id
$res = update_usermeta( $user_id, 'memnumber', $user_id);
if (! $res) { // New user, memnumber set already or failures
$create_res = update_usermeta( $user_id, 'memnumber', $new_user_num);
if (! $create_res)
return 1;
}
return 0;
?>

Related

Custom fields for profile page using plugin are not created/updated

I am creating a plugin that creates custom fields on the author profile page.
-It creates custom fields on the user profile page
-We can save it for every user.
What is happening now:
-Creates field and it can be seen in the front end of the author profile page
-On saving the fields do not save.
Observation: I saw the custom meta key and data is not there is the user meta table. So I am thinking we are able to see fields in the front end but database meta key values are not created.
The same code works perfectly when added to functions.php. Please let me if I am missing anything or if any other hooks need to be fired when adding as a plugin.
<?php
class authorFieldsPlugin {
function __construct() {
//Uses author.php from plugin of not there in theme.
add_filter( 'template_include', 'wpa_155871_template_loader' );
function wpa_155871_template_loader( $template ) {
$file = '';
if ( is_author() ) {
$file = 'author.php'; // the name of your custom template
$find[] = $file;
$find[] = 'plugin-name/' . $file; // name of folder it could be in, in user's theme
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
if ( ! $template ) {
// if not found in theme, will use your plugin version
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/' . $file;
}
}
return $template;
}
function memberpage_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','memberpage_rewrite');
//Custom profile fields
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Author Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span class="description"><?php _e("Please enter Author's Information from plugin."); ?></span>
</td>
</tr>
<tr>
<th><label for="author_title"><?php _e("Author Title"); ?></label></th>
<td>
<textarea name="author_title" id="author_title" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author_title', $user->ID ) ); ?></textarea><br />
<span class="author_title"><?php _e("Please enter author_title."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'author', $_POST['author'] );
}
}
}
$authorFieldsPlugin = new authorFieldsPlugin(); ?>

How to add value to a custom user field when an hook is triggered/fired

How do i add value to a custom text field in user profile when an hook is triggered/fired. I have been able to add a custom field called example using the following code
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Example Section", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="example"><?php _e("Example"); ?></label></th>
<td>
<input type="text" name="example" id="example" value="<?php echo esc_attr( get_the_author_meta( 'example', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("This field should add YES when to field when a hook is triggered ad empty if hook not triggered."); ?></span>
</td>
</tr>
</table>
<?php }
And save the input using the following code
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'example', $_POST['example'] );
}
i would like this newly generated custom field to be populated with YES when the user_register hook is triggered (i.e. when a user registers, YES should be added to the field and updated)
So that i can get the YES value and use it to display content dynamically. Something like
$user = wp_get_current_user();
if ( get_the_author_meta( 'example', $user->ID ) = 'YES') {
//Show this page
} else {
return 'Your are not allowed to view this page';
}
How can i achieve this? Thanks
This should work:
function add_yes_to_field ( $user_id ) {
update_user_meta( $user_id, 'winner', 'YES' );
}
add_action( 'user_register', 'add_yes_to_field');
Regarding get_the_author_meta(), I don't think you can actually get user meta data with it, because it calls get_userdata(). You can use get_user_meta() instead.
https://developer.wordpress.org/reference/functions/get_the_author_meta/

Custom User Fields on Registration

I'm working on a custom plugin for a client and I need to add some custom user fields. I've searched through the Codex but couldn't come across the answer.
I basically need to add some new rows to the users table in the MySQL and then add some extra fields during the registration. I'm sure there are other plugins out there that allow you to add custom user fields but I'd like to incorporate it directly into my plugin.
How can I do it?
I've answered a similar Question at WordPress Answers: Checkboxes in registration form.
You need the action hooks register_form (to inject your input fields) and user_register (to process it). The rest of the code is just sample code to check the results in the pages Profile and User Edit.
// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );
// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
function signup_fields_wpse_87261() {
?>
<label>
<input type="checkbox" name="custom_feature_a" id="custom_feature_a" />
Enable feature A?
</label>
<br />
<label>
<input type="checkbox" name="custom_feature_b" id="custom_feature_b" />
Enable feature B?
</label>
<hr />
<?php
}
function handle_signup_wpse_87261( $user_id, $data = null )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
if ( $feat_a )
{
add_user_meta( $user_id, 'custom_feature_a', $feat_a );
}
if ( $feat_b )
{
add_user_meta( $user_id, 'custom_feature_b', $feat_b );
}
}
function user_field_wpse_87261( $user )
{
$feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
$feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
<h3><?php _e('Custom Fields'); ?></h3>
<table class="form-table">
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
checked( $feat_a, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature A?'); ?></span>
</label>
</td>
</tr>
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
checked( $feat_b, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature B?'); ?></span>
</label>
</td>
</tr>
</table>
<?php
}
function save_profile_fields_87261( $user_id )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
update_usermeta( $user_id, 'custom_feature_a', $feat_a );
update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}

Add multiple checkbox value to user meta box in wordpress

I found a post regarding the add extra field to the user profile page. After that i want to save the extra field to the usermeta table. Which i did by the following code.
add_action( 'personal_options_update', 'my_save_badge_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_badge_profile_fields' );
function my_save_badge_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'badge', $_POST['badge'] );
}
But now i want to store an array of value to the 'badge' name because i added multiple check box in user profile page in admin and my code for adding more check box in user profile page is written bellow.
add_action( 'show_user_profile', 'my_show_badge_profile_fields' );
add_action( 'edit_user_profile', 'my_show_badge_profile_fields' );
function my_show_badge_profile_fields( $user ) {
?> <h3>User Badges</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Badges:</label></th>
<?php
$args = array(
'post_type' => 'badge',
);
// The query itself
$sb_user_query = new WP_Query( $args );
// The loop
while ( $sb_user_query->have_posts() ) : $sb_user_query->the_post();
$badge_id = $sb_user_query->post->ID;
$badge_title = get_the_title();
$badge_image_small = get_the_post_thumbnail( $badge_id, array(16,16) );
?>
<td>
<?php echo $badge_image_small; ?> <input type="checkbox" value="<?php echo $badge_title; ?>" name="badge">
</td>
<?php
endwhile;
?>
</tr>
</table>
<?php
}
Now i want to save the data in the usermeta table in database like an array, just like the name in the check box is 'badge' and the value is an array like { 'a','b','c'} .

Add Custom ComboBox Filters to Admin User List in Wordpress

I want to add 2 combo box in Admin User List Panel. For example the first one will be a combo box with countries and the other combo will be User Age.
So I want to add these combos in order to filter the user list.
Could you please shed some light here?.
Thank you.
This is what i'm looking for:
add_action('restrict_manage_posts', 'my_restrict_manage_posts');
function my_restrict_manage_posts()
{
global $typenow;
if ($typenow == 'your_custom_post_type') {
$args = array(
'show_option_all' => "Show All Categories",
'taxonomy' => 'your_custom_taxonomy',
'name' => 'your_custom_taxonomy'
);
wp_dropdown_categories($args);
}
}
add_action('request', 'my_request');
function my_request($request)
{
if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type'] == 'your_custom_post_type') {
$request['term'] = get_term($request['your_custom_taxonomy'], 'your_custom_taxonomy')->name;
}
return $request;
}
You can add your own fields to a user add/edit field.
this example shows how to add a address input field, if you get this working try switching it with the dropdown's that you need. If that's what you mean by 'combobox'
function fb_add_custom_user_profile_fields( $user ) {
?>
<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="address"><?php _e('Address', 'your_textdomain'); ?>
</label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter your address.', 'your_textdomain'); ?></span>
</td>
</tr>
</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'address', $_POST['address'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

Resources