Title: Bulk clone solution
Last modified: August 21, 2016

---

# Bulk clone solution

 *  [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/bulk-clone-solution/)
 * Hi Attached in a piece of code I use for bulk clone of a post type called “class_schedule”
 * You can remove that post_type check to use it everywhere. I did not made the 
   class, just implemented it to the plugin for a client site.
 *     ```
       /**
        * Timersys - www.timersys.com
        * Add the option to bulk clone a set of posts. Thanks to:
        *     http://www.foxrunsoftware.net/articles/wordpress/add-custom-bulk-action/
        *	Copyright: © 2012 Justin Stern (email : justin@foxrunsoftware.net)
        *	License: GNU General Public License v3.0
        *	License URI: http://www.gnu.org/licenses/gpl-3.0.html
       */
   
       if (!class_exists('FRS_Custom_Bulk_Action')) {
   
       	class FRS_Custom_Bulk_Action {
   
       		public function __construct() {
   
       			if(is_admin()) {
       				// admin actions/filters
       				add_action('admin_footer-edit.php', array(&$this, 'custom_bulk_admin_footer'));
       				add_action('load-edit.php',         array(&$this, 'custom_bulk_action'));
       				add_action('admin_notices',         array(&$this, 'custom_bulk_admin_notices'));
       			}
       		}
   
       		/**
       		 * Step 1: add the custom Bulk Action to the select menus
       		 */
       		function custom_bulk_admin_footer() {
       			global $post_type;
   
       			if($post_type == 'class_schedule') {
       				?>
       					<script type="text/javascript">
       						jQuery(document).ready(function() {
       							jQuery('<option>').val('clone').text('<?php _e('Clone')?>').appendTo("select[name='action']");
       							jQuery('<option>').val('clone').text('<?php _e('Clone')?>').appendTo("select[name='action2']");
       						});
       					</script>
       				<?php
       	    	}
       		}
   
       		/**
       		 * Step 2: handle the custom Bulk Action
       		 *
       		 * Based on the post http://wordpress.stackexchange.com/questions/29822/custom-bulk-action
       		 */
       		function custom_bulk_action() {
       			global $typenow;
       			$post_type = $typenow;
   
       			if($post_type == 'class_schedule') {
   
       				// get the action
       				$wp_list_table = _get_list_table('WP_Posts_List_Table');  // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc
       				$action = $wp_list_table->current_action();
   
       				$allowed_actions = array("clone");
       				if(!in_array($action, $allowed_actions)) return;
   
       				// security check
       				check_admin_referer('bulk-posts');
   
       				// make sure ids are submitted.  depending on the resource type, this may be 'media' or 'ids'
       				if(isset($_REQUEST['post'])) {
       					$post_ids = array_map('intval', $_REQUEST['post']);
       				}
   
       				if(empty($post_ids)) return;
   
       				// this is based on wp-admin/edit.php
       				$sendback = remove_query_arg( array('cloned', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
       				if ( ! $sendback )
       					$sendback = admin_url( "edit.php?post_type=$post_type" );
   
       				$pagenum = $wp_list_table->get_pagenum();
       				$sendback = add_query_arg( 'paged', $pagenum, $sendback );
   
       				switch($action) {
       					case 'clone':
   
       						// if we set up user permissions/capabilities, the code might look like:
       						//if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
       						//	wp_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 Cloning post.') );
   
       							$exported++;
       						}
   
       						$sendback = add_query_arg( array('cloned' => $exported, 'ids' => join(',', $post_ids) ), $sendback );
       					break;
   
       					default: return;
       				}
   
       				$sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status',  'post', 'bulk_edit', 'post_view'), $sendback );
   
       				wp_redirect($sendback);
       				exit();
       			}
       		}
   
       		/**
       		 * Step 3: display an admin notice on the Posts page after exporting
       		 */
       		function custom_bulk_admin_notices() {
       			global $post_type, $pagenow;
   
       			if($pagenow == 'edit.php' && $post_type == 'class_schedule' && isset($_REQUEST['cloned']) && (int) $_REQUEST['cloned']) {
       				$message = sprintf( _n( 'Classes cloned.', '%s classes cloned.', $_REQUEST['cloned'] ), number_format_i18n( $_REQUEST['cloned'] ) );
       				echo "<div class=\"updated\"><p>{$message}</p></div>";
       			}
       		}
   
       		function perform_export($post_id) {
       			// do whatever work needs to be done
   
       			$post = get_post($post_id);
       			duplicate_post_create_duplicate($post);
       			return true;
       		}
       	}
       }
   
       new FRS_Custom_Bulk_Action();
       ?>
       ```
   
 * [http://wordpress.org/plugins/duplicate-post/](http://wordpress.org/plugins/duplicate-post/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [WPyogi](https://wordpress.org/support/users/wpyogi/)
 * (@wpyogi)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/bulk-clone-solution/#post-4244819)
 * Please repost that using the code buttons or to a pastebin – so that your code
   is not corrupted by the forum’s parser –
 * [http://codex.wordpress.org/Forum_Welcome#Posting_Code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
 *  Thread Starter [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/bulk-clone-solution/#post-4244820)
 * I missed the button, thanks!
 *  [tsmulugeta](https://wordpress.org/support/users/tsmulugeta/)
 * (@tsmulugeta)
 * [12 years ago](https://wordpress.org/support/topic/bulk-clone-solution/#post-4245079)
 * I added your script to my functions.php file and it didn’t do anything?! I changed
   class_schedule to page because I want all pages cloned. Please specify how your
   script is supposed to work.
 * Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Bulk clone solution’ is closed to new replies.

 * ![](https://ps.w.org/duplicate-post/assets/icon-256x256.png?rev=2336666)
 * [Yoast Duplicate Post](https://wordpress.org/plugins/duplicate-post/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/duplicate-post/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/duplicate-post/)
 * [Active Topics](https://wordpress.org/support/plugin/duplicate-post/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/duplicate-post/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/duplicate-post/reviews/)

 * 3 replies
 * 3 participants
 * Last reply from: [tsmulugeta](https://wordpress.org/support/users/tsmulugeta/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/bulk-clone-solution/#post-4245079)
 * Status: not resolved