Plugin Directory

Changeset 2335090


Ignore:
Timestamp:
07/03/2020 10:59:23 PM (6 years ago)
Author:
rvencu
Message:

version 0.5

Location:
user-meta-shortcodes/tags/0.5
Files:
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • user-meta-shortcodes/tags/0.5/user-meta-shortcodes.php

    r788594 r2335090  
    11<?php
    22/**
    3 Plugin Name: User meta shortcodes
    4 Description: Use user meta-data as shortcodes in post content
    5 Plugin URI:  http://vencu.ro/user-meta-shortcodes/
    6 Author:      Richard Vencu
    7 Author URI:  http://vencu.ro
    8 Version:     0.4.1
    9 
    10 
    11 USAGE:
    12 
    13 use [userinfo field="fieldname"]some content[/userinfo] or [authorinfo field="fieldname"]some content[/authorinfo] shortcodes in your post content to show the "fieldname" meta value from the user-meta for the current logged-in user or for the post author without editing your theme files.
    14 
    15 
    16 EXAMPLES:
    17 
    18 [userinfo field="last_name"]{{empty}}[/userinfo]
    19 
    20 returns the last name of the current logged-in user. If no user is logged in then the value is empty string
    21 
    22 [userinfo field="user_login" if="admin"]You are the admin[/userinfo]
    23 
    24 this shortcode tests the field against the value and if true it displays the included content
    25 
    26 [userinfo field="user_login"] is your username and you are reading a post by [authorinfo field="user_login"]{{empty}}[/authorinfo].[/userinfo]
    27 
    28 this will display the username followed with the processed content. Note: never use the nested shortcode same as the enclosing shortcode. This will produce unexpected results!
    29 
    30 [userinfo nospan="true"] should eliminate the surrounding span tag so the output can be used inside URLs or similar applications
    31 
    32 [userinfo field="avatar" size="50"] will display the logged-in user's avatar with the size of 50px. The display depends of theme's css class 'avatar'
    33 
    34 
    35 List of some of the available meta field names: ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, display_name, first_name, last_name,nickname, description, user_level, admin_color (Theme of your admin page. Default is fresh.), closedpostboxes_page, nickname, primary_blog, rich_editing, source_domain
    36 
    37 [authorinfo field="last_name"]
    38 
    39 returns the last name of the current post/page author.
    40 
    41 
    42 [authorinfo field="ID"]
    43 
    44 returns the user ID of the current post/page author.
    45 
    46 [authorinfo field="avatar" size="50"] will display the post author's avatar with the size of 50px. The display depends of theme's css class 'avatar'
    47 
    48 [authorinfo field="posts"] will display the  author's posts link
    49 
    50 List of some of the available meta field names: user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name,nickname, first_name, last_name, description, jabber, aim, yim, user_level, user_firstname, user_lastname, user_description, rich_editing, comment_shortcuts, admin_color,plugins_per_page, plugins_last_view, ID
    51 
    52 If you add custom user meta via additional plugins, then the meta should be available for the above shortcodes
    53 
    54 
    55 
    56 */
     3 * Plugin Name: User meta shortcodes
     4 * Description: Use user meta-data as shortcodes in post content
     5 * Plugin URI:  http://vencu.ro/user-meta-shortcodes/
     6 * Author:      Richard Vencu
     7 * Author URI:  http://vencu.ro
     8 * Version:     0.5
     9 *
     10 *
     11 * USAGE:
     12 *
     13 * use [userinfo field="fieldname"]some content[/userinfo] or [authorinfo field="fieldname"]some content[/authorinfo] shortcodes in your post content to show the "fieldname" meta value from the user-meta for the current logged-in user or for the post author without editing your theme files.
     14 *
     15 *
     16 * EXAMPLES:
     17 *
     18 * [userinfo field="last_name"]{{empty}}[/userinfo]
     19 *
     20 * returns the last name of the current logged-in user. If no user is logged in then the value is empty string
     21 *
     22 * [userinfo field="user_login" if="admin"]You are the admin[/userinfo]
     23 *
     24 * this shortcode tests the field against the value and if true it displays the included content
     25 *
     26 * [userinfo field="user_login"] is your username and you are reading a post by [authorinfo field="user_login"]{{empty}}[/authorinfo].[/userinfo]
     27 *
     28 * this will display the username followed with the processed content. Note: never use the nested shortcode same as the enclosing shortcode. This will produce unexpected results!
     29 *
     30 * [userinfo nospan="true"] should eliminate the surrounding span tag so the output can be used inside URLs or similar applications
     31 *
     32 * [userinfo field="avatar" size="50"] will display the logged-in user's avatar with the size of 50px. The display depends of theme's css class 'avatar'
     33 *
     34 *
     35 * List of some of the available meta field names: ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, display_name, first_name, last_name,nickname, description, user_level, admin_color (Theme of your admin page. Default is fresh.), closedpostboxes_page, nickname, primary_blog, rich_editing, source_domain
     36 *
     37 * [authorinfo field="last_name"]
     38 *
     39 * returns the last name of the current post/page author.
     40 *
     41 *
     42 * [authorinfo field="ID"]
     43 *
     44 * returns the user ID of the current post/page author.
     45 *
     46 * [authorinfo field="avatar" size="50"] will display the post author's avatar with the size of 50px. The display depends of theme's css class 'avatar'
     47 *
     48 * [authorinfo field="posts"] will display the  author's posts link
     49 *
     50 * List of some of the available meta field names: user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name,nickname, first_name, last_name, description, jabber, aim, yim, user_level, user_firstname, user_lastname, user_description, rich_editing, comment_shortcuts, admin_color,plugins_per_page, plugins_last_view, ID
     51 *
     52 * If you add custom user meta via additional plugins, then the meta should be available for the above shortcodes
     53 */
    5754?>
    5855<?php
    5956
    60   function otherUserInfoSc ( $attributes, $content=null ) {   
     57function otherUserInfoSc( $attributes, $content = "" ) {
    6158
    62     extract(shortcode_atts(array(
    63         "field" => null, "if" => null, "nospan"=>false, "size"=>32, $default=>"", "uservar"=>"", "login"=>""
    64     ), $attributes));
    65    
    66     //use {{empty}} instead of nothing inside the shortcode
    67     if('{{empty}}'==$content) $content="";
    68    
    69     if (isset($uservar) && $uservar != "") {
    70         $user_info = get_user_by("login",$_GET[$uservar]);
     59    extract( shortcode_atts( array(
     60        "field"   => null,
     61        "if"      => null,
     62        "nospan"  => false,
     63        "size"    => 32,
     64        $default  => "",
     65        "uservar" => "",
     66        "login"   => ""
     67    ), $attributes ) );
     68
     69    //if {{empty}} instead of nothing inside the shortcode
     70    if ( '{{empty}}' == $content ) {
     71        $content = "";
    7172    }
    72     else
    73     {
    74         $user_info = get_user_by("login",$login);
     73
     74    if ( isset( $uservar ) && $uservar != "" ) {
     75        $user_info = get_user_by( "login", $_GET[ $uservar ] );
     76    } else {
     77        $user_info = get_user_by( "login", $login );
    7578    }
    76    
     79
    7780    //if we have a test, we print the content in case the test is true. otherwise we print the field value concatenated with the content of the shortcode
    78     if (isset($if) && $field != "avatar") {
    79         if ( $if == $user_info->$field ) return do_shortcode($content);
    80     }
    81     else {
    82         if( $field != "avatar" ) {
    83             if ($nospan)
    84                 return $user_info->$field . do_shortcode($content);
    85             else
    86                 return "<span class=\"userinfo\">" . $user_info->$field . do_shortcode($content) . "</span>";
     81    if ( isset( $if ) && $field != "avatar" ) {
     82        if ( $if == $user_info->$field ) {
     83            return do_shortcode( $content );
    8784        }
    88         else  {
     85    } else {
     86        if ( $field != "avatar" ) {
     87            if ( $nospan ) {
     88                return $user_info->$field . do_shortcode( $content );
     89            } else {
     90                return "<span class=\"userinfo\">" . $user_info->$field . do_shortcode( $content ) . "</span>";
     91            }
     92        } else {
    8993            return get_avatar( $user_info->id, $size, $default, $user_info->first_name . " " . $user_info->last_name );
    9094        }
     
    9397    //return empty string in case we arrive here
    9498    return "";
     99}
    95100
    96 }
    97   add_shortcode('otheruserinfo','otheruserInfoSc');
     101add_shortcode( 'otheruserinfo', 'otheruserInfoSc' );
    98102
    99   function userInfoSc ( $attributes, $content=null ) {   
     103function userInfoSc( $attributes, $content = "" ) {
    100104
    101     extract(shortcode_atts(array(
    102         "field" => null, "if" => null, "nospan"=>false, "size"=>32, $default=>""
    103     ), $attributes));
    104    
    105     //use {{empty}} instead of nothing inside the shortcode
    106     if('{{empty}}'==$content) $content="";
     105    extract( shortcode_atts( array(
     106        "field"  => null,
     107        "if"     => null,
     108        "nospan" => false,
     109        "size"   => 32,
     110        $default => ""
     111    ), $attributes ) );
    107112
    108     global $user_info, $user_ID;
    109    
    110     get_currentuserinfo();
     113    //if {{empty}} instead of nothing inside the shortcode
     114    if ( '{{empty}}' == $content ) {
     115        $content = "";
     116    }
    111117
    112     $user_info = get_userdata($user_ID);
    113    
     118    global $user_info, $user_ID;
     119
     120    get_currentuserinfo();
     121
     122    $user_info = get_userdata( $user_ID );
     123
    114124    //if we have a test, we print the content in case the test is true. otherwise we print the field value concatenated with the content of the shortcode
    115     if (isset($if) && $field != "avatar") {
    116         if ( $if == $user_info->$field ) return do_shortcode($content);
    117     }
    118     else {
     125    if ( isset( $if ) && $field != "avatar" ) {
     126        if ( $if == $user_info->$field ) {
     127            return do_shortcode( $content );
     128        }
     129    } else {
    119130        if ( is_user_logged_in() ) {
    120             if( $field != "avatar" ) {
    121                 if ($nospan)
    122                     return $user_info->$field . do_shortcode($content);
    123                 else
    124                     return "<span class=\"userinfo\">" . $user_info->$field . do_shortcode($content) . "</span>";
    125             }
    126             else {
     131            if ( $field != "avatar" ) {
     132                if ( $nospan ) {
     133                    return $user_info->$field . do_shortcode( $content );
     134                } else {
     135                    return "<span class=\"userinfo\">" . $user_info->$field . do_shortcode( $content ) . "</span>";
     136                }
     137            } else {
    127138                return get_avatar( $user_info->id, $size, $default, $user_info->first_name . " " . $user_info->last_name );
    128139            }
     
    135146}
    136147
    137   add_shortcode('userinfo','userInfoSc');
     148add_shortcode( 'userinfo', 'userInfoSc' );
    138149
    139   function authorInfoSc ( $attributes, $content=null ) {
     150function authorInfoSc( $attributes, $content = null ) {
    140151
    141     extract(shortcode_atts(array(
    142         "field" => null, "if" => null, "nospan"=>false, "size"=>32, $default=>""
    143     ), $attributes));
    144    
    145     //use {{empty}} instead of nothing inside the shortcode
    146     if('{{empty}}'==$content) $content="";
     152    extract( shortcode_atts( array(
     153        "field"  => null,
     154        "if"     => null,
     155        "nospan" => false,
     156        "size"   => 32,
     157        $default => ""
     158    ), $attributes ) );
     159
     160    //if {{empty}} instead of nothing inside the shortcode
     161    if ( '{{empty}}' == $content ) {
     162        $content = "";
     163    }
    147164
    148165    //if we have a test, we print the content in case the test is true. otherwise we print the field value concatenated with the content of the shortcode
    149     if (isset($if)) {
    150         if ( $if == get_the_author_meta($field) ) return do_shortcode($content);
    151     }
    152     else {
    153         switch ($field) {
     166    if ( isset( $if ) ) {
     167        if ( $if == get_the_author_meta( $field ) ) {
     168            return do_shortcode( $content );
     169        }
     170    } else {
     171        switch ( $field ) {
    154172            case "avatar":
    155                 return get_avatar( get_the_author_meta("id"), $size, $default, get_the_author_meta("first_name") . " " . get_the_author_meta(last_name) );
     173                return get_avatar( get_the_author_meta( "id" ), $size, $default, get_the_author_meta( "first_name" ) . " " . get_the_author_meta( last_name ) );
    156174            case "posts":
    157                 return get_author_posts_url( get_the_author_meta("id") );
     175                return get_author_posts_url( get_the_author_meta( "id" ) );
    158176            default:
    159                 if ($nospan)
    160                     return get_the_author_meta($field) . do_shortcode($content);
    161                 else
    162                     return "<span class=\"userinfo\">" . get_the_author_meta($field) . do_shortcode($content) . "</span>";
     177                if ( $nospan ) {
     178                    return get_the_author_meta( $field ) . do_shortcode( $content );
     179                } else {
     180                    return "<span class=\"userinfo\">" . get_the_author_meta( $field ) . do_shortcode( $content ) . "</span>";
     181                }
    163182        }
    164183    }
     
    166185    //return empty string in case we arrive here
    167186    return "";
     187}
    168188
    169   }
    170 
    171   add_shortcode('authorinfo','authorInfoSc');
    172 
    173 ?>
     189add_shortcode( 'authorinfo', 'authorInfoSc' );
Note: See TracChangeset for help on using the changeset viewer.