Duplicate Posts & Pages without a Plugin! The Simple Post Duplicator!

Duplicate Posts Pages Without A Plugin The Simple Post Duplicator

Duplicating posts or pages in WordPress used to be simple. Yea, you can install a plugin, and there are some good ones. Years ago I used to use a great free one until it got bought by a bigger company and became bloated and tried to upsell me on other plugins. So, I wrote this code. And now… everything is simple again!

Duplicate Posts Pages Without A Plugin The Simple Post Duplicator

What it does…

This is a WordPress code snippet that allows users to duplicate posts and pages seamlessly. It enables the creation of duplicate drafts, ensuring that the original content remains untouched (including taxonomy terms and post meta). This straightforward solution avoids the complexities of plugins while providing an efficient post duplication feature.

Why it does it…

This code was created as an alternative to bloated plugins that compromise simplicity and attempt to sell additional features. This Simple Post Duplicator code snippet aims to bring back the ease of duplicating posts and pages in WordPress without unnecessary complexities or upsells. It offers a lightweight and reliable solution for users who prefer a straightforward approach.

How it does it…

Orig: This code snippet gives you a PHP class (which means you can use the Code Snippets Pro ❤️ plugin or just copy/paste the code into your theme’s functions.php file) and will only run the code if the class doesn’t already exist.

This works on posts, pages and even custom post types (CPTs). It consists of two main methods:

  1. duplicatePostAsDraft(): This method handles the post duplication process. It verifies the user’s permission, checks for the presence of a post to duplicate, performs nonce verification for security, creates a duplicate draft post with identical content, taxonomy terms, and post meta information, and redirects the user to the new draft’s edit screen.
  2. addDuplicatePostLink(): This method adds a “Duplicate” link to the action list of individual posts in the WordPress admin panel. It ensures that only users with the appropriate permissions can see and use the link. Upon clicking the “Duplicate” link, the user is directed to the duplicate post’s edit screen.

Ahhh… concise and well-structured code to ensure a hassle-free post duplication experience! You’re welcome.

Use the JSON file for easily importing into your CodeSnippetsPro plugin. Use the PHP file or just copy/paste the code sample into your functions.php if you’re not using CodeSnippetsPro.

Related Snips

See the code on github…

Leave a Reply

Your email address will not be published. Required fields are marked *