Skip Navigation

[Resolved] Need a creative solution

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 2.30 hours from now. Thank you for your understanding.

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 10 replies, has 2 voices.

Last updated by larryL 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1714873

I have read through previous support tickets on cloning posts/duplicating posts:

https://toolset.com/forums/topic/duplicate-custom-posts-at-front-end/
https://toolset.com/forums/topic/views-front-end-user-duplicate-post/
https://toolset.com/forums/topic/duplicate-post-on-front-end/

And I understand that currently Toolset does not support users duplicating a post from the front end and editing that post in CRED in one action.

What I was wondering is if there are any creative work-a-rounds for this problem? For example, I noticed that if I install Yoast's Duplicate Page plugin I can duplicate the post but then it just disappears. So, if I can figure out a way to get that post to publish using YDP or another plugin and then the user can go back and edit it in CRED, that would work for me. Or do you have any experience with an existing hook that allows me to not use a plugin (skipping that step) but still requires the user to go back, find the new duplicated post and click edit?

Perhaps the newly duplicated post could be captured by a view that only displays posts in the last x minutes? I don't know. Just trying to work around this. Ideas? Thanks!

#1715221
Screen Shot 2020-07-22 at 2.58.20 PM.png

Depending on the complexity of the post, you might be able to work around this with a "Clone post" link containing URL parameters, and a new Post Form. For example, if the post to be cloned contains a simple text custom field, you can configure a new Post Form where the custom field input responds to a URL parameter. So from post 1, with custom field value "abc", you create a link to the Post Form with URL parameters like https://yoursite.com/page-with-new-post-form/?field-slug=abc&post-title=Some%20Post%20Title
The Form loads with the custom field value and post title pre-populated, and the User must submit the Form. This creates a new post with the same custom field value. Additional URL parameters would be required to set the post title and other custom fields. It doesn't require an external plugin or any custom code beyond creating that link with URL parameters manually with a mix of HTML and shortcodes. I can help you set up that link with some examples if needed.

Some limitations to be aware of up front:
- This approach doesn't help you clone related posts or other post relationships associated with the original post, so if you're trying to clone a parent post it won't clone the child posts currently associated with that parent post. If you're trying to automatically relate the cloned post to the same posts as the original in a many-to-many relationship, it won't work. Custom code is required to do those things. If you're cloning a child post, it IS possible to automatically select the same parent post.
- This approach doesn't help you clone RFGs associated with the post being cloned. Custom code is required.
- This approach doesn't support all field types. Checkboxes and images, for example, are not supported. To determine if your field type is supported, you can check in a new Post Form created with the drag-and-drop Form builder. Toggle open the field settings, and look to see if there is an option "Set default value from a URL parameter" available (see the screenshot here). If so, this field type supports URL parameters. If not, this approach will not help you clone the value of that field.

As far as third-party plugins, I really have no idea. We're not trained on those and I don't have any experience with how they work. Since post relationship and RFG implementations are proprietary to Toolset, I can pretty much guarantee a third-party plugin will not help you clone those or create similar relationships to existing posts. That would require significant knowledge of our APIs, and I'm not aware of any plugins that claim to have such capability. A third party plugin may be able to clone custom fields by simply copying custom field values from the database, and may be able to copy taxonomy terms over to the cloned post, but again I don't know about any specific plugins so I don't have a recommendation to share.

Hope this helps!

#1715467

This is perfect! And it eliminates YATPP (Yet another Third Party Plugin).

It's the construct that I'm not clear on:

hidden link

So how do I get the link to pull the current post's (when they click 'clone' link) custom fields (other than non-supported one's values?

is it as simple as:

hidden link ....

or do I need to do something else? Also, in terms of the link that I put on the page, is this just a simple href to the above?

Thanks!

#1716215

You can use Types custom field shortcodes to place the current post's custom field values, and use other Views shortcodes to place the post's title and other fields in a link's href attribute. For example:

<a href="<em><u>hidden link</u></em> field='field-slug' output='raw'][/types]&other-field-slug=[types field='other-field-slug'][/types]&post-title=[wpv-post-title]">Create clone</a>

Types field shortcodes are documented here: https://toolset.com/documentation/customizing-sites-using-php/functions/
Other Views shortcodes, like for the post title, are documented here: https://toolset.com/documentation/user-guides/views/views-shortcodes/

This works best with simple text and numeric content. Double quote marks in these fields, for example, would break the link markup code.

#1716471

Ok, I think we're on the right path but I'm not getting it to clone. Here is what I did:

<a href="/?vin=[types field="vin" output='raw'][/types]&garage=[types field="garage" output='raw'][/types]&level=[types field="level" output='raw'][/types]&space-number=[types field="space-number" output='raw'][/types]&fleet-space-comment=[types field="fleet-space-comment" output='raw'][/types]&state=[types field="state" output='raw'][/types]&tag-number=[types field="tag-number" output='raw'][/types]&tag-agency=[types field="tag-agency" output='raw'][/types]&make=[types field="make" output='raw'][/types]&make-other=[types field="make-other" output='raw'][/types]&vehicle-model=[types field="vehicle-model" output='raw'][/types]&vehicle-notes=[types field="vehicle-notes" output='raw'][/types]&driver=[types field="driver" output='raw'][/types]&eligibility=[types field="eligibility" output='raw'][/types]&asset-id=[types field="asset-id" output='raw'][/types]&post-date=[wpv-post-date]&post-author=[wpv-post-author]&post-title=[wpv-post-title]">Clone</a>

