Menu Close Menu
Menu
  • 0
    • Number of items in basket: 0

      • Your basket is empty.
      • Total: £0.00
      • Checkout
  • Blog
  • Extensions
  • Support
    • Documentation
    • Log a Support Ticket
  • Your Account
    • Register
  • Contact Us
  • Demo

Creating Custom User Fields

Creating Custom User Fields

Adding custom fields to the user profile screen for customers and agents

Using the user profile hooks WordPress already has, KB Support provides additional hooks that ensure all custom user fields created by any KB Support extension are grouped together under a user profile page header section called KB Support.

There are two hooks of interest here;

The kbs_user_profile_fields filter is used to determine which custom user profile fields are registered within KBS and it is important to do this so that KBS knows whether or not to print out the KB Support profile page header section. It also ensures the hook required to display the profile fields is run.

Simply, if there are no custom fields registered within KBS, the KB Support profile page header section will not be displayed, and the kbs_display_user_profile_fields hook which displays custom KBS user fields will not run either.

/**
 * @param	array	$fields	Array of registed KBS custom user profile fields
 * @return	array	Filtered array of registed KBS custom user profile fields
 */
function mh_custom_profile_fields( $fields )	{

	$mh_fields = array( 'company_name' );
	$fields    = array_merge( $mh_fields, $fields );

	return $fields;

}
add_filter( 'kbs_user_profile_fields', 'mh_custom_profile_fields' );

In the above example, we’re registering the field name company_name.

Next up, we need to hook into the kbs_display_user_profile_fields action to display our field.

/**
 * @param	object	$user	The WP_User object
 * @return	void	Any output must be echo'd as `add_action` does not return a value
 */
function mh_company_name_profile_field( $user )	{

	ob_start();
	?>
	<tr>
		<th><label for="company_name"><?php _e( 'Company Name', 'kb-support' ); ?></label></th>

		<td><input type="text" name="company_name" id="company_name" class="regular-text" value="<?php echo esc_attr( get_the_author_meta( 'company_name', $user->ID ) ); ?>">

			<p class="description"><?php _e( 'Enter your company name here.', 'kb-support' ); ?></p>

		</td>
	</tr>
	<?php
	echo ob_get_clean();

}
add_action( 'show_user_profile', 'mh_company_name_profile_field' );
add_action( 'edit_user_profile', 'mh_company_name_profile_field' );

The above code will result in the following output:

Company Name

Remember, you’ll still need to hook into personal_options_update and edit_user_profile_update to save the value of your field(s) when the user saves changes.


Was this article helpful?

We're working hard to ensure we provide you with useful and relevant documentation to help you get the most out of KB Support.

Please take a moment to let us know if you found this article helpful.

Share this:

  • Tweet
  • WhatsApp

Related Articles

Developer Docs Hooks Agent Customer Last updated: 3rd March 2017

Published by Michael

Mike is the founder and lead developer of KB Support. When he's not working on KB Support, he's generally spending time with his 3 children and following his favourite football team

Posts by Michael Visit Website

Post navigation

Previous

Configuring Custom Ticket Status

Next

Template Tags

Article Categories

  • Configuration
    • Advanced
    • Getting Started
  • Developer Docs
    • Constants
    • Hooks
  • Extensions
  • FAQs

Social Links

  • Follow us on Twitter
  • Like us on Facebook
  • Fork us on GitHub

Resources

  • Contact Us
  • KB Support Discount Code
  • KB Support Demo Site
  • Easy Plugin Demo

Sign up to our newsletter!

Subscribe now to receive all the latest news, thoughts and offers from KB Support.
New subscribers will receive 15% off their first purchase.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
© 2021 KB Support. All rights reserved.
  • Blog
  • Shop
  • Support
  • Demo
  • Privacy Policy
  • Discount Code