-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser-meta-shortcode.php
More file actions
68 lines (57 loc) · 1.56 KB
/
user-meta-shortcode.php
File metadata and controls
68 lines (57 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* User Meta Shortcode
*
* Plugin Name: User Meta Shortcode
* Description: Add shortcode to display user meta.
* Version: 1.0
* Author: Patryk Kachel
* Author URI: https://blogonyourown.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) {
die();
}
class User_Meta_Shortcode {
private static $_instance = null;
private $_user_table_fields = array( 'user_login', 'user_email', 'user_url', 'display_name', 'user_nicename', 'user_pass', 'ID', 'user_registered' );
public function __construct() {
add_shortcode( 'user_meta', array( $this, 'user_meta_shortcode' ) );
}
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function user_meta_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => get_current_user_id(),
'key' => '',
),
$atts,
'user_meta'
);
if ( empty( $atts['id'] ) || empty( $atts['key'] ) ) {
return;
}
$output = '';
if ( in_array( $atts['key'], $this->_user_table_fields ) ) {
$output = $this->get_userdata( $atts );
} else {
$output = $this->get_user_meta( $atts );
}
return $output;
}
private function get_user_meta( $atts ) {
return get_user_meta( $atts['id'], $atts['key'], true );
}
private function get_userdata( $atts ) {
$userdata = get_userdata( $atts['id'] );
$key = $atts['key'];
return $userdata->$key;
}
}
User_Meta_Shortcode::instance();