Forked from greathmaster/pmpro-display-membership-levels-for-posts.php
Last active
November 12, 2020 18:53
-
-
Save ronalfy/94afc54cd12bb6ae153381908f39e2e5 to your computer and use it in GitHub Desktop.
PMPro Display membership levels for posts and pages
This file contains hidden or 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 | |
/** | |
* Show levels for a post/page in the admin. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
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