Duplicate recipe programmatically
-
Hi guys,
Is there a way to duplicate a recipe from another plugin and changing just the recipe name and the post id of the forminator form in the trigger?
-
Hi @sanaconeltantra ,
It sounds like instead of using our UI to duplicate a recipe, you want to do it with code in another plugin. I checked with our dev team and they said this would be difficult right now and not something we could support. We’ll consider how we might make these easier for other developers, but for now, unfortunately, this effectively wouldn’t be possible.
We have captured your feedback, so I’ll close this request for now, and we’ll see if we can do anything here in a future release.
Hi!
Thanks for your feedback.
There is a function called copy_this_recipe. It seems to do the job?
Can you just let me know how I can call it because of the namespace?
I tried$new_recipe_id = Uncanny_Automator::copy_this_recipe($recipe_id);
but it’s still not able to find the function.
I’ll then just copy paste the function in your php file and will add some parameters to the new one.
I understand you won’t support that modification, I just want to see if that works at least on a sandbox and then ask for a custom code from your team if that’s possible because I really need this feature.Hi @sanaconeltantra , I ran it by our dev team and they said you could use this code example in your plugin or the child theme’s functions.php file to copy a recipe:
-- add_action( 'init', function () { if ( ! defined( 'UA_ABSPATH' ) ) { return; } if ( ! class_exists( '\Uncanny_Automator\Copy_Recipe_Parts' ) ) { require_once UA_ABSPATH . 'src/core/admin/class-copy-recipe-parts.php'; } $copy = new \Uncanny_Automator\Copy_Recipe_Parts(); $copy->copy_this_recipe( 7509 ); }, 1234 );
We hope that helps!
Hi there!
Thank you so much, it does work from a plugin because putting it in the init hook would create many duplicates at each page load.if ( ! class_exists( '\Uncanny_Automator\Copy_Recipe_Parts' ) ) { require_once UA_ABSPATH . 'src/core/admin/class-copy-recipe-parts.php'; } $copy = new \Uncanny_Automator\Copy_Recipe_Parts(); $id = $copy->copy_this_recipe( 2119 ); echo "new recipe id=" . $id;
Now that I get the post id of the duplicate recipe, can I :
– update post_status = publish where post_id = $id or post_parent = $id (triggers and actions)
– update postmeta for the post trigger of forminator and update FRFORM and FRFORM_readable to use the new form id and name
Or do you provide functions to do it more cleanly?Hi @sanaconeltantra ,
This one’s probably at a point where following up may be better on our website (so our developers can reply directly), but one of our developers suggested the following sample code snippet you could use to modify frform ID and publishing triggers/actions as well as the recipe:
function change_frform_id( $recipe_id ) { $form = Forminator_API::get_form( 123 ); // 123 if form ID $form_name = isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name; $recipe_data = Automator()->get_recipe_data_by_recipe_id( $recipe_id ); $triggers = $recipe_data['triggers']; if ( $triggers ) { foreach ( $triggers as $trigger ) { $trigger_id = $trigger['ID']; if ( ! empty( get_post_meta( $trigger_id, 'FRFORM', true ) ) ) { update_post_meta( $trigger_id, 'FRFORM', 123 ); update_post_meta( $trigger_id, 'FRFORM_readable', $form_name ); } } } } function change_recipe_to_live( $recipe_id ) { $recipe_data = Automator()->get_recipe_data_by_recipe_id( $recipe_id ); $triggers = $recipe_data['triggers']; if ( $triggers ) { foreach ( $triggers as $trigger ) { $trigger_id = $trigger['ID']; wp_publish_post( $trigger_id ); } } $actions = $recipe_data['actions']; if ( $actions ) { foreach ( $actions as $action ) { $action_id = $action['ID']; wp_publish_post( $action_id ); } } wp_publish_post( $recipe_id ); }
Amazing guys, really appreciate that, will put 5 stars for the great support!
It worked well, I just needed to add the recipe id as a key :
$triggers = $recipe_data[$recipe_id]['triggers'];
instead of$triggers = $recipe_data['triggers'];
Thanks for the feedback! Your updates look good and we’re glad we could help.
- The topic ‘Duplicate recipe programmatically’ is closed to new replies.