What did I do wrong?

#1716639

Okay have you set up a Page that contains the new post Form? If so, you need to change the link to point to that page slug, and then include the URL parameters after that Page slug. Right now it looks like it's just redirecting to the homepage - maybe that is what you intended and you have a new post Form there but I'm not sure. Here is an updated link for you. You'll have to update the page slug at the beginning, or remove it to direct to the homepage like it was originally set. Notice I changed all the nested quotation marks in the Types field shortcodes to use single instead of double quotes. Nested double quotes may not parse correctly, so single quotes are better here:

<a href="/some-page-with-new-post-form/?vin=[types field='vin' output='raw'][/types]&garage=[types field='garage' output='raw'][/types]&level=[types field='level' output='raw'][/types]&space-number=[types field='space-number' output='raw'][/types]&fleet-space-comment=[types field='fleet-space-comment' output='raw'][/types]&state=[types field='state' output='raw'][/types]&tag-number=[types field='tag-number' output='raw'][/types]&tag-agency=[types field='tag-agency' output='raw'][/types]&make=[types field='make' output='raw'][/types]&make-other=[types field='make-other' output='raw'][/types]&vehicle-model=[types field='vehicle-model' output='raw'][/types]&vehicle-notes=[types field='vehicle-notes' output='raw'][/types]&driver=[types field='driver' output='raw'][/types]&eligibility=[types field='eligibility' output='raw'][/types]&asset-id=[types field='asset-id' output='raw'][/types]&post-date=[wpv-post-date]&post-author=[wpv-post-author]&post-title=[wpv-post-title]">Clone</a>

After you replace some-page-with-new-post-form with the slug of a Page containing the new post Form, you should be able to place this link code in a template for the custom post type single posts, or in a View of the custom post type to clone any of the posts in the list.

I see you have post date and post author included in the URL query string. Those two post attributes are not usually set with arbitrary input in a Form, they are set inherently using the current date/time and logged-in User information when the Form is submitted. If you want the current date and User, nothing extra is required and you can safely remove these attributes from the URL query string. If you want to customize the author and date, you must also add generic fields in the Form and add custom code using the Forms API to capture and store those values correctly using wp_update_post. There are probably some other similar tickets in the forum that set the post author or post date in Forms using custom code, I can help you find examples if necessary. Documentation for the wp_update_post function in WP:
https://developer.wordpress.org/reference/functions/wp_update_post/

#1717005

I made the changes as you suggested, removing the date & author slugs, but when the form appears none of the field date has populated.

<a href="/add-car/?vin=[types field='vin' output='raw'][/types]&garage=[types field='garage' output='raw'][/types]&level=[types field='level' output='raw'][/types]&space-number=[types field='space-number' output='raw'][/types]&fleet-space-comment=[types field='fleet-space-comment' output='raw'][/types]&state=[types field='state' output='raw'][/types]&tag-number=[types field='tag-number' output='raw'][/types]&tag-agency=[types field='tag-agency' output='raw'][/types]&make=[types field='make' output='raw'][/types]&make-other=[types field='make-other' output='raw'][/types]&vehicle-model=[types field='vehicle-model' output='raw'][/types]&vehicle-notes=[types field='vehicle-notes' output='raw'][/types]&driver=[types field='driver' output='raw'][/types]&eligibility=[types field='eligibility' output='raw'][/types]&asset-id=[types field='asset-id' output='raw'][/types]&post-title=[wpv-post-title]">Clone</a>

#1719757

when the form appears none of the field date has populated.
Okay, I'll take a closer look.
1. Can you copy the full URL produced by the link and paste it here for me to review? Not the link markup we've been working on, but the URL that is generated when you click the link.
2. Which URL parameter(s) correspond(s) to the missing date field(s)?

#1719807

hidden link

I don't understand your second question.

#1720101

I don't understand your second question.
I thought you meant there was a missing custom "date" field, and I was asking you which URL parameter corresponds to the date field. Re-reading your comment, I think you made a typo - "date" instead of "data" - and you're saying none of the data is populated in the Form, rather than just a single missing date field. Okay my guess is you have not set any of the corresponding URL parameters in the Form itself on each field, mapping the URL parameter "vin" for example to the custom field that holds the VIN number, mapping the URL parameter "post-title" to the field that holds the post title, etc. My screenshot in the comment here shows where that URL parameter must be set for each custom field in the drag-and-drop Form builder: https://toolset.com/forums/topic/need-a-creative-solution/#post-1715221
Each URL parameter must be mapped to its corresponding field in the Form builder so that field knows to grab the data from the URL.

If you are using the expert Form builder instead of the drag-and-drop Form builder, you must map the URL parameter in the urlparam shortcode attribute of the corresponding Form field, for example, for the VIN field:

[cred-field field="vin-field-slug" urlparam="vin"]

If this isn't working as expected I'll need a login and I need to know where to see the actual generated links on the front-end of the site.

#1723191

My issue is resolved now. Thank you!