forked from chrisguitarguy/WPSE-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restricted-content-shortcode.php
71 lines (61 loc) · 2.19 KB
/
restricted-content-shortcode.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
Plugin Name: Restricted Content Shortcode
Plugin URI: http://wordpress.stackexchange.com/q/57819/6035
Description: Adds a shortcode that hides content from non-logged in users
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('init', 'wpse57819_add_shortcode');
/**
* Adds the shortcode
*
* @uses add_shortcode
* @return null
*/
function wpse57819_add_shortcode()
{
add_shortcode('restricted', 'wpse57819_shortcode_cb');
}
/**
* Callback function for the shortcode. Checks if a user is logged in. If they
* are, display the content. If not, show them a link to the login form.
*
* @return string
*/
function wpse57819_shortcode_cb($args, $content=null)
{
// if the user is logged in just show them the content. You could check
// rolls and capabilities here if you wanted as well
if(is_user_logged_in())
return $content;
// If we're here, they aren't logged in, show them a message
$defaults = array(
// message show to non-logged in users
'msg' => __('You must login to see this content.', 'wpse57819'),
// Login page link
'link' => site_url('wp-login.php'),
// login link anchor text
'anchor' => __('Login.', 'wpse57819')
);
$args = wp_parse_args($args, $defaults);
$msg = sprintf(
'<aside class="login-warning">%s <a href="%s">%s</a></aside>',
esc_html($args['msg']),
esc_url($args['link']),
esc_html($args['anchor'])
);
return $msg;
}