Advertisement
HarunRRayhan

Create & Save custom WP Metabox

Aug 6th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | None | 0 0
  1. /*
  2.     Add the PMPro meta box to a CPT
  3. */
  4. function hrx_product_meta_wrapper()
  5. {
  6.     //duplicate this row for each CPT
  7.     add_meta_box('pmpro_require_membership', 'Require Membership', 'hrx_pmpro_page_meta', 'product', 'side');  
  8. }
  9. function hrx_pmpro_cpt_init()
  10. {
  11.     if (is_admin() && function_exists('pmpro_hasMembershipLevel')){
  12.         add_action('admin_menu', 'hrx_product_meta_wrapper');
  13.          /* Save post meta on the 'save_post' hook. */
  14.         // add_action( 'save_post', 'hrx_save_product_level_meta', 10, 2 );
  15.     }
  16. }
  17. add_action("init", "hrx_pmpro_cpt_init", 20);
  18.  
  19. function hrx_pmpro_page_meta($post)
  20. {
  21.     global $membership_levels, $post, $wpdb;
  22.     $page_levels = get_post_meta( $post->ID, 'hrx_product_levels', true );
  23. ?>
  24.     <ul id="membershipschecklist" class="list:category categorychecklist form-no-clear">
  25.     <?php
  26.     // Add a nonce field so we can check for it later.
  27.     wp_nonce_field( 'global_notice_nonce', 'global_notice_nonce' );
  28.         $in_member_cat = false;
  29.         foreach($membership_levels as $level)
  30.         {
  31.     ?>
  32.         <li id="membership-level-<?php echo $level->id?>">
  33.             <label class="selectit">
  34.                 <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?>" />
  35.                 <?php
  36.                     echo $level->name;
  37.                     //Check which categories are protected for this level
  38.                     $protectedcategories = $wpdb->get_col("SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE membership_id = $level->id");  
  39.                     //See if this post is in any of the level's protected categories
  40.                     if(in_category($protectedcategories, $post->id))
  41.                     {
  42.                         $in_member_cat = true;
  43.                         echo ' *';
  44.                     }
  45.                 ?>
  46.             </label>
  47.         </li>
  48.     <?php
  49.         }
  50.     ?>
  51.     </ul>
  52.     <?php
  53.        
  54.         do_action('pmpro_after_require_membership_metabox', $post);
  55.     ?>
  56. <?php
  57. }
  58.  
  59. /**
  60.  * When the post is saved, saves our custom data.
  61.  *
  62.  * @param int $post_id
  63.  */
  64. function save_global_notice_meta_box_data( $post_id ) {
  65.  
  66.     // Check if our nonce is set.
  67.     if ( ! isset( $_POST['global_notice_nonce'] ) ) {
  68.         return;
  69.     }
  70.  
  71.     // Verify that the nonce is valid.
  72.     if ( ! wp_verify_nonce( $_POST['global_notice_nonce'], 'global_notice_nonce' ) ) {
  73.         return;
  74.     }
  75.  
  76.     // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  77.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  78.         return;
  79.     }
  80.  
  81.     // Check the user's permissions.
  82.     if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
  83.  
  84.         if ( ! current_user_can( 'edit_page', $post_id ) ) {
  85.             return;
  86.         }
  87.  
  88.     }
  89.     else {
  90.  
  91.         if ( ! current_user_can( 'edit_post', $post_id ) ) {
  92.             return;
  93.         }
  94.     }
  95.  
  96.     /* OK, it's safe for us to save the data now. */
  97.  
  98.     // Make sure that it is set.
  99.     if ( ! isset( $_POST['product_levels'] ) ) {
  100.         return;
  101.     }
  102.  
  103.     // Sanitize user input.
  104.     $my_data = $_POST['product_levels'];
  105.  
  106.     // Update the meta field in the database.
  107.     update_post_meta( $post_id, 'hrx_product_levels', $my_data );
  108. }
  109.  
  110. add_action( 'save_post', 'save_global_notice_meta_box_data' );
  111. // Testing POST Data
  112. add_action('wp_head', 'hrx_test_postdata' );
  113. function hrx_test_postdata() {
  114.     if(!is_admin()){
  115.         global $post;
  116.         echo "<pre>";
  117.         print_r(get_post_meta( $post->ID, 'hrx_product_levels'));
  118.         echo "</pre>";
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement