Forked from kimwhite/add-custom-column-to-post-page-list.php
Created
November 26, 2025 21:05
-
-
Save MaryOJob/b118b991546b95eec8d5d9bebd612884 to your computer and use it in GitHub Desktop.
Add a "Requires Membership" column to the All Pages and All Post dashboard page to show what levels are required to view. Raw
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 the Posts and Pages lists in the WordPress admin. | |
| * | |
| * title: Add a “Requires Membership” Column to Posts and Pages | |
| * - shows which membership levels are required to view each post or page. | |
| * layout: snippet | |
| * collection: admin-pages | |
| * category: admin | |
| * link: TBD | |
| * | |
| * 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 pages and posts views | |
| function requires_membership_columns_head( $defaults ) { | |
| $defaults['requires_membership'] = 'Required Membership'; | |
| return $defaults; | |
| } | |
| // Get the column data for pages and posts | |
| function requires_membership_columns_content( $column_name, $post_ID ) { | |
| if ( $column_name == 'requires_membership' ) { | |
| global $membership_levels, $wpdb; | |
| // Retrieve membership levels protecting the content | |
| $post_levels = $wpdb->get_col( | |
| $wpdb->prepare( | |
| "SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = %d", | |
| $post_ID | |
| ) | |
| ); | |
| $protected_levels = array(); | |
| if ( ! empty( $post_levels ) ) { | |
| foreach ( $membership_levels as $level ) { | |
| if ( in_array( $level->id, $post_levels ) ) { | |
| $protected_levels[] = $level->name; | |
| } | |
| } | |
| } | |
| // Display the membership levels or 'None' | |
| echo ! empty( $protected_levels ) ? implode( ', ', $protected_levels ) : 'None'; | |
| } | |
| } | |
| // Add column for pages | |
| add_filter( 'manage_pages_columns', 'requires_membership_columns_head' ); | |
| add_action( 'manage_pages_custom_column', 'requires_membership_columns_content', 10, 2 ); | |
| // Add column for posts | |
| add_filter( 'manage_posts_columns', 'requires_membership_columns_head' ); | |
| add_action( 'manage_posts_custom_column', 'requires_membership_columns_content', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment