Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A reply to one of my blog's comment. The goal is to restrict the content to a BuddyPress member and to run nested shortcode. Example of use : [user_restrict user_id="3"][bc_groups][/user_restrict]
<?php
/*
Restricting content to the user_id
Requires BuddyPress 1.7 & Bowe Codes 2.0.1
*/
class Restrict_Content_User_Shortcode{
function __construct() {
$this->setup_filters();
}
function setup_filters() {
add_filter( 'bowe_codes_shortcodes', array( $this, 'register' ), 10, 1 );
add_filter( 'bowe_codes_shortcodes_settings', array( $this, 'settings' ), 10, 1);
}
function register( $shortcodes = array() ) {
$shortcodes['user_restrict'] = array( $this, 'display_restricted' );
return $shortcodes;
}
/*
to make this work, you'll need to close manually the shortcode in wp_editor
eg : [user_restrict user_id="2"]the content to restrict[/user_restrict]
*/
function display_restricted( $atts = '', $content = '' ) {
$restricted = false;
$defaults = array(
'user_id' => false,
);
$r = wp_parse_args( $atts, $defaults );
extract($r);
if( empty( $content ) )
return false;
/* adding a do_shortcode( $content ) will allow to make the shortcodes in $content to be interpreted*/
if( ( ! empty( $user_id ) && bp_loggedin_user_id() == $user_id ) || is_super_admin() )
$restricted = '<div id="restricted">'.do_shortcode( $content ).'</div>';
else
$restricted = false;
return $restricted;
}
function settings( $settings = array() ) {
$settings['user_restrict'] = array(
'attributes' => array(
array(
'id' => 'user_id',
'type' => 'int',
'default' => 0,
'required' => true,
'caption' => __( 'user id to allow the content for' )
)
),
'description' => __( 'Restrict content to a user', 'bowe-codes' )
);
return $settings;
}
}
function restrict_content_user() {
return new Restrict_Content_User_Shortcode();
}
add_action( 'bowe_codes_ready', 'restrict_content_user' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment