Back to Community

How to Duplicate Posts from the WordPress Frontend and Customize the Process

28 threads Sep 16, 2025 PluginYoast duplicate post

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 with duplicate_post_pre_copy to 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_post hook 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