Created
March 25, 2015 19:53
-
-
Save lewayotte/c8f502aad248f8afa374 to your computer and use it in GitHub Desktop.
Restricted Content in WordPress - WordCamp ATL 2015
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 | |
function restricted_content_filter( $content ) { | |
if ( !current_user_can( 'administrator' ) && is_content_restricted() ) { | |
$content = 'Restricted!'; | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'restricted_content_filter' ); | |
add_filter( 'the_excerpt', 'restricted_content_filter' ); | |
function is_content_restricted() { | |
global $post; | |
$restricted_posts = array( 1, 2, 3, 4, 5 ); //You'll want a UI to set this in the options table | |
if ( in_array( $post->ID, $restricted_posts ) ) { | |
$user = get_current_user(); | |
$mode = 'live'; //You'll want to set this to live or test... and store that setting in the options table to set and get it dynamically. | |
$status = get_user_meta( '_member_status_' . $mode, true ); | |
if ( 'active' === $status ) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment