Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PMPro Display membership levels for posts and pages
function pmprobe_manage_posts_columns( $columns, $post_type)
{
$post_types = apply_filters('pmprobe_post_type_columns', array('post'));
if (in_array($post_type, $post_types))
{
$columns[ 'pmpro_membership_levels' ] = 'Membership Levels';
}
return $columns;
}
add_filter( 'manage_posts_columns', 'pmprobe_manage_posts_columns', 10, 2 );
function pmprobe_manage_pages_columns($columns)
{
$columns[ 'pmpro_membership_levels' ] = 'Membership Levels';
return $columns;
}
add_filter( 'manage_pages_columns', 'pmprobe_manage_pages_columns', 10, 2 );
function pmprobe_manage_posts_custom_column( $column_name, $post_id )
{
global $wpdb;
$sqlQuery = "SELECT membership_id FROM $wpdb->pmpro_memberships_pages LEFT JOIN $wpdb->pmpro_membership_levels ON $wpdb->pmpro_memberships_pages.membership_id = $wpdb->pmpro_membership_levels.id WHERE page_id = $post_id";
$require_metabox_levels = $wpdb->get_col($sqlQuery);
$sqlQuery = "SELECT * FROM $wpdb->pmpro_membership_levels ORDER BY id ASC";
$all_levels = $wpdb->get_results($sqlQuery, OBJECT);
$category_levels = array();
foreach($all_levels as $level)
{
$protectedcategories = $wpdb->get_col("SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE membership_id = $level->id");
//See if this post is in any of the level's protected categories
if(in_category($protectedcategories, $post_id))
{
$category_levels[] = $level->id;
}
}
$restricted = array_unique(array_merge($require_metabox_levels, $category_levels), SORT_REGULAR);
switch( $column_name )
{
case 'pmpro_membership_levels':
echo '<div id="pmpro_membership_levels-' . $post_id . '"> ';
foreach($restricted as $id)
{
echo '<span id = "pmpro-level-'.$id.'">';
$l = pmpro_getLevel($id);
echo $id. "</span>-". $l->name."<br>";
}
echo '</div>';
break;
}
}
add_action( 'manage_posts_custom_column', 'pmprobe_manage_posts_custom_column', 10, 2 );
add_action( 'manage_pages_custom_column', 'pmprobe_manage_posts_custom_column', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment