Advertisement
HarunRRayhan

Custom Metabox for WP Users

Aug 20th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1.  
  2. /**
  3.  * Add Disable Drip-Feed (LearnDash) Metabox on user profile
  4.  * @description Disable drip-feed metabox for each users.
  5.  *
  6.  * @author HarunRRayhan
  7.  */
  8.  
  9. add_action( 'show_user_profile', 'hrx_user_disable_dripfeed' );
  10. add_action( 'edit_user_profile', 'hrx_user_disable_dripfeed' );
  11.  
  12. function hrx_user_disable_dripfeed($user) {
  13.     // Check if user is Admin
  14.     if( !current_user_can('manage_options', $user->ID) ) {
  15.         return;
  16.     }
  17.     // Get Already saved disabled drip courses ID
  18.     $disabled_drip_courses_id = get_the_author_meta( 'disabled_drip_courses', $user->ID );
  19.     ?>
  20.     <h3><?php _e("Disable Drip-Feed for Courses", "divi"); ?></h3>
  21.    
  22.     <table class="form-table">
  23.         <tr>
  24.             <!-- <th>
  25.                 <label for="disabled-drip-courses"><?php _e("Course List"); ?></label>
  26.             </th> -->
  27.             <td>
  28.                 <!-- <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br /> -->
  29.                 <?php
  30.  
  31.                 // Check if LearnDash exists
  32.                 if ( post_type_exists( 'sfwd-courses' ) ) {
  33.  
  34.                     // Course Query
  35.                     $course_args = array(
  36.                         'posts_per_page'   => -1,
  37.                         'orderby'          => 'date',
  38.                         'order'            => 'DESC',
  39.                         'post_type'        => 'sfwd-courses',
  40.                         'post_status'      => 'publish',
  41.                         'suppress_filters' => true
  42.                     );
  43.                     $courses_array = get_posts( $course_args );
  44.                    
  45.                     // Check if there any course available
  46.                     if($courses_array ):
  47.                         foreach ($courses_array as $course ) {
  48.                             setup_postdata( $course );
  49.                             // echo "<pre>";
  50.                             // print_r($course);
  51.                             // echo "</pre>";
  52.                             echo '<input type="checkbox" name="disabled-drip-courses[]" value="' . $course->ID . '" id="course-' . $course->ID  . '" ' . ((in_array($course->ID, $disabled_drip_courses_id)) ? 'checked' : '') . '> <label for="course-' . $course->ID  . '">' . $course->post_title . '</label><br>';
  53.                         }
  54.                         wp_reset_postdata();
  55.                     ?>
  56.  
  57.  
  58.                     <span class="description"><?php _e("Please select course which you want to disable for driping. Multiple courses allowed."); ?></span>
  59.                     <?php
  60.                    
  61.                     else:
  62.                         // If no couse
  63.                         ?>
  64.                     <p>
  65.                         <span class="description"><?php _e("There is no course in LearnDash. Please add a course first."); ?></span>
  66.                     </p>
  67.                 <?php
  68.                     endif;    
  69.                 } ?>
  70.             </td>
  71.         </tr>
  72.     </table>
  73.  
  74.     <?php
  75. }
  76.  
  77. /**
  78.  * Saving disabled courses ID
  79.  *
  80.  * @author HarunRRayhan
  81.  */
  82. add_action( 'personal_options_update', 'save_disabled_drip_courses_id' );
  83. add_action( 'edit_user_profile_update', 'save_disabled_drip_courses_id' );
  84. function save_disabled_drip_courses_id( $user_id ) {
  85.  
  86.     if ( !current_user_can('manage_options', $user_id ) ) { return false; }
  87.     update_user_meta( $user_id, 'disabled_drip_courses', $_POST['disabled-drip-courses'] );
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement