Creating Custom User Contact Methods Plugin for WordPress
WordPress is an excellent platform for creating websites and blogs. It offers a wide range of features and functionalities that can be customized to suit individual needs. One such feature is the user contact methods block that appears when editing a user in the administration. In this article, we will show you how to add your own custom fields to this block and even display them on the registration page.
Step 1: Create the Plugin
To get started, create a new folder in wp-content/plugins and name it “custom-user-contact-methods”. Inside this folder, create a file called “rc-custom-user-contact-methods.php” and open it in your preferred editor software. Copy and paste the following code into the file:
“`
/*
Plugin Name: Custom User Contact Methods
Plugin URL: http://remicorson.com/
Description: Add custom fields to users “contact” section
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
*/
“`
This code registers the plugin and sets its basic information.
Step 2: Define Your Custom Fields
Next, create a variable called `$extra_fields` that will contain the custom fields you want to add. These fields will be used on the user edit page and the registration page. The fields should be stored in an array with three parameters: field ID, field label, and a boolean value that determines whether the field is shown on the registration page or not. Here is an example:
“`
$extra_fields = array(
array( ‘facebook’, __( ‘Facebook Username’, ‘rc_cucm’ ), true ),
array( ‘twitter’, __( ‘Twitter Username’, ‘rc_cucm’ ), true ),
array( ‘googleplus’, __( ‘Google+ ID’, ‘rc_cucm’ ), true ),
array( ‘linkedin’, __( ‘Linked In ID’, ‘rc_cucm’ ), false ),
array( ‘pinterest’, __( ‘Pinterest Username’, ‘rc_cucm’ ), false ),
array( ‘wordpress’, __( ‘WordPress.org Username’, ‘rc_cucm’ ), false ),
array( ‘phone’, __( ‘Phone Number’, ‘rc_cucm’ ), true )
);
“`
You can add as many parameters as you want, such as a placeholder or required information.
Step 3: Hook the Right Filter
Now, hook a function to the “user_contactmethods” filter. This function will add the custom fields to the user edit page. Name the function “rc_add_user_contactmethods”. Here is the code:
“`
// Use the user_contactmethods to add new fields
add_filter( ‘user_contactmethods’, ‘rc_add_user_contactmethods’ );
“`
Step 4: Create Your Custom Fields
Create the “rc_add_user_contactmethods” function. This function will add the custom fields to the user edit page. Since the fields are stored in an array, this function will be dynamic and easy to modify. Here is the code:
“`
function rc_add_user_contactmethods( $user_contactmethods ) {
// Get fields
global $extra_fields;
// Display each field
foreach( $extra_fields as $field ) {
if ( !isset( $contactmethods[ $field[0] ] ) )
$user_contactmethods[ $field[0] ] = $field[1];
}
// Returns the contact methods
return $user_contactmethods;
}
“`
After saving and activating the plugin, you should see your custom fields on the user edit page.
Step 5: Registration Page Hooks
To display your custom fields on the registration page, you need to access two hooks and create two functions. The first function will display the fields, and the second function will save the field data to the database.
“`
// Add our fields to the registration process
add_action( ‘register_form’, ‘rc_register_form_display_extra_fields’ );
add_action( ‘user_register’, ‘rc_user_register_save_extra_fields’, 100 );
“`
Step 6: Display Custom Fields on Registration Page
The `rc_register_form_display_extra_fields` function will display the custom fields on the registration page. It will use the third parameter of each array in `$extra_fields` to determine whether to show the field or not. Here is the code:
“`
function rc_register_form_display_extra_fields() {
// Get fields
global $extra_fields;
// Display each field if 3rd parameter is set to “true”
foreach( $extra_fields as $field ) {
if ( $field[2] == true ) {
$field_value = isset( $_POST[ $field[0] ] ) ? $_POST[ $field[0] ] : ”;
echo ‘
‘;
} // endif
} // end foreach
}
“`
Step 7: Store Field Values Upon Registration Process
The `rc_user_register_save_extra_fields` function will store the custom field values in the database. It will use the `wp_update_user()` function to update the user data. Here is the code:
“`
function rc_user_register_save_extra_fields( $user_id, $password = ”, $meta = array() ) {
// Get fields
global $extra_fields;
$userdata = array();
$userdata[‘ID’] = $user_id;
// Save each field
foreach( $extra_fields as $field ) {
if( $field[2] == true ) {
$userdata[ $field[0] ] = $_POST[ $field[0] ];
} // endif
} // end foreach
$new_user_id = wp_update_user( $userdata );
}
“`
Conclusion
In this article, we have shown you how to create a custom user contact methods plugin for WordPress. You can add or remove fields as needed and even add functions to sanitize your custom fields. With this plugin, you can customize the user experience on your website and make it more user-friendly.