-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-display-name.php
48 lines (43 loc) · 1.1 KB
/
js-display-name.php
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
<?php
/*
Plugin Name: JS Display Name
Plugin URI: http://bwawwp.com/js-display-name/
Description: A way to load the display name of a logged-in user through JavaScript
Version: .1
Author: Jason Coleman
Author URI: http://bwawwp.com
*/
/*
use this function to place the JavaScript in your theme
if(function_exists("jsdn_show_display_name"))
{
jsdn_show_display_name();
}
*/
function jsdn_show_display_name($prefix = "Welcome, ")
{
?>
<p>
<script src="<?php echo admin_url("/admin-ajax.php?action=jsdn_show_display_name&prefix=" . urlencode($prefix));?>"></script>
</p>
<?php
}
/*
This function detects the JavaScript call and returns the user's display name
*/
function jsdn_wp_ajax()
{
global $current_user;
if(!empty($current_user->display_name))
{
$prefix = sanitize_text_field($_REQUEST['prefix']);
$text = $prefix . $current_user->display_name;
header('Content-Type: text/javascript');
?>
document.write(<?php echo json_encode($text);?>);
<?php
}
exit;
}
add_action('wp_ajax_jsdn_show_display_name', 'jsdn_wp_ajax');
add_action('wp_ajax_nopriv_jsdn_show_display_name', 'jsdn_wp_ajax');