By default, the User Profile page has a few options and fields that the user can set. Such as the following:
Personal Options
-- Visual Editor
-- Admin Color Scheme
-- Keyboard Shortcuts
-- Toolbar
Name
-- First Name
-- Last Name
-- Nickname
-- Display Name
Contact Info
-- Website
About Yourself
-- Biographical Info
-- Avatar Display
In my plugin, Disable Blogging, I've added a settings page which lists all of these fields in checkboxes to toggle which ones to hide (see screenshot).
However, depending on the plugin or theme you're using, there will be additional settings and fields that will be shown. Take the Yoast SEO plugin as an example, which adds these extra fields under the Contact Info section and a section of their own:
Contact Info
-- Website
-- Google+
-- Twitter username (without @)
-- Facebook profile URL
...
Yoast SEO
-- Title to use for Author page
-- Meta description to use for Author page
-- Exclude user from Author-sitemap
-- Disable SEO analysis
-- Disable readability analysis
Using the wp_get_user_contact_methods()
function, I was able to easily echo
a list of the custom contact fields that were added by the Yoast SEO plugin like so:
add_action( 'personal_options', 'wpse_237504_user_profile_fields' );
function wpse_237504_user_profile_fields() {
foreach ( wp_get_user_contact_methods() as $id => $label ) {
echo( $label . " > " . $id . "<br>" );
}
}
Output:
Google+ > googleplus
Twitter username (without @) > twitter
Facebook profile URL > facebook
Unfortunately, I do not see a native WordPress function I can call to get all of the fields listed in the User Profile.
The Disable Blogging plugin hides these fields is by using JavaScript based on the for=
values set in each <label>
. For example: <label for="user_login">Username</label>
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$( 'form#your-profile > h2' ).hide();
<?php
foreach ( $profile_fields as $label ) {
echo( "$( '#" . $label . "' ).closest( 'tr' ).hide(); " );
}
?>
} );
</script>
<?php
Overall Goal
While this works for all of User Profile fields that I've manually listed in the plugin, my overall goal for this question is automatically get a list of all available fields (both native and custom) in the User Profile without having to manually list them in my plugin.
This way, when a user who is using the Disable Blogging plugin, want to hide profile files that were created by another plugin or theme, it will be automatically listed as a checkbox.
While I'm not asking for anyone to write the whole code for me (though I won't mind either and be happy to add you as a contributor), I'll accept partial answers to help me complete this function or show me an alternative way to get the same results.
If there is something you're not clear of, drop a comment or message me on The Loop for a faster response
preg_match
to find<label>
tags in the html will work, but theoretically it is possible to include two<label>
tags as a result of one meta field. So whether this result is water tight depends on the ultimate goal of your list: do you want the database fields or the displayed input fields?...it is possible to include two <label> tags as a result of one meta field.