6

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

7
  • Using 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?
    – cjbj
    Aug 30, 2016 at 7:30
  • @cjbj I want to display the input fields. What do you mean by this statement? ...it is possible to include two <label> tags as a result of one meta field. Aug 30, 2016 at 10:51
  • The html to be displayed on the profile page is generated by the plugin that defines the field. The plugin may decide to hide the field or to generate two labels or whatever. It's not very likely, but not impossible either.
    – cjbj
    Aug 30, 2016 at 10:59
  • @cjbj Okay, from testing I haven't run into that issue yet, so I'm not concerned about that at the moment. Aug 30, 2016 at 11:02
  • 1
    Not all these are stored in user meta, and so they might not be 'fields' as such, since they're special and stored in the user table itself
    – Tom J Nowell
    Sep 3, 2016 at 1:02

1 Answer 1

2

The second parameter in get_user_meta is optional, so you can retrieve all user meta like this (code tested quickly in my install):

$cu = get_current_user_id ();
$um = get_user_meta ($cu);
var_dump ($um);

This would also include meta fields that are hidden on the user profile page.

2
  • Thanks for your input. However, as you mentioned, this returns all of the hidden metadata that I don't want. Also, I it looks like that this won't work in getting the label of each field (ex: user_login = Username), unless I'm wrong. Aug 29, 2016 at 20:16
  • My guess would be that the label is in some way automatically derived from one of the metafields. But I didn't check.
    – cjbj
    Aug 29, 2016 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.