0

I'm building a contact form with the cf7 plugin for Wordpress. In this form I need a drop down menu which changes automatically every year. In the drop down menu I need to have the current and last 5 years in format 2020, 2019, 2018, 2017, 2016, 2015. That's easy to make with the plugin itself, but of course, when 2021 comes in, I would like that the drop down menu will changes automatically to 2021, 2020, 2019, 2018, 2017 and 2016. So that 2015 disappears(deleted).

Based on the code in this threat I tried to add the current year automatically, but unfortunately I'm already stuck. Hopefully someone can help me with this.

Thanks in advance. Best regards,

Vasco

Vasco
  • 17
  • 6
  • Here is the answer you need. https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/ – Tami Dec 27 '20 at 18:12
  • Thank you Tami, but it's not the solution I'm looking for. I found this post earlier, and it's for creating a big list for example. So yes, I can add al lot of years (eg. 2015 till 2099), but it still won't change automatically. – Vasco Dec 27 '20 at 18:46
  • OK, I'll try to work out a solution for you based off of that example then – Tami Dec 27 '20 at 20:28

1 Answers1

0

Usage Use in Contact Form form like this:

[select recent-years data:years]

In functions.php of your child theme, or in a custom plugin, add this function to dynamically populate the dropdown with the 5 most recent years, and get the years updated automatically as years go by:

function vasco_five_most_recent_years ($values, $options, $args) {

       if ( in_array('years', $options) ){
    
            $years = [];
            $i = 0;

            for( $i = 0; $i < 5; $i++){ // Get 4 most recent years after current year

                 $years[] = (int) date("Y") - $i;
             }

             return $years ;
       }

      return $values;
    }

 add_filter('wpcf7_form_tag_data_option', 'vasco_five_most_recent_years', 10, 3);

Tested, working correctly, entered in the form like so:

setting in cf7 form

Rendering on frontend like so:

dynamically populated cf7 form

Based on the tutorial here https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/

Tami
  • 686
  • 4
  • 8
  • Thanks, but unfortunately it doesn't work. I see a empty drop-down (---) and on page load I see the php code for a split second. I tried to tweak your code, but with no results. – Vasco Dec 27 '20 at 22:12
  • The code is also shown on the front end at the top of the page. – Vasco Dec 27 '20 at 22:33
  • Yeah, ok, I wrote that without testing it. I am writing it properly now and will edit the answer – Tami Dec 28 '20 at 00:13
  • @Vasco I never got an empty dropdown, nor the php code flash you report. Sounds like there is something wrong in the way you implemented either the function or the shortcode in the CF7 form. I did have to update the function as it was showing 2020 and then 4 times 2019 instead of the correct dropdown rendering now. – Tami Dec 28 '20 at 00:27
  • I have no idea what was wrong, it works perfect now! Thank you. So if i'm correct, in 2021 it will hide/delete 2016 from the menu and show 2021? – Vasco Dec 28 '20 at 01:12
  • Exactly, that's correct. And the following year will add 2022 and remove 2017 and so on every year that goes by :) – Tami Dec 28 '20 at 12:14