Problem: I have a CRED form that allows users to clone a post by clicking the "Submit" button. This works, but I want the post's URL to change. I want the URL to be the same as the original post, but I want to append "-" and the post ID at the end.
Solution: Use the cred_save_data hook to modify the post after it's created. Tweak the slug, update the post, and copy any postmeta over into the duplicated post.
add_action('cred_save_data_22086', 'duplicate_post', 10, 2); function duplicate_post($post_id, $form_data) { // get data of original post $post = get_post( $_POST['wpcf-container_id'] ); $orig_slug = $post->post_name; $orig_id = $post->ID; $pos = strpos( $orig_slug, '-' . $orig_id); if ( $pos > -1 ) { // the -ID is part of the slug, so we need to get rid of that $orig_slug = substr( $orig_slug, 0, $pos); } $new_slug = $orig_slug . '-' . $post_id; // update the new post ID and slug $post->ID = $post_id; $post->post_name = $new_slug; wp_update_post( $post ); // get fields of original post $fields = get_post_custom( $_POST['wpcf-container_id'] ); // update the new post with these fields foreach ($fields as $key => $values) { foreach ($values as $value) { add_post_meta($post_id, $key, $value, false); } } }
Relevant Documentation:
http://php.net/manual/en/function.strpos.php
http://php.net/manual/en/function.substr.php
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/get_post
https://developer.wordpress.org/reference/functions/get_post_custom/
https://developer.wordpress.org/reference/functions/wp_update_post/
https://codex.wordpress.org/Function_Reference/add_post_meta
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 15 replies, has 2 voices.
Last updated by 7 years, 3 months ago.
Assisted by: Christian Cox.