AI Verified

Name

post duplication

About

Function for post duplication. Dups appear as drafts. User is redirected to the edit screen

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

jbeer

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: Not Specified

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

14/11/2022

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

-------------------------------------------------------------------------------------------------------------
Name | Potential vulnerability found : SQL Injection
Line | 79
Code | ->query($sql_query)
Code | Line | 72 : $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ");

Name | Potential vulnerability found : Cross Site Scripting
Line | 89
Code | die('Post creation failed, could not find original post: ' . $post_id)
Code | Line | 16 : $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) ));

Found 2 vulnerabilities

post duplication

 
                    
1function rd_duplicate_post_as_draft(){
2 global $wpdb;
3 if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
4 wp_die('No post to duplicate has been supplied!');
5 }
6 
7 /*
8 * Nonce verification
9 */
10 if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
11 return;
12 
13 /*
14 * get the original post id
15 */
16 $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
17 /*
18 * and all the original post data then
19 */
20 $post = get_post( $post_id );
21 
22 /*
23 * if you don't want current user to be the new post author,
24 * then change next couple of lines to this: $new_post_author = $post->post_author;
25 */
26 $current_user = wp_get_current_user();
27 $new_post_author = $current_user->ID;
28 
29 /*
30 * if post data exists, create the post duplicate
31 */
32 if (isset( $post ) && $post != null) {
33 
34 /*
35 * new post data array
36 */
37 $args = array(
38 'comment_status' => $post->comment_status,
39 'ping_status' => $post->ping_status,
40 'post_author' => $new_post_author,
41 'post_content' => $post->post_content,
42 'post_excerpt' => $post->post_excerpt,
43 'post_name' => $post->post_name,
44 'post_parent' => $post->post_parent,
45 'post_password' => $post->post_password,
46 'post_status' => 'draft',
47 'post_title' => $post->post_title,
48 'post_type' => $post->post_type,
49 'to_ping' => $post->to_ping,
50 'menu_order' => $post->menu_order
51 );
52 
53 /*
54 * insert the post by wp_insert_post() function
55 */
56 $new_post_id = wp_insert_post( $args );
57 
58 /*
59 * get all current post terms ad set them to the new post draft
60 */
61 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
62 foreach ($taxonomies as $taxonomy) {
63 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
64 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
65 }
66 
67 /*
68 * duplicate all post meta just in two SQL queries
69 */
70 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
71 if (count($post_meta_infos)!=0) {
72 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
73 foreach ($post_meta_infos as $meta_info) {
74 $meta_key = $meta_info->meta_key;
75 if( $meta_key == '_wp_old_slug' ) continue;
76 $meta_value = addslashes($meta_info->meta_value);
77 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
78 }
79 $sql_query.= implode(" UNION ALL ", $sql_query_sel);
80 $wpdb->query($sql_query);
81 }
82 
83 
84 /*
85 * finally, redirect to the edit post screen for the new draft
86 */
87 wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
88 exit;
89 } else {
90 wp_die('Post creation failed, could not find original post: ' . $post_id);
91 }
92}
93add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
94 
95/*
96 * Add the duplicate link to action list for post_row_actions
97 */
98function rd_duplicate_post_link( $actions, $post ) {
99 if (current_user_can('edit_posts')) {
100 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
101 }
102 return $actions;
103}
104 
105add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

0

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

1

Hide Login Errors

Added: 1 year ago

Last Updated: 1 year ago

Hide login error making them more generic to avoid giving any hint to potential harmful attacks

WordPress Admin

AI Verified

0

Hide ‘Screen Options’ Tab

Added: 1 year ago

Last Updated: 1 year ago

WordPress Admin

AI Verified

0

Hide WP Block Editor on specific post type

Added: 1 year ago

Last Updated: 1 year ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

3

Confirm Password Field in Woocommerce

Added: 1 year ago

Last Updated: 1 day ago

WooCommerce

AI Verified

3

Redirect customers to own thank-you page after order

Added: 1 year ago

Last Updated: 1 day ago

You may want to redirect your customers to a custom thank you page after they place an order. You can do that with the following WooCommerce snippet

WooCommerce

AI Verified

2

Bypass logout confirmation.

Added: 1 year ago

Last Updated: 1 month ago