Contact-form-7: Change year-text in auto-replies dynamically

ε祈祈猫儿з 提交于 2021-01-28 06:20:55

问题


In every email auto-reply, there is a text for copyright purposes like: <em>Copyright © 2019 - All rights reserved.</em>

Every year I have to change it manually for every form.

Can this be done automatically?

Like replacing the year with a variable: <em>Copyright © {{current_year}} - All rights reserved.</em>?


回答1:


There is no simple 'variable replacement' in the CF7 email. It is however easy to include the value of a form field. With that said, you could create a custom form tag [year_tag] which you would include on the "FORM" tab of your contact form. (It could be right before the [submit] tag.)

First, you would add the below code to your theme functions.php and/or you could put it in a plugin if you were so inclined.

function dd_add_year_tag(){
        // This adds a form tag to the FORM itself called [year_tag]
        wpcf7_add_form_tag('year_tag', 'cf7_year_field_handler');
}
add_action('wpcf7_init', 'dd_add_year_tag');

function cf7_year_field_handler($tag){
    $year = date('Y');
    // create hidden form field with name "current-year" and Current Year as value.
    $output = '<input type="hidden" name="current-year" value='.$year.'>';
    return $output;
}

Then on your email you would put.

<em>Copyright © [current-year] - All rights reserved.</em>

-- On the CF7 E-Mail form... the NAME of the form field can be used to output any form field, whether hidden or visible.



来源:https://stackoverflow.com/questions/59718002/contact-form-7-change-year-text-in-auto-replies-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!