How to Create Duplicate Posts Without Plugins In WordPress

In the present day, i we are going to present you easy methods to create duplicate posts with out plugins in wordpress. This text provides you with easy instance of easy methods to create duplicate posts with out plugins in wordpress. you’ll discover ways to create duplicate posts with out plugins in wordpress. So let’s observe few step to create instance of easy methods to create duplicate posts with out plugins in wordpress.

Copy the code and add it to your capabilities.php file:


/*
 * Perform creates put up duplicate put up 
 */
perform rd_duplicate_post_as_draft(){
	international $wpdb;
	if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
		wp_die('No put up to duplicate has been provided!');
	}
	/*
	 * Nonce verification
	 */
	if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
		return;
 
	/*
	 * get the unique put up id
	 */
	$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
	/*
	 * and all the unique put up information then
	 */
	$put up = get_post( $post_id );
 
	/*
	 * 
	 */
	$current_user = wp_get_current_user();
	$new_post_author = $current_user->ID;
 
	/*
	 * if put up information exists, create the put up duplicate
	 */
	if (isset( $put up ) && $put up != null) {
 
		/*
		 * new put up information array
		 */
		$args = array(
			'comment_status' => $post->comment_status,
			'ping_status'    => $post->ping_status,
			'post_author'    => $new_post_author,
			'post_content'   => $post->post_content,
			'post_excerpt'   => $post->post_excerpt,
			'post_name'      => $post->post_name,
			'post_parent'    => $post->post_parent,
			'post_password'  => $post->post_password,
			'post_status'    => 'draft',
			'post_title'     => $post->post_title,
			'post_type'      => $post->post_type,
			'to_ping'        => $post->to_ping,
			'menu_order'     => $post->menu_order
		);
 
		/*
		 * insert the put up by wp_insert_post() perform
		 */
		$new_post_id = wp_insert_post( $args );
 
		/*
		 * get all present put up phrases advert set them to the brand new put up draft
		 */
		$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for put up sort, ex array("class", "post_tag");
		foreach ($taxonomies as $taxonomy) {
			$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
			wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
		}
 
		/*
		 * duplicate all put up meta simply in two SQL queries
		 */
		$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
		if (rely($post_meta_infos)!=0) {
			$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
			foreach ($post_meta_infos as $meta_info) {
				$meta_key = $meta_info->meta_key;
				if( $meta_key == '_wp_old_slug' ) proceed;
				$meta_value = addslashes($meta_info->meta_value);
				$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
			}
			$sql_query.= implode(" UNION ALL ", $sql_query_sel);
			$wpdb->question($sql_query);
		}
		/*
		 * lastly, redirect to the edit put up display for the brand new draft
		 */
		wp_redirect( admin_url( 'put up.php?motion=edit&put up=" . $new_post_id ) );
		exit;
	} else {
		wp_die("Submit creation failed, couldn't discover authentic put up: ' . $post_id);
	}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
/*
 * Add the duplicate hyperlink to motion listing for post_row_actions
 */
perform rd_duplicate_post_link( $actions, $put up ) {
	if (current_user_can('edit_posts')) {
		$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?motion=rd_duplicate_post_as_draft&put up=" . $post->ID, basename(__FILE__), "duplicate_nonce' ) . '" title="Duplicate this merchandise" rel="permalink">Duplicate</a>';
	}
	return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

Komentar

Postingan populer dari blog ini

Laravel 8 Get HTTP Hostname

JQuery Drag And Drop Menu Example

Laravel 8 Share Social Media Button