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, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/)
 * Hi,
 * Is there a way to call the duplicate action of this plugin using PHP? Like, passing
   the post ID and getting the new post draft ID? Tks.

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

 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/#post-14738763)
 * I’m trying to do something like:
 * `$rr = do_action("admin_action_duplicate_post_new_draft", 27572);`
 * But I have to pass the variable **post** with the value _27572_ using _GET_ or
   _POST_ and the variable **action** with value _duplicate\_post\_new\_draft_.
 * Is there a way to do that?
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/#post-14738862)
 * Just found out they have a function for that:
 * `$new_post_id = duplicate_post_create_duplicate(27572, "draft");`
 * But I am getting this error:
 * **Fatal error: Uncaught Error: Call to undefined function duplicate_post_create_duplicate()**
 *  Plugin Support [Michael Tiña](https://wordpress.org/support/users/mikes41720/)
 * (@mikes41720)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/#post-14740076)
 * Hi [@ayrancd](https://wordpress.org/support/users/ayrancd/)
 * Please do check out our documentation for the Yoast Duplicate Post plugin in 
   order to locate the specific filter or action you need for your use-case:
    - [https://developer.yoast.com/duplicate-post/filters-actions/](https://developer.yoast.com/duplicate-post/filters-actions/)
    - [https://developer.yoast.com/duplicate-post/functions-template-tags/](https://developer.yoast.com/duplicate-post/functions-template-tags/)
    - Thanks!
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/#post-14740159)
 * [@mikes41720](https://wordpress.org/support/users/mikes41720/) I saw that article
   but I couldn’t make the action dp_duplicate_post work…
 * Do I need the do_action as well? How do I get the new post ID? Can you pls post
   a snippet code?
 * Did you see the php error when I call that function also found in one of your
   articles??
 *  Thread Starter [Cezar Ayran](https://wordpress.org/support/users/ayrancd/)
 * (@ayrancd)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-plugin-action-using-php/#post-14742403)
 * Just found a solution without using a plugin:
 *     ```
       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;
       }
       ```
   

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

The topic ‘Calling plugin action using PHP’ 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/)

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