I have a form built with CRED which creates a CPT I call Event.
When the Event is published and the User is sent to view the single Event page, I would like to add a button which allows the User to Publish a similar Event. For example, the button will link back to the CRED form, but this time the fields will be prefilled with the same information as the Event that has just been published.
Basically, I would like the User to be able to post the event again for another date - if it's a regular Event.
I'm open to other suggestions to make this work, but I'm not the most advanced coder!
1. Add following code to your theme's functions.php (at end):
add_shortcode("post-data", "post_data");
function post_data($atts) {
$field = $atts["field"];
// Fetch post id from URL parameter post_id (i.e. /?post_id=1)
$post_id = $_REQUEST["post_id"];
$mypost = get_post($post_id);
$content = "";
switch($field) {
case "title":
$content = $mypost->post_title;
break;
case "body":
$content = $mypost->post_content;
break;
case "author":
$content = $mypost->post_author;
break;
// and so on...
default: // Otherwise consider $field as custom field (i.e. wpcf-myfield)
$content = get_post_meta($post_id, $field, true);
break;
}
return $content;
}
This will create a short code [post-data] which will accept 1 parameters, Field (to retrieve) and the post id from URL parameter. See usage in 3rd step below.
2. When your form is posted and you present a button or link to go back to the previous form. Make sure that link includes post_id=<PostID> as a URL parameter, such as:
[/php] hidden link
[/php]
Where 99 is the ID of newly (just) created post.
3. Use the short code in the "value" parameter of the CRED form field, such as:
When the link is hit from #2 above (containing the post_id=xx in URL as Parameter), the [post-data] short code will be executed to grab the provided field='...' and short code function will fetch the post_id from URL query string.
case "price":
$content = get_post_meta($post_id, "wpcf-event-entry-price", true);
break;
Since 'event-entry-price' is a custom field, it will not be available with $mypost. You need to grab it via get_post_meta() function. Please also notice, that if you added this custom field using Types, you may want to refer it as 'wpcf-event-entry-price'.
Image and taxonomy fields are complex field and I am afraid those can not be pre-populated easily. That may require a lot of custom work. I will suggest to contact our Certified Partners at https://toolset.com/consultant/ for this purpose. They can help you building advanced custom code in this regard.