Title: Calling plugin action using PHP
Last modified: August 5, 2021

---

# Calling plugin action using PHP

 *  Resolved [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php-2/)
 * Hi,
 * How can I duplicate a post (_using this plugin_) using PHP? Thank you!

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

 *  [duplicatepagesupport](https://wordpress.org/support/users/duplicatepagesupport/)
 * (@duplicatepagesupport)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php-2/#post-14749746)
 * Hey [@ayrancd](https://wordpress.org/support/users/ayrancd/)
 * There are 2 options for duplicate post using duplicate page plugin
 * 1. Open All posts -> On hover on post there is “”Duplicate this”” option, click
   on it for duplicate post
 * 2. Open All posts -> Click on edit post which you want to duplicate -> in editor
   page there is “”Duplicate this”” option in right side above update button click
   on it to duplicate post
 * You can also change its settings from, Duplicate page plugin tab -> setting ”
 * Regards,
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php-2/#post-14750053)
 * [@duplicatepagesupport](https://wordpress.org/support/users/duplicatepagesupport/)
   you didn’t read my question. I needed a solution using PHP and I found it:
 *     ```
       function duplicatePost($post_id, $title, $block, $execution){
       	$post = get_post($post_id);
       	$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'     => $title,
       	      'post_type'      => $post->post_type,
       	      'to_ping'        => $post->to_ping,
       	      'menu_order'     => $post->menu_order
           	);
           	$new_post_id = wp_insert_post($args);
   
           	global $wpdb;
           	$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM ydo_postmeta WHERE post_id = $post_id");
        	if (count($post_meta_infos)!=0){
        		$sql_query = "INSERT INTO ydo_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' ) continue;
       	        		$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->query($sql_query);
        	}
           	return $new_post_id;
       }
       ```
   
 * We don’t nee a plugin to use it.

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

The topic ‘Calling plugin action using PHP’ is closed to new replies.

 * ![](https://ps.w.org/duplicate-page/assets/icon-128x128.jpg?rev=1412874)
 * [Duplicate Page](https://wordpress.org/plugins/duplicate-page/)
 * [Support Threads](https://wordpress.org/support/plugin/duplicate-page/)
 * [Active Topics](https://wordpress.org/support/plugin/duplicate-page/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/duplicate-page/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/duplicate-page/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * Last activity: [4 years, 9 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php-2/#post-14750053)
 * Status: resolved