Forked from strangerstudios/requires_membership_posts_columns.php
Last active
January 10, 2020 17:44
-
-
Save kimwhite/a5baeb6ea810ff4bf915d2a78e38f761 to your computer and use it in GitHub Desktop.
Add a "Requires Membership" column to the All Posts dashboard page to show what levels are required to view.
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 | |
/** | |
* Add a Requires Membership Column to All Post view | |
* This is an easy way to see how your content is being restricted for members. | |
* | |
* 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/ | |
*/ | |
//add a new column to the all posts view | |
function requires_membership_posts_columns_head( $defaults ) { | |
$defaults['requires_membership'] = 'Requires Membership'; | |
return $defaults; | |
} | |
//get the column data | |
function requires_membership_posts_columns_content( $column_name, $post_ID ) { | |
if ($column_name == 'requires_membership') { | |
global $membership_levels, $wpdb; | |
$post_levels = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post_ID}'"); | |
$protected_levels = array(); | |
foreach($membership_levels as $level) | |
{ | |
$protectedcategories = $wpdb->get_col("SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE membership_id = $level->id"); | |
if( | |
in_array( $level->id, $post_levels ) || | |
in_category($protectedcategories, $post_ID) | |
) { | |
$protected_levels[] = $level->name; | |
} | |
} | |
echo implode( ', ', $protected_levels); | |
} | |
} | |
add_filter( 'manage_posts_columns', 'requires_membership_posts_columns_head' ); | |
add_action( 'manage_posts_custom_column', 'requires_membership_posts_columns_content', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment