Skip to content

Instantly share code, notes, and snippets.

@messica
Last active August 4, 2016 19:59
Show Gist options
  • Save messica/9627536 to your computer and use it in GitHub Desktop.
Save messica/9627536 to your computer and use it in GitHub Desktop.
Copy membership post requirements from one level to another.
<?php
/*
* Copy Membership Requirements from one level to another
*
* Instructions:
* 1. Set $copy_from and $copy_to to the level IDs you want
* 2. Visit any page of your site and add "&copy_membership=1" to the address.
*
*/
function pmpro_copy_membership_requirements() {
//only let admins continue
if(!current_user_can('manage_options'))
return false;
if(!empty($_REQUEST['copy_membership'])) {
global $wpdb;
$copy_from = 1; //level id to copy from
$copy_to = 2; //level id to copy to
$rows_to_copy = $wpdb->get_results(
"
SELECT *
FROM $wpdb->pmpro_memberships_pages
WHERE membership_id = {$copy_from}
", ARRAY_A
);
foreach ($rows_to_copy as $row) {
$success = $wpdb->replace($wpdb->pmpro_memberships_pages, array(
'membership_id' => $copy_to,
'page_id' => $row['page_id']
));
if(!empty($success))
echo "added membership level {$copy_to} to post {$row['page_id']}...<br>";
}
die();
}
}
add_action('init', 'pmpro_copy_membership_requirements');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment