Skip to content

Internal Shortcode | [Post_Author]

EkoJr edited this page Mar 17, 2017 · 5 revisions

Description

Displays the Author Name associated with the post/page.

Usage

[post_author label="display_name"]

Params

Label

(string) (Optional) Determines which label is used to display Author/User data. WP Codex get_userdata()

  • ID
  • user_name (user_login)
  • user_nicename
  • display_name
  • user_email
  • user_url

Legacy WP Labels

  • user_description (description)
  • user_firstname
  • user_lastname

Return

(string) Author/User data -> Label.

Examples/Sample

Example 1

[post_author]

Returns

Jester

Example 2

[post_author label="ID"]

Returns

32

Source

File: advanced-post-list/includes/class/apl_shortcodes.php

/**
 * Post Author Shortcode. 
 * 
 * Desc: Adds the Author/User Data associated with the post. 
 * 
 * 1. Set Label Types used within WP, and extensions. User_Friendly => WP_Friendly. 
 * 2. Get Author/User Data associated with WP_Post. 
 * 3. Add User Label to return, IF Label is valid with APL and IF data/prop 
 *    even exist in UserData (including extension labels registered with 
 *    APL & WP). 
 * 4. Otherwise, IF no variable exists, add default display_name to return. 
 * 
 * @since 0.1.0
 * @version 0.3.0 - Changed to Callback Function, and added 'label' attribute. 
 * @version 0.4.0 - Changed to Class function, and uses WP's built-in
 *                  functions for setting default attributes & do_shortcode().
 * 
 * @link https://codex.wordpress.org/Function_Reference/get_userdata
 * 
 * @param array $atts {
 *     
 *     Shortcode Attributes. 
 *     
 *     @type string 'label' Used to display user data.
 * }
 * @return string User Data from WP_Post.
 */
public function post_author($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'label' => 'display_name'
    ), $atts, 'post_author');
    $return_str = '';
    
    //STEP 1
    $label_type = array(
        //// Data Object (WP's Standard)
        'ID'            => 'ID',
        'user_login'    => 'user_login',
        'user_name'     => 'user_login',
        'user_nicename' => 'user_nicename',
        'display_name'  => 'display_name',
        'user_email'    => 'user_email',
        'user_url'      => 'user_url',
        
        //// Back_Compat_Keys Array (Legacy)
        'description'       => 'user_description',
        'user_description'  => 'user_description',
        'user_firstname'    => 'user_firstname',
        'user_lastname'     => 'user_lastname'
    );
    //STEP 2
    $userData = get_userdata($this->_post->post_author);
    
    //STEP 3
    if (isset($label_type[$atts_value['label']]) && 
        $userData->has_prop($label_type[$atts_value['label']]))
    {
        $return_str .= $userData->get($label_type[$atts_value['label']]);
    }
    
    //STEP 4
    else
    {
        $return_str .= $userData->data->display_name;
    }
    
    //STEP 5
    return $return_str;
}

Additional Resources

Clone this wiki locally