How to Duplicate Posts from the WordPress Frontend and Customize the Process
Content
Many WordPress users want to clone posts directly from their site's frontend, but run into issues like being redirected to the admin area or needing to customize the cloned content. This guide explains the common challenges and provides solutions using the 'Yoast Duplicate Post' plugin's built-in functions and hooks.
The Core Problem: Frontend Duplication and Redirects
The primary issue users face is that the standard frontend cloning methods, such as the duplicate_post_clone_post_link() template tag or a manually constructed admin URL, automatically redirect the user to the WordPress backend after duplication. This is the plugin's default behavior, mimicking the "Copy to a new draft" admin action.
Solution 1: Use the Built-in Function (For Programmatic Cloning)
If you need to trigger duplication from your own code (e.g., via a custom form or button), you can use the duplicate_post_create_duplicate() function. This function accepts a post ID and a status, and returns the ID of the new cloned post.
$new_post_id = duplicate_post_create_duplicate( $original_post_id, 'draft' );
Important Note: This function is typically only loaded in the WordPress admin. To use it on the frontend, you may need to ensure the plugin's functions are loaded by including the necessary admin file or checking if the function exists first.
Solution 2: Customize the Process with Action Hooks
The most powerful way to customize duplication is by using the action hooks provided by the plugin. These hooks allow you to run your own code at specific points in the cloning process.
Common Use Cases for Hooks:
- Copying data from custom database tables (not post meta).
- Modifying the new post's title, status, or permalink dynamically.
- Setting specific meta values on the cloned post.
- Implementing a custom redirect after cloning.
Key Action Hooks:
dp_duplicate_post/dp_duplicate_page: Fired after the post and its meta data have been saved to the database. This is the best hook for most modifications.duplicate_post_post_copy: Intended to be used withduplicate_post_pre_copyto enable/disable features during the copy process.
Example: Redirect to Frontend and Set Status to Published
This code snippet uses the dp_duplicate_post hook to change the new post's status to 'publish' and then redirect the user to a frontend URL instead of the admin.
add_action( 'dp_duplicate_post', 'my_custom_after_clone_action', 100, 3 );
function my_custom_after_clone_action( $new_post_id, $original_post, $status ) {
// Update the status of the new post to 'publish'
wp_update_post( array(
'ID' => $new_post_id,
'post_status' => 'publish'
));
// Redirect to a frontend page, perhaps the new post itself
wp_redirect( get_permalink( $new_post_id ) );
exit;
}
Example: Copy Data from a Custom Table
add_action( 'dp_duplicate_post', 'copy_custom_table_data', 9, 2 );
function copy_custom_table_data( $new_post_id, $post ) {
$old_post_id = $post->ID;
// Your custom function logic to query and insert data from your custom table goes here
}
Solution 3: Modify the Clone Link (For Simple Redirects)
If your only goal is to change the redirect URL for the duplicate_post_clone_post_link(), you can use the duplicate_post_get_clone_post_link filter to modify the URL before it is generated. This is more lightweight but offers less control than the action hooks.
Important Considerations and Troubleshooting
- User Permissions: Ensure the user role has the correct "Copy" permissions enabled in Settings > Duplicate Post > Permissions.
- Post Type Support: Verify the post type is enabled for duplication in the plugin's settings.
- Loading Functions: The
duplicate_post_create_duplicate()function may not be available on the frontend by default. You might need to check if it exists or manually include the plugin's admin file. - ACF Fields: If you need to interact with Advanced Custom Fields data, use the
dp_duplicate_posthook with a priority higher than 10 to ensure ACF has finished saving its meta data to the new post.
By leveraging these hooks and functions, you can greatly extend the 'Yoast Duplicate Post' plugin to fit complex frontend workflows without modifying the plugin's core code.
Related Support Threads Support
-
Front-end clone & Edit optionhttps://wordpress.org/support/topic/front-end-clone-edit-option/
-
Bulk action on frontendhttps://wordpress.org/support/topic/bulk-action-on-frontend/
-
Change specific meta_key to different value, dont direct user into adminhttps://wordpress.org/support/topic/change-specific-meta_key-to-different-value-dont-direct-user-into-admin/
-
Clone page link on frontendhttps://wordpress.org/support/topic/clone-page-link-on-frontend/
-
Calling plugin action using PHPhttps://wordpress.org/support/topic/calling-plugin-action-using-php/
-
Redirected to backend when trying to duplicate post on frontendhttps://wordpress.org/support/topic/redirected-to-backend-when-trying-to-duplicate-post-on-frontend/
-
Frontend Duplicate For Spesific User Rolehttps://wordpress.org/support/topic/frontend-duplicate-for-spesific-user-role/
-
FrontEnd post clonehttps://wordpress.org/support/topic/frontend-post-clone/
-
How to duplicate related posts (like you duplicate child pages)https://wordpress.org/support/topic/how-to-duplicate-related-posts-like-you-duplicate-child-pages/
-
Action hook after clone is completehttps://wordpress.org/support/topic/action-hook-after-clone-is-complete/
-
Duplicate Post from front endhttps://wordpress.org/support/topic/duplicate-post-from-front-end/
-
clone or draft from frontendhttps://wordpress.org/support/topic/clone-or-draft-from-frontend/
-
duplicate a post from front endhttps://wordpress.org/support/topic/duplicate-a-post-from-front-end/
-
Clone and Delete originalhttps://wordpress.org/support/topic/clone-and-delete-original/
-
Custom Redirect after duplicate_post_clone_post_linkhttps://wordpress.org/support/topic/custom-redirect-after-duplicate_post_clone_post_link/
-
Duplicate Post via theme function or phphttps://wordpress.org/support/topic/duplicate-post-via-theme-function-or-php/
-
Frontend redirecthttps://wordpress.org/support/topic/frontend-redirect/
-
Custom plugin with own db table, not using post_meta to store datahttps://wordpress.org/support/topic/custom-plugin-with-own-db-table-not-using-post_meta-to-store-data/
-
Change Link Classhttps://wordpress.org/support/topic/change-link-class/
-
Cloning in front endhttps://wordpress.org/support/topic/cloning-in-front-end/
-
Action hooks? I want to modify cloned post permalink.https://wordpress.org/support/topic/action-hooks-i-want-to-modify-cloned-post-permalink/
-
Uncaught Error: Call to undefined methodhttps://wordpress.org/support/topic/uncaught-error-call-to-undefined-method-2/
-
One Click Copyhttps://wordpress.org/support/topic/one-click-copy/
-
[Plugin: Duplicate Post] auto-clone via template??https://wordpress.org/support/topic/plugin-duplicate-post-auto-clone-via-template/
-
new post id in duplicate_post_post_copyhttps://wordpress.org/support/topic/new-post-id-in-duplicate_post_post_copy/
-
Dynamic creation of the title of the cloned posthttps://wordpress.org/support/topic/dynamic-creation-of-the-title-of-the-cloned-post/
-
new post id in duplicate_post_post_copyhttps://wordpress.org/support/topic/new-post-id-in-duplicate_post_post_copy-2/
-
Front-end duplicationhttps://wordpress.org/support/topic/front-end-duplication/