wpcf7_add_form_tag not working
-
I recently updated from 4.6 to 4.8 and have custom shortcodes in using ‘wpcf7_add_shortcode’ which broke after the update. I then found that this had been replaced by ‘wpcf7_add_form_tag’ which also isn’t working, is there anything else I need to do to get this to work?
-
Having quite the same issue here and am currently inspecting it. Do you have a sample of your code available?
Below is an example of a shortcode i’m currently using:
wpcf7_add_shortcode('opt_out', 'wpcf7_opt_out_shortcode_handler', true); /*[opt_out opt_out]*/ function wpcf7_opt_out_shortcode_handler($tag) { if (!is_array($tag)) return ''; $name = $tag['name']; if (empty($name)) return ''; $output = ""; $opt_out_text = get_field('opt_out_text', 'option'); $output .= "<div style='clear: both;'></div>"; $output .= "<div class='checkbox clearfix'>"; $output .= "<input type='checkbox' id='opt_out' name='opt_out' value='Opt out'>"; $output .= "<label class='round-check opt_out' for='opt_out'></label>"; $output .= "<p class='small'>".$opt_out_text."</p>"; $output .= '<input type="hidden" id="opt_out" name="opt_out" value="no" />'; $output .= "</div>"; return $output; }
After upgrading from 4.7 to 4.8 this no longer works, have also tried updating the shortcode to use
wpcf7_add_form_tag
but still no luck so I’ve reverted the plugin back to 4.7 for the moment.-
This reply was modified 7 years, 8 months ago by
roostermarketing.
Try the following code instead and let me know if it is working for you:
wpcf7_add_shortcode('opt_out', 'wpcf7_opt_out_shortcode_handler', true); /*[opt_out opt_out]*/ function wpcf7_opt_out_shortcode_handler($tag) { if (!is_object($tag)) return ''; $name = $tag->name; if (empty($name)) return ''; $output = ""; $opt_out_text = get_field('opt_out_text', 'option'); $output .= "<div style='clear: both;'></div>"; $output .= "<div class='checkbox clearfix'>"; $output .= "<input type='checkbox' id='opt_out' name='opt_out' value='Opt out'>"; $output .= "<label class='round-check opt_out' for='opt_out'></label>"; $output .= "<p class='small'>".$opt_out_text."</p>"; $output .= '<input type="hidden" id="opt_out" name="opt_out" value="no" />'; $output .= "</div>"; return $output; }
And if it is working try to use wpcf7_add_form_tag instead of wpcf7_add_shortcode and re-check again. This is because the wpcf7_add_shortcode is deprecated and will be removed soon anyway.
As mentioned above have already tried using wpcf7_add_form_tag. This appears to remove the error saying that wpcf7_add_shortcode is deprecated, however this also seems to stop the shortcode from working.
-
This reply was modified 7 years, 8 months ago by
roostermarketing.
-
This reply was modified 7 years, 8 months ago by
roostermarketing.
Have you tried the modified code from above?
Thanks Joe,
changing it from an array to an object has worked but we have kept ‘wpcf7_add_form_tag’
Cheers for your help
Hey,
yes thats fine. That (object vs array) was basically anyway the main issue on the whole thing. However, you might also find a solution for swaping the wpcf7_add_shortcode with the wpcf7_add_form_tag just in order to be save for future updates of CF7.
On a sidenote: The wpcf7_add_shortcode doesn’t do anything other than only wrapping the wpcf7_add_form_tag (see contact-form-7/includes/shortcodes.php) – so the issue must be quite easy to solve.
Thanks for this.
I was using it for displaying from which page the form was submitted:
https://gist.github.com/bainternet/1133507
stopped working after update to 4.8, and this corrected it back to working.Dan
You’re welcome Dan.
From your example code on line 9 it should also say:
$name = $tag->name;
instead of
$name = $tag['name'];
Thanks Joe,
is that also a suggestion for future-proof compatibility?Dan
As far as I can tell: Yes. This is a change being made in Contact Form 7 v4.8 but @takayukister can answer this probably better.
-
This reply was modified 7 years, 8 months ago by
Joe Hana.
-
This reply was modified 7 years, 8 months ago by
- The topic ‘wpcf7_add_form_tag not working’ is closed to new replies.