forked from fatthemes/user-meta-shortcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_meta_select.php
More file actions
83 lines (75 loc) · 2.23 KB
/
user_meta_select.php
File metadata and controls
83 lines (75 loc) · 2.23 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
Plugin Name: User Meta Select
Description: Adds a new block where user can select the meta data to be displayed
Version: 1.0
Author: Snigdha
*/
if(! defined('ABSPATH'))
exit;
class MyUserMetaSelect{
function __construct(){
add_action('init',array($this,'adminAsset'));
}
function adminAsset(){
wp_register_script('myblockScript',plugin_dir_url(__FILE__).'build/index.js',array('wp-blocks','wp-element','wp-i18n', 'wp-editor'));
wp_register_style('myblockStyle', plugin_dir_url(__FILE__) . 'build/editor.css');
wp_register_style('myblockStyleComplete', plugin_dir_url(__FILE__) . 'build/style-index.css');
register_block_type('ourplugin/usermetaselect', array(
'editor_script'=>'myblockScript',
'editor_style' => 'myblockStyle',
'style' => 'myblockStyleComplete',
'render_callback'=> array($this, 'displayUserMeta')
));
}
function displayUserMeta($attributes){
$user_id = get_current_user_id();
$data = get_userdata( $user_id );
$username = $data->user_login;
$user_email = $data->user_email;
$first_name = $data->first_name;
$last_name = $data->last_name;
if( $attributes['choice'] == 1) {
if(!empty($first_name))
{
$message = 'First Name: '.$first_name;
}
else {
$message = 'First Name: This field is empty';
}
}
elseif ($attributes['choice'] == 2) {
if (!empty($last_name))
{
$message = 'Last Name: '.$last_name;
}
else{
$message = 'Last Name: This field is empty';
}
}
elseif ($attributes['choice'] == 3) {
if(!empty($username))
{
$message = 'User ID: '.$username;
}
else {
$message = 'User ID: This field is empty';
}
}
elseif ($attributes['choice'] == 4){
if(!empty($user_email))
{
$message = 'User Email: '.$user_email;
}
else {
$message = 'User Email: This field is empty';
}
}
ob_start();?>
<div>
<p>If you are logged in, you will be able to see the following user data: </p><p class="userdata"><?php echo esc_html($message);?></p></div>
<?php return ob_get_clean();
}
}
$userMetaSelect = new MyUserMetaSelect();
?>