Search code examples
phpwordpress

Reserved keys for user meta in wordpress - About me


Does anyone know which meta keys are reserved (that I cannot use for my own purposes) in wordpress, if any?

Also, for the wordpress about me section. Does anyone know how to call that?

Thanks in advance.


Solution

  • Post Meta Keys:

    You probably don't want to use the following post meta keys:

    • _thumbnail_id - Used to store the featured image ID
    • _edit_last - Used by the Heartbeat API
    • _edit_lock - Used by the Heartbeat API
    • _wp_page_template - Stores the page template.
    • _wp_attached_file
    • _wp_attachment_metadata
    • _menu_item_{url, object, object_id, target, classes, xfn, ... }

    as your custom meta keys, as they might be overwritten by the WordPress Core.

    User Meta Keys:

    Similarly for these user meta keys:

    • first_name
    • last_name
    • nickname
    • description
    • rich_editing
    • comment_shortcuts
    • admin_color
    • jabber
    • aim
    • yim
    • default_password_nag
    • use_ssl
    • show_admin_bar_front
    • show_welcome_panel
    • dismissed_wp_pointers
    • nav_menu_recently_edited
    • managenav-menuscolumnshidden
    • closedpostboxes_{post, dashboard, page, ...}
    • metaboxhidden_{post, dashboard, page, ...}
    • meta-box-order_{post, dashboard, page, ...}
    • screen_layout_{post, dashboard, page, ...}

    and the following user meta keys that have the default table prefix:

    • wp_capabilities
    • wp_user_level
    • wp_dashboard_quick_press_last_post_id
    • wp_user-settings
    • wp_user-settings-time

    So in general, I would suggest you to prefix your own meta keys, to avoid clashes with the WordPress core or other plugins or themes.

    How To Display The User Meta Keys?

    If you want to display all the user meta keys, for the current user, you can use:

    print_r( array_keys( get_user_meta( get_current_user_id() ) ) );
    

    with an output like:

    Array
    (
        [0] => first_name
        [1] => last_name
        [2] => nickname
        [3] => description
        [4] => rich_editing
        [5] => comment_shortcuts
        [6] => admin_color
        [7] => use_ssl
        [8] => show_admin_bar_front
        ...cut...
    )
    

    Check out the Codex page to get more information about the get_user_meta() function.

    Or just investigate your wp_usermeta table.

    A Simple Plugin To View The Metadata For Each User:

    It's useful to be able to view the metadata for each user, so let's create a simple plugin for that.

    Here's a screenshot from a user's profile/edit page:

    User Meta View

    and here's the code:

    <?php
    /**
     * Plugin Name: A Simple User Meta Data Viewer
     * Description: This plugin allows the site admin to view the metadata for each user, in the edit user screen
     * Author:      birgire
     * Version:     0.0.1
     * Plugin URI:  http://stackoverflow.com/a/25316090/2078474
     */
    
    add_action( 'show_user_profile', 'birgire_usermeta_list' );
    add_action( 'edit_user_profile', 'birgire_usermeta_list' );
    
    function birgire_usermeta_list( $profileuser )
    {
        if( current_user_can( 'manage_options' ) )
        {
            // Fetch all the user meta data for current profile user:
            $items = get_user_meta( $profileuser->ID );
    
            // Loop:
            $rows = '';
            foreach( $items as $key => $item )
            {
                    $rows .= sprintf( '
                            <tr>
                                <th>%s</th>
                                <td><input type="text" value="%s" readonly="readonly" class="regular-text" /></td>
                           </tr>',
                        $key,
                        esc_attr( array_shift( $item ) )
                    );
            }
    
            // Output:
            printf( '<h3>%s</h3><table class="form-table"><tbody>%s</tbody></table>', 
                __( 'User Meta' ),
                $rows   
            );
    
        }
    }
    

    I hope this helps.