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

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(); ?>

Related

Create membership number for existing/new users

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;
?>

Add Image Upload to Woocommerce Registration Form And Display it in the Admin Backend

Hi I am trying to add an upload image field to the woocommerce register form and then get this to display in the admin area in the backend. I am using the code below which give the image upload on register form but having trouble trying to get it to display within the admin area.
//// ADD IMAGE UPLOAD TOO REGSITER
// Add enctype to form to allow file upload
function AddEnctypeCustomRegistrationForms() {
echo 'enctype="multipart/form-data"';
}
add_action('woocommerce_register_form_tag','AddEnctypeCustomRegistrationForms');
// Add file input html to register form
add_action('woocommerce_register_form','AddImageField');
function AddImageField() {
?>
<p class="form-row validate-required" id="pro_image" data-priority="">
<label for="pro_image" class="">Upload Picture ID<abbr class="required" title="required">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type='file' name='pro_image' accept='image/*,.pdf' required>
</span>
</p>
<?php
}
// Validate new fields
function ValidateImageField( $errors, $username, $email ) {
if ( isset( $_POST['pro_image'] ) && empty( $_POST['pro_image'] ) ) {
$errors->add('pro_image_error', __( 'Please provide a valid image', 'woocommerce' ) );
}
return $errors;
}
add_filter('woocommerce_registration_errors','ValidateImageField',10,3 );
// Save new field
function SaveImageField( $customer_id ) {
if ( isset( $_FILES['pro_image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_id= media_handle_upload('pro_image', 0 );
if ( is_wp_error( $attach_id) ) {
update_user_meta( $customer_id,'pro_image', $_FILES['pro_image'] . ": " . $attach_id->get_error_message() );
} else {
update_user_meta( $customer_id,'pro_image', $attach_id);
}
}
}
add_action('user_register','SaveImageField',1);
I have tried a few different things but not having any luck, here is one of them.
function add_SaveImageField( $user ) {
?>
<h3><?php _e('ID','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="SaveImageField_field"><?php _e( 'ID', 'woocommerce' ); ?></label></th>
<td><input type="image" name="SaveImageField_field" value="<?php echo esc_attr( get_the_author_meta( 'pro_image', $user->ID )); ?>" class="pro_image" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_SaveImageField', 10, 1 );
add_action( 'edit_user_profile', 'add_SaveImageField', 10, 1 );
Any help would be much apricated
Thanks
You have to do some changes to your code
// Save new field
function SaveImageField( $customer_id ) {
if ( isset( $_FILES['pro_image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
//Code changes
foreach ($_FILES as $file => $array){
if ($file == 'pro_image') {
if (!empty($array['name'])) {
$attach_id = media_handle_upload($file,$new_post);
update_user_meta( $customer_id, $file, $attach_id );
}
}
}
//End Code changes
}
}
add_action('user_register','SaveImageField',1);
function add_SaveImageField( $user ) {
//Code changes
$thumbnail_id = get_the_author_meta( 'pro_image', $user->ID );
$image_url = '';
if (!empty($thumbnail_id) && $thumbnail_id) {
$image_size = 'full'; // (thumbnail, medium, large, full or custom size)
$image_attachment_data_arr = wp_get_attachment_image_src( $thumbnail_id, $image_size );
$image_url = $image_attachment_data_arr[0];
}
//EndCode changes
?>
<h3><?php _e('ID','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="SaveImageField_field"><?php _e( 'ID', 'woocommerce' ); ?></label></th>
<td>
<!--Code changes-->
<?php if (!empty($image_url)) { ?>
<img src="<?php echo $image_url;?>" style="width: 200px;">
<?php }else{ ?>
<h3>Image Not uploaded</h3>
<?php } ?>
<!--End Code changes-->
</td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_SaveImageField', 10, 1 );
add_action( 'edit_user_profile', 'add_SaveImageField', 10, 1 );

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 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