Can i write custom bulk action for my custom fields in wordpress? if yes, what are the ways to do that?

I am working on wordpress website where i am generating Landing Pages dynamically. I have used custom fields such as price, background image etc. on these landing pages. Now i want to create a custom bulk action for those pages where I can update price field for all the landing pages at once. Now how do i create bulk action for that.

Related posts

Leave a Reply

1 comment

  1. Here is a the code to begin with :

    add_action('admin_footer-edit.php', 'custom_bulk_admin_footer');
     function custom_bulk_admin_footer() {
    
      global $post_type;
    
      if($post_type == 'post') {
        ?>
        <script type="text/javascript">
          jQuery(document).ready(function() {
            jQuery('<option>').val('edit_custom_field...').text('<?php _e('Update custo....')?>').appendTo("select[name='udpate...action1']");
            jQuery('<option>').val('edit_custom_field...').text('<?php _e('Update custo....')?>').appendTo("select[name='udpate...action2']");
          });
        </script>
        <?php
      }
    }
    
    
    
    add_action('load-edit.php', 'custom_bulk_action');
    
    function custom_bulk_action() {
    
      // ...
    
      // 1. get the action
      $wp_list_table = _get_list_table('WP_Posts_List_Table');
      $action = $wp_list_table->current_action();
    
      // ...
    
      // 2. security check
      check_admin_referer('bulk-posts');
    
      // ...
    
      switch($action) {
        // 3. Perform the action
        case 'export':
          // if we set up user permissions/capabilities, the code might look like:
          //if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
          //  pp_die( __('You are not allowed to export this post.') );
    
          $exported = 0;
    
          foreach( $post_ids as $post_id ) {
            if ( !$this->perform_export($post_id) )
              wp_die( __('Error exporting post.') );
            $exported++;
          }
    
          // build the redirect url
          $sendback = add_query_arg( array('exported' => $exported, 'ids' => join(',', $post_ids) ), $sendback );
    
        break;
        default: return;
      }
    
      // ...
    
      // 4. Redirect client
      wp_redirect($sendback);
    
      exit();
    }