Generate post title from another field
-
Hi,
I’m using forminator to create a custom post and it works quite well. But I would like to improve my form with 2 features :
– Populate the post title automatically based on other fields. In my case it would be populated from a date field, post category and tag. I have found a javascript code on this page that makes exactly what I need : https://wpmudev.com/forums/topic/forminator-pro-populating-fields-from-other-fields/
But I don’t find a way to make it work, maybe the code needs to be updated or I need some tips on how to integrate this code. I have tried to add it to the page without success.
– I would also like to generate the post tag automatically based on other field (a radio button) the same way as the post title. I guess I can simply customize the code from the first point.Thanks a lot for your support!
Best,Benoit
-
Hi @beng56
I hope you are doing well.
Usually, we need to update the selector in the script.
Could you please export your form and we can take a closer look?
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-exportUpload it on Google drive and share the link.
Best Regards
Patrick FreitasHi,
Thanks for your reply.
Here is the link to the form exported :
https://drive.google.com/file/d/1K6qSMjMwtnlctY0l2kGyxgV8-0nDm2YW/view?usp=sharingBest regards,
Benoit
In details what I want has “complex” conditions but I guess I can adapt myself the code once I get the idea.
Ideally, I would like the title to be populated this way :
– if {date-6} not empty –> title = {select-1} {radio-3} {date-6} “to” {date-7}
– elseif {date-2} not empty –> title = {select-1} {radio-3} {date-1} “…”
– else title = {select-1} {radio-3} {date-1}For the tag, it is simply post_tag = {radio-3}
My tags are already created with the same values as {radio-3} choices.I can either make the title readonly, in that case I will put it after the last field needed. Or I can simply hide it if easier.
Thanks,
Benoit
Hi @beng56
This sounds like a complex conditional which would be out of our scope.
I created a small JS example and how you can extend it to your needs:
https://gist.github.com/patrickfreitasdev/db5344c9c5a3080803f995f3881ef594
It listens to the change on both select and updates the title, you need to extend it to use the radios or date values.
Let us know if you have any additional question.
Best Regards
Patrick FreitasHi @beng56
I see your form uses the Load from Ajax, so you need to use
$(document).on("after.load.forminator", function (e, id) {
An example can be found here:
https://gist.github.com/patrickfreitasdev/763239e3b92c068abcbc23440b93b709
Best Regards
Patrick FreitasHi Patrick,
Thanks a lot for your help. I already managed to get some results :
– in order to update the title, I can now update it from select field or date field. Thanks ! I will try to get more complex conditions later (and I’ve tried for curiosity and later improvements from text field too but without success until now). I understood the idea.
– in order to update post tag according to a select field, I have some issues. First of all I managed to make it readonly but only partially with :
$(‘#forminator-field-post_tag-postdata-1 option:not(:selected)’).attr(‘disabled’, true);
But I can still select the first option. Besides, I’ve tried to update the tag post value using the following :
– select2-forminator-field-post_tag-postdata-1-results
– select2-forminator-field-post_tag-postdata-1-container
– post_tag-postdata-1
– forminator-field-post_tag-postdata-1
– ..
without any success. I don’t know if I can force it with the value of another select field (even though the text value are the same) or if I have to set the value of the field like “select2-data-select2-forminator-field-post_tag-postdata-1-result-7w9t-51”Sorry if it sounds stupid, I’m in learning process ??
Thanks anyway for the help you already gave me!
Benoit
PS : for now I changed the loading but later on I might move back to Ajax ..
Hi @beng56
Sorry for the delay here.
Sorry if it sounds stupid, I’m in learning process
No worries, it is a bit complex to handle using JS, especially for conditionals.
– in order to update post tag according to a select field, I have some issues. First of all I managed to make it readonly but only partially with :
Just to make sure we are on the same page, it will add to the post as tag what user selected or multiple tags based on selection?
Best Regards
Patrick FreitasHi Patrick,
I have only 2 tags options. It is a single tag choice and I have a radio field with exactly these 2 values. I want the post tag to get the value from this radio field value.
Why so you may ask? It’s only because I have conditions in my form and I’m not able to use condition on “post tag field” easily. So I’m using this role field to do so.
Right now I have twice the same question : once for this radio field and another time for the post tag. My idea is only to get the form cleaner and not confuse my clients with the same question ??Thanks !
Benoit
PS : I’ve deleted the access to the file in my drive, but I can publish it again if necessary.
Hi @beng56
I took a closer look on this, you need to use two Js lines for it on your listen event function,
jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked');
you need to manage the postdata-1-2-post_tag to your tags.
https://monosnap.com/file/JT4bCC82N7habANROAwdI21drWM1kF
Let us know if this helps you with your custom script.
Best Regards
Patrick FreitasHi,
Thanks a lot, this is just what I needed !
I just had to change the post tag field type to multiple in order to make it work.
But it doesn’t matter as I make those fields readonly.Below I will put the code I have been using in case it might be useful to someone.
I’m quite sure it’s not an optimized code or nicely written one but at least it works :). This example has been written for a 3 tags option.<script> jQuery(document).ready(function ($) { // Update if any value is updated var val_changed = ""; // tag fields readonly $('#postdata-1-1-post_tag').attr('disabled', 'disabled'); $('#postdata-1-2-post_tag').attr('disabled', 'disabled'); $('#postdata-1-3-post_tag').attr('disabled', 'disabled'); //get value from radio field $('input[name=radio-3]').change(function(){ var value = $( 'input[name=radio-3]:checked' ).val(); // conditions on radio field value and on the tag value. // The 2nd condition is useful if the user changes the radio field value // I simply call again the jquery.prop in order to deselect the old value on tag field if (value == "Value1"){ if (val_changed == 'Value2') { jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-1-post_tag").prop("checked", true); jQuery('label[for="postdata-1-1-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value1"; } else if (val_changed == 'Value3'){ jQuery("#postdata-1-3-post_tag").prop("checked", true); jQuery('label[for="postdata-1-3-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-1-post_tag").prop("checked", true); jQuery('label[for="postdata-1-1-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value1"; } else { jQuery("#postdata-1-1-post_tag").prop("checked", true); jQuery('label[for="postdata-1-1-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value1"; } } else if (value == "Value2"){ if (val_changed == 'Value3') { jQuery("#postdata-1-3-post_tag").prop("checked", true); jQuery('label[for="postdata-1-3-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value2"; } else if (val_changed == 'Value1'){ jQuery("#postdata-1-1-post_tag").prop("checked", true); jQuery('label[for="postdata-1-1-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value2"; } else { jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value2"; } } else if (value == "Value3"){ if (val_changed == 'Value1') { jQuery("#postdata-1-1-post_tag").prop("checked", true); jQuery('label[for="postdata-1-1-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-3-post_tag").prop("checked", true); jQuery('label[for="postdata-1-3-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value3"; } else if (val_changed == 'Value2'){ jQuery("#postdata-1-2-post_tag").prop("checked", true); jQuery('label[for="postdata-1-2-post_tag"]').toggleClass('forminator-is_checked'); jQuery("#postdata-1-3-post_tag").prop("checked", true); jQuery('label[for="postdata-1-3-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value3"; } else { jQuery("#postdata-1-3-post_tag").prop("checked", true); jQuery('label[for="postdata-1-3-post_tag"]').toggleClass('forminator-is_checked'); val_changed = "Value3"; } } else { //no value selected, do nothing } } ); }); </script>
I will mark it as resolved.
Thanks for your help.Best,
Benoit
- The topic ‘Generate post title from another field’ is closed to new replies.