-
-
Save butlerblog/fb7a11d4695ed4186dc2 to your computer and use it in GitHub Desktop.
Example of Group restriction built from the example code for levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This example is primarily the same as the example code for levels from: | |
* http://rocketgeek.com/filter-hooks/restrict-content-by-user-level/ | |
* | |
* The only real core change was the logic in the check_user_access() | |
* function hooked ot wpmem_securify. The original example had logic | |
* that was primarily hierarchial. This example's logic looks to see | |
* if a user has a value or not. | |
* | |
* Important note: while this example is simple to set up, especially | |
* because it reuses components from a related example, it's simplicity | |
* has one drawback and that is that it is not a "groups" example in terms | |
* of some users being members of more than one group. This example is | |
* only able to set users as members of one group. | |
* | |
* The error message in the setup function is a little different than | |
* the levels example - it incorporates the example from another post | |
* and puts the name of the group into the error message. | |
* | |
* To use, one should be familiar with the levels example post referenced | |
* above, as well as the groups by category example here: | |
* http://rocketgeek.com/filter-hooks/how-to-restrict-categories-to-a-defined-user-group/ | |
* | |
* This code is a hybrid of those two processes. | |
*/ | |
/** | |
* A simple function to create a settings array | |
* for access levels and information. | |
*/ | |
function my_post_access_levels() { | |
return array( | |
'levels' => array( | |
/** | |
* Set array values as: | |
* metavalue => label, | |
*/ | |
'group_a' => 'Group A', | |
'group_b' => 'Group B', | |
'group_c' => 'Group C', | |
), | |
'default' => 'group_a', | |
'user_field' => 'user_category', | |
'post_field' => 'post_access', | |
'error_msg' => 'This content is restricted to %s users.', | |
); | |
} | |
// Remove this if there is no default... | |
/** | |
* Set the user to the default at registration. | |
*/ | |
add_action( 'wpmem_post_register_data', 'set_default_level' ); | |
function set_default_level( $fields ){ | |
extract( my_post_access_levels() ); | |
update_user_meta( $fields['ID'], $user_field, $default ); | |
} | |
// End remove... | |
// No need to change anything after this line. | |
/** | |
* Sets up access restriction by User ID and access level | |
*/ | |
add_filter( 'wpmem_securify', 'check_user_access' ); | |
function check_user_access( $content ) { | |
// Is the content to be blocked and is the user logged in? | |
if ( is_user_logged_in() && wpmem_block() == true ) { | |
// Get settings. | |
extract( my_post_access_levels() ); | |
// We need the user's ID and the $post object. | |
global $user_ID, $post; | |
// Get the user's access level. | |
$user_level = get_user_meta( $user_ID, $user_field, true ); | |
// Get the post access level. | |
$post_access = get_post_meta( $post->ID, $post_field, true ); | |
/* | |
* If the user is the correct post access or an admin/super admin | |
* then return content, otherwise return error message. | |
*/ | |
return ( $user_level == $post_access || current_user_can( 'edit_plugins' ) ) ? $content : sprintf( $error_msg, $levels[ $post_access ] ); | |
} | |
// Return unfiltered content for all other cases. | |
return $content; | |
} | |
/** | |
* Add dropdown to post and page meta box for marking level. | |
*/ | |
add_action( 'wpmem_admin_after_block_meta', 'my_add_post_level', 10, 2 ); | |
function my_add_post_level( $post, $block ) { | |
// Get the settings. | |
extract( my_post_access_levels() ); | |
// Get the current access setting, if any. | |
$post_access = get_post_meta( $post->ID, $post_field, true ); | |
// Create dropdown selector of users for access. | |
echo '<label>Limit access to: </label> | |
<select name="post_access"> | |
<option value="">Select User Level</option>'; | |
foreach ( $levels as $meta => $label ) { | |
$selected = ( $meta == $post_access ) ? 'selected' : ''; | |
echo "<option value=\"$meta\" $selected>$label</option>"; | |
} | |
echo '</select>'; | |
} | |
/** | |
* Save custom post meta for user ID access. | |
*/ | |
add_action('wpmem_admin_block_meta_save','my_add_post_level_save',10,3); | |
function my_add_post_level_save( $post, $block, $unblock ) { | |
extract( my_post_access_levels() ); | |
if ( isset( $_POST[$post_field] ) ) { | |
// If a user ID is passed to limit access, save it. | |
update_post_meta( $post->ID, $post_field, $_POST[$post_field] ); | |
} else { | |
// If no user ID is passed to limit access, delete any meta. | |
delete_post_meta( $post->ID, $post_field ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this line | |
/** | |
* Optional filter to redirect user on login based | |
* on the group the user has access to.group. | |
* | |
* Adjust page URLs accordingly. | |
*/ | |
add_filter( 'wpmem_login_redirect', 'my_login_redirect', 10, 2 ); | |
function my_login_redirect( $redirect_to, $user_id ) { | |
extract( my_post_access_levels() ); | |
// Get the user's access level. | |
$user_level = get_user_meta( $user_id, $user_field, true ); | |
switch ( $user_level ) { | |
case 'group_a': | |
$redirect_to = site_url( '/group_a_page/' ); | |
break; | |
case 'group_b': | |
$redirect_to = site_url( '/group_b_page/' ); | |
break; | |
case 'group_c': | |
$redirect_to = site_url( '/group_c_page/' ); | |
break; | |
} | |
return $redirect_to; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add below this line to functions.php | |
add_shortcode( 'restrict_content', 'my_restrict_content' ); | |
function my_restrict_content( $atts, $content = null, $tag ) { | |
// This should be the option name/meta key for your level field | |
$user_field = 'user_category'; | |
// You shouldn't need any other changes after this. | |
$current_user = wp_get_current_user(); | |
if ( array_key_exists( 'level', $atts ) ) { | |
$user_level = get_user_meta( $current_user->ID, $user_field, true ); | |
if ( $atts['level'] == $user_level ) { | |
return do_shortcode( $content ); | |
} | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment