Avoid Using Plugins for WordPress Page & Post Duplication
While managing a blog in the WPlaybook community, I’ve received many questions—one of the most frequent being how to duplicate WordPress pages or posts.
Today, I’ll show you how to implement WordPress page/post duplication using code instead of relying on plugins.
For a detailed guide on duplicating WordPress pages and posts without plugins, you can check this resource.
Why You Need a WordPress Page Duplication Plugin
When building a high-performance WordPress blog, you’ll eventually realize that you frequently use the same template format for your posts.
I wish I could save my preferred template and load it whenever I need!
This thought naturally comes to mind, and that’s where the page/post duplication feature becomes incredibly useful. It allows you to effortlessly copy previously created pages or posts directly from the menu.
Pros and Cons of the Yoast Duplicate Post Plugin
Among post duplication plugins, Yoast Duplicate Post is one of the most well-known options. Once installed, it adds a “Clone” menu under the post list, making it incredibly convenient to duplicate posts
Many users rely on specific writing templates for their posts, making this plugin highly useful. If you prefer a hassle-free approach and want a simple solution, installing Yoast Duplicate Post might be the best choice.
Increasing Plugins, Slowing Down Speed
However, installing a plugin for a single function contradicts the principle of keeping plugins to a minimum for optimal WordPress performance.
The more plugins a WordPress site has, the greater the potential impact on speed. This raises the question: Is it worth installing an entire plugin just for post duplication?
Isn't it a bit of a waste to install a plugin just for one function: copying text?
Inserting a WordPress page copy function
After thinking about how to implement a WordPress page copy or article copy method, I came to the conclusion that I would implement it by installing a plugin that can insert code. The reason is as follows.
- As WordPress decoration becomes more advanced, a code insertion plugin will be installed.
- It is not a plugin for the article copy function alone. It can remove multiple plugins at the same time.
Therefore, I will introduce a method to implement a WordPress page copy or article copy function using the WPCode plugin that I chose. It passed all the tests of WP Hive Insights, which tells you how plugins affect WordPress.

Installing the WPCode Plugin
To install the WPCode plugin, go to the Plugins > Add New menu and search for “wpcode”. Install the first plugin that appears in the search results.

Setting Up Code Snippets
Now let’s learn how to set up and activate the code to copy the post.

If you follow the above method, you will see a space to create a new snippet. First, set the snippet title. I set it to “Duplicate Post/Page Link”!
Then, on the right, set the Code Type to PHP Snippet. This is because the snippet code we will paste is a programming code called PHP.
Please refer to the code below for the code to paste! Don’t be intimidated by the long code. All you need to know is that if you just copy and paste the code, the copy function will be created. This is a code that has already been tested in the WPCode plugin. (For reference, I don’t know much about PHP code either.)
Duplicate Post/Page snippet code
// Add duplicate button to post/page list of actions.
add_filter( 'post_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
// Let's make sure the function doesn't already exist.
if ( ! function_exists( 'wpcode_snippet_duplicate_post_link' ) ) {
/**
* @param array $actions The actions added as links to the admin.
* @param WP_Post $post The post object.
*
* @return array
*/
function wpcode_snippet_duplicate_post_link( $actions, $post ) {
// Don't add action if the current user can't create posts of this post type.
$post_type_object = get_post_type_object( $post->post_type );
if ( null === $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) {
return $actions;
}
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'wpcode_snippet_duplicate_post',
'post_id' => $post->ID,
),
'admin.php'
),
'wpcode_duplicate_post_' . $post->ID,
'wpcode_duplicate_nonce'
);
$actions['wpcode_duplicate'] = '<a href="' . $url . '" title="Duplicate item" rel="permalink">Duplicate</a>';
return $actions;
}
}
/**
* Handle the custom action when clicking the button we added above.
*/
add_action( 'admin_action_wpcode_snippet_duplicate_post', function () {
if ( empty( $_GET['post_id'] ) ) {
wp_die( 'No post id set for the duplicate action.' );
}
$post_id = absint( $_GET['post_id'] );
// Check the nonce specific to the post we are duplicating.
if ( ! isset( $_GET['wpcode_duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['wpcode_duplicate_nonce'], 'wpcode_duplicate_post_' . $post_id ) ) {
// Display a message if the nonce is invalid, may it expired.
wp_die( 'The link you followed has expired, please try again.' );
}
// Load the post we want to duplicate.
$post = get_post( $post_id );
// Create a new post data array from the post loaded.
if ( $post ) {
$current_user = wp_get_current_user();
$new_post = array(
'comment_status' => $post->comment_status,
'menu_order' => $post->menu_order,
'ping_status' => $post->ping_status,
'post_author' => $current_user->ID,
'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 . ' (copy)',// Add "(copy)" to the title.
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
);
// Create the new post
$duplicate_id = wp_insert_post( $new_post );
// Copy the taxonomy terms.
$taxonomies = get_object_taxonomies( get_post_type( $post ) );
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $duplicate_id, $post_terms, $taxonomy );
}
}
// Copy all the custom fields.
$post_meta = get_post_meta( $post_id );
if ( $post_meta ) {
foreach ( $post_meta as $meta_key => $meta_values ) {
if ( '_wp_old_slug' === $meta_key ) { // skip old slug.
continue;
}
foreach ( $meta_values as $meta_value ) {
add_post_meta( $duplicate_id, $meta_key, $meta_value );
}
}
}
// Redirect to edit the new post.
wp_safe_redirect(
add_query_arg(
array(
'action' => 'edit',
'post' => $duplicate_id
),
admin_url( 'post.php' )
)
);
exit;
} else {
wp_die( 'Error loading post for duplication, please try again.' );
}
} );
Setting the Location for the Code Snippet
Next, you need to specify where the code will be executed. Since the post duplication function is only for admins, select “Location” and then choose “Admin Only”.

Once this configuration is complete, click “Save Snippet” in the upper right corner to save and exit.
Verify and activate the code snippet
When you finish saving and enter the WPCode Snippets screen, you’ll see the snippet we saved listed. Now all we need to do is activate the code. Press the button on the right to activate it, as shown below!
Enable and test the Copy Posts feature
After activating the code, if you go into the list of posts on your WordPress dashboard, you’ll see that the snippet we wrote is working fine.

Closing thoughts
Today, we explored how to implement the post duplication feature using WPCode. WPCode is a powerful plugin that allows easy customization—not just for post duplication, but for various other functionalities.
Moving forward, I will continue sharing methods to implement additional features using WPCode!


