Saturday, October 16, 2021

Directorist | Bulk Update | Listings Expiry Date | Listings Never Expire

 

Directorist | Bulk Update | Listings Expiry Date | Listings Never Expire

It is straightforward to implement the listings expiry and never expire as a bulk upload option for the listings on the Directorist.

Anyone can do it by following the steps described below - 

Copy and paste the following code at the end of the "functions.php" currently activated theme folder.

1. functions.php

Directorist | Bulk Update | Listings Expiry Date | Listings Never Expire


2. Custom code


  1. add_filter('bulk_edit_custom_box', 'directorist_custom_box_never_expire', 10, 2);

  2. function directorist_custom_box_never_expire($column_name, $post_type)
  3. {
  4.     if (('atbdp_expiry_date' === $column_name) && (ATBDP_POST_TYPE == $post_type)) {
  5.         ?>
  6.         <fieldset class="inline-edit-col-right" style="margin-top: 0;">
  7.             <div class="misc-pub-section misc-pub-atbdp-never-expires">
  8.                 <label>
  9.                     <input type="checkbox" name="never_expire" value="1" />
  10.                     <strong>Never Expires</strong>
  11.                 </label>
  12.                 <div class="atbdp_expiry_date">
  13.                     <label><strong>Expiry Date</strong></label>
  14.                     <input type="datetime-local" name="expiry_date" />
  15.                 </div>
  16.             </div>
  17.         </fieldset>
  18. <?php
  19.     };
  20. }
  21.  
  22. add_action('save_post', 'directorist_custom_box_never_expire_save');

  23. function directorist_custom_box_never_expire_save($post_id)
  24. {
  25.  
  26.     if (!is_admin()) {
  27.         return;
  28.     }
  29.  
  30.     if (get_post_type($post_id) !== ATBDP_POST_TYPE) {
  31.         return;
  32.     }
  33.  
  34.     // if our current user can't edit this post, bail
  35.     if (!current_user_can('edit_posts')) {
  36.         return;
  37.     }
  38.  
  39.     // Make sure that it is set.
  40.     if (isset($_REQUEST['never_expire']) && !empty($_REQUEST['never_expire'])) {
  41.         update_post_meta($post_id, '_never_expire', sanitize_text_field($_REQUEST['never_expire']));
  42.     }
  43.  
  44.     if (isset($_REQUEST['expiry_date']) && !empty($_REQUEST['expiry_date'])) {
  45.         update_post_meta($post_id, '_expiry_date', sanitize_text_field($_REQUEST['expiry_date']));
  46.         update_post_meta($post_id, '_never_expire', 0);
  47.     }
  48. }
  49.  
  50. add_action('admin_footer', function(){
  51. ?>
  52.     <script>
  53.         jQuery(document).ready(function($){
  54.             $('.misc-pub-atbdp-never-expires input[name="never_expire"]').change(function(){
  55.                 if( $(this).is(":checked") ){
  56.                     $('.misc-pub-atbdp-never-expires .atbdp_expiry_date').hide();
  57.                     $('.misc-pub-atbdp-never-expires input[name="expiry_date"]').val('');
  58.                 }else{
  59.                     $('.misc-pub-atbdp-never-expires .atbdp_expiry_date').show();
  60.                 }
  61.             });
  62.         });
  63.     </script>
  64. <?php
  65. });


This should export results like the following image -

Directorist | Bulk Update | Listings Expiry Date | Listings Never Expire

Simple, is not it? Please comment below if you have any questions regarding this.

No comments:

Post a Comment