Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Add the PMPro meta box to a CPT
- */
- function hrx_product_meta_wrapper()
- {
- //duplicate this row for each CPT
- add_meta_box('pmpro_require_membership', 'Require Membership', 'hrx_pmpro_page_meta', 'product', 'side');
- }
- function hrx_pmpro_cpt_init()
- {
- if (is_admin() && function_exists('pmpro_hasMembershipLevel')){
- add_action('admin_menu', 'hrx_product_meta_wrapper');
- /* Save post meta on the 'save_post' hook. */
- // add_action( 'save_post', 'hrx_save_product_level_meta', 10, 2 );
- }
- }
- add_action("init", "hrx_pmpro_cpt_init", 20);
- function hrx_pmpro_page_meta($post)
- {
- global $membership_levels, $post, $wpdb;
- $page_levels = get_post_meta( $post->ID, 'hrx_product_levels', true );
- ?>
- <ul id="membershipschecklist" class="list:category categorychecklist form-no-clear">
- <?php
- // Add a nonce field so we can check for it later.
- wp_nonce_field( 'global_notice_nonce', 'global_notice_nonce' );
- $in_member_cat = false;
- foreach($membership_levels as $level)
- {
- ?>
- <li id="membership-level-<?php echo $level->id?>">
- <label class="selectit">
- <input id="in-membership-level-<?php echo $level->id?>" type="checkbox" <?php if(in_array($level->id, $page_levels)) { ?>checked="checked"<?php } ?> name="product_levels[]" value="<?php echo $level->id?>" />
- <?php
- echo $level->name;
- //Check which categories are protected for this 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))
- {
- $in_member_cat = true;
- echo ' *';
- }
- ?>
- </label>
- </li>
- <?php
- }
- ?>
- </ul>
- <?php
- do_action('pmpro_after_require_membership_metabox', $post);
- ?>
- <?php
- }
- /**
- * When the post is saved, saves our custom data.
- *
- * @param int $post_id
- */
- function save_global_notice_meta_box_data( $post_id ) {
- // Check if our nonce is set.
- if ( ! isset( $_POST['global_notice_nonce'] ) ) {
- return;
- }
- // Verify that the nonce is valid.
- if ( ! wp_verify_nonce( $_POST['global_notice_nonce'], 'global_notice_nonce' ) ) {
- return;
- }
- // If this is an autosave, our form has not been submitted, so we don't want to do anything.
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
- return;
- }
- // Check the user's permissions.
- if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
- if ( ! current_user_can( 'edit_page', $post_id ) ) {
- return;
- }
- }
- else {
- if ( ! current_user_can( 'edit_post', $post_id ) ) {
- return;
- }
- }
- /* OK, it's safe for us to save the data now. */
- // Make sure that it is set.
- if ( ! isset( $_POST['product_levels'] ) ) {
- return;
- }
- // Sanitize user input.
- $my_data = $_POST['product_levels'];
- // Update the meta field in the database.
- update_post_meta( $post_id, 'hrx_product_levels', $my_data );
- }
- add_action( 'save_post', 'save_global_notice_meta_box_data' );
- // Testing POST Data
- add_action('wp_head', 'hrx_test_postdata' );
- function hrx_test_postdata() {
- if(!is_admin()){
- global $post;
- echo "<pre>";
- print_r(get_post_meta( $post->ID, 'hrx_product_levels'));
- echo "</pre>";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement