Skip to content

Bulk restrict content recipes #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions restricting-content/bulk-assign-membership-levels-posts-pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Bulk assign membership levels to posts and pages.
*
* title: Bulk assign membership levels to posts and pages.
* layout: snippet
* collection: restricting-content
* category: restricting-content, bulk update
* link: https://www.paidmembershipspro.com/restrict-access-bulk-methods/
*
* You will need to adjust the $levels to your desired levels as well as
* select your desired $post_type.
* Onced added to your site add the following to the end of yout URL
* in order ro run the script `/wp-admin/?assign_levels_posts=true`
*
* 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 mypmpro_bulk_assign_levels_to_posts(){

if( isset( $_REQUEST['assign_levels_posts'] ) ){

global $wpdb;

$levels = array( 1, 2 ); //Assign these levels to each post. Will override exsising restrictions.

$post_type = 'post'; //Change to your preference

$args = array(
'post_type' => $post_type,
'posts_per_page' => -1
);

$the_query = new WP_Query( $args );

if( $the_query->have_posts() ){
while( $the_query->have_posts() ){
$the_query->the_post();

$post_id = get_the_ID();

//remove all memberships for this page
$wpdb->query("DELETE FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '$post_id'");

//add new memberships for this page
if(is_array($levels))
{
foreach($levels as $level){
$sql = "INSERT INTO {$wpdb->pmpro_memberships_pages} (membership_id, page_id) VALUES('" . intval($level) . "', '" . intval($post_id) . "')";
echo $sql."<br/>";
$wpdb->query($sql);
}
}

}
} else {
echo "Nothing Found";
}
exit();

}

}
add_action( 'admin_init', 'mypmpro_bulk_assign_levels_to_posts' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Duplicating Membership Requirements Across Levels
*
* title: Duplicating Membership Requirements Across Levels
* layout: snippet
* collection: restricting-content
* category: restricting-content, bulk update, sql
* link: https://www.paidmembershipspro.com/restrict-access-bulk-methods/
*
* This is a SQL query and should not be added to your theme or site as PHP code.
* Instead, you can run this query using a database management tool like phpMyAdmin
* or through your hosting control panel's database access (e.g. cPanel > phpMyAdmin).
* Always make a full database backup before running SQL queries on your live site.
*/

/** Example SQL to duplicate restrictions for level ID 1 to also restrict for level ID 2 */
INSERT IGNORE INTO wp_pmpro_memberships_pages (membership_id, page_id)
SELECT '2', page_id FROM wp_pmpro_memberships_pages WHERE membership_id = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* The recipe below will replicate parent page membership requirements the recipe below will lock all pages that
* are a child of a specific page (in this case, page ID 123).
*
* title: Copy page restrictions from parent pages to child pages.
* layout: snippet
* collection: restricting-content
* category: restricting-content, pages, bulk update, sql
* link: https://www.paidmembershipspro.com/restrict-access-bulk-methods/
*
* This is a SQL query and should not be added to your theme or site as PHP code.
* Instead, you can run this query using a database management tool like phpMyAdmin
* or through your hosting control panel's database access (e.g. cPanel > phpMyAdmin).
* Always make a full database backup before running SQL queries on your live site.
*/

/** For this recipe to work, you must first add level requirements via
the Pages > Edit Page > “Require Membership” metabox to the appropriate parent page.
Change the ID on line 32 to your Parent Page ID

Copy page restrictions from parent pages to child pages.
NOTE that this doesn't delete any existing restrictions for the child pages. */

/* Copy page restrictions from parent page to child pages.
NOTE that this doesn't delete any existing restrictions for the child pages.*/

INSERT IGNORE INTO wp_pmpro_memberships_pages (membership_id, page_id)
SELECT mp.membership_id, p.ID
FROM wp_posts p
LEFT JOIN wp_pmpro_memberships_pages mp ON p.post_parent = mp.page_id
WHERE mp.membership_id IS NOT NULL
AND p.post_type IN ( 'page' )
AND p.post_parent = 123; -- Change the p.post_parent here to match the parent ID.
28 changes: 28 additions & 0 deletions restricting-content/sql-bulk-parent-restrictions-to-children.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* The recipe below will replicate parent page membership requirements across all child pages in your site.
*
* title: Copy page restrictions from parent pages to child pages.
* layout: snippet
* collection: restricting-content
* category: restricting-content, pages, bulk update, sql
* link: https://www.paidmembershipspro.com/restrict-access-bulk-methods/
*
* This is a SQL query and should not be added to your theme or site as PHP code.
* Instead, you can run this query using a database management tool like phpMyAdmin
* or through your hosting control panel's database access (e.g. cPanel > phpMyAdmin).
* Always make a full database backup before running SQL queries on your live site.
*/

/** For this recipe to work, you must first add level requirements via
the Pages > Edit Page > “Require Membership” metabox to the appropriate parent pages.

Copy page restrictions from parent pages to child pages.
NOTE that this doesn't delete any existing restrictions for the child pages. */

INSERT IGNORE INTO wp_pmpro_memberships_pages (membership_id, page_id)
SELECT mp.membership_id, p.ID
FROM wp_posts p
LEFT JOIN wp_pmpro_memberships_pages mp ON p.post_parent = mp.page_id
WHERE mp.membership_id IS NOT NULL
AND p.post_type IN ( 'page' )
AND p.post_parent <> p.ID;
21 changes: 21 additions & 0 deletions restricting-content/sql-bulk-restict-post-level.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Example SQL to add restriction to all 'posts' for Level ID 2. Change level ID for your needs.
*
* title: Add restriction to all 'posts' for Level ID 2.
* layout: snippet
* collection: restricting-content
* category: restricting-content, bulk update, sql
* link: https://www.paidmembershipspro.com/restrict-access-bulk-methods/
*
* Change level ID for your needs.
*
* This is a SQL query and should not be added to your theme or site as PHP code.
* Instead, you can run this query using a database management tool like phpMyAdmin
* or through your hosting control panel's database access (e.g. cPanel > phpMyAdmin).
* Always make a full database backup before running SQL queries on your live site.
*/

/* Example SQL to add restriction to all 'posts' for Level ID 2. Change level ID for your needs. */

INSERT IGNORE INTO wp_pmpro_memberships_pages (membership_id, page_id)
SELECT 2, ID FROM wp_posts WHERE post_type = 'post';