0

TL;DR - Is there a way CF7 can pull it's form markup and tags from a static file, instead of the database? So I can maintain that file in version control.

We have a very complex form running under Contact Form 7. It has branching, logic and more, all handled by custom code using hooks and filters. However we still have to copy and paste the (thousands of lines) of code that describes this form into the "form editor" in the Wordpress admin panel if the form is changed. We would never edit it there though, it'd be impossible, and in fact we keep this form as a static file, so we can version it. So we rely on the fact that the form in the database matches this static file at all times.

It occured to me today - why not cut out the middle-man and simply have CF7 use markup from this file directly? It would be easier to maintain, and stop anyone accidentally damaging the form markup in the editor.

Is such a mechanism available? Many thanks.

I have discovered this: https://medium.com/@deyanyanakiev/embed-contact-form-7-in-your-custom-theme-9e038c494815 a technique to replace the default form on installation, but as I understand it, that would be a one-off operation and would not allow for the form to be updated.

5
  • A close vote? Care to enlighten me, how can this be any more Wordpress specific?... Ahh.. "questions concerning third party plugins and themes" - seriously? That covers something as ubiquitous as CF7... which even has its own tag? Commented Jun 29, 2021 at 17:25
  • Tags are created by any user with 300 reputation - their existence does not factor in to topicality. Tens of WooCommerce questions are closed daily :) . Knowledge of WordPress alone is insufficient to provide an answer to this question - it requires fairly intimate knowledge of CF7's APIs and inner workings. The question would be best addressed in CF7's official support channels, or associated communities.
    – bosco
    Commented Jun 29, 2021 at 18:27
  • All of that said, there is a motion to see third-party products brought on-topic for another run at it (the last time did not go so well, agitating regulars and plummeting our answer rate to among the lowest on Stack Exchange as a whole). But the lowering of the close-vote threshold to 3 votes has obscured the statistical baseline for determining the impact of questions about third-party products on our site.
    – bosco
    Commented Jun 29, 2021 at 18:44
  • Thanks for the feedback. For your information I solved this problem already, and (imho) it's such a quirky and useful piece of work that I was looking forward to posting the answer here, rather than on SO where I originally posted the question. I thought it would be of interest and benefit to this community. So - just so you can see it from my perspective - I deliberately came here to add to the knowledgebase, just to be kinda pushed away. I understand about the plugins thing, but CF7 seems above the grey area to me. Commented Jun 29, 2021 at 19:03
  • Oh by all means! Yeah, I think it would be awesome to have your answer here, and well formatted and interesting question. It's just... historically, and from the daily volume of questions regarding third-party products, your question and answer are very much the exception. The argument has been hashed over time and time again on our meta site - using a plugin's popularity has been brought up frequently, but is widely regarded as too arbitrary of a criteria and risks favoritism if not general confusion
    – bosco
    Commented Jun 29, 2021 at 19:08

1 Answer 1

1

I answered this question on Stackoverflow, and reposting the answer here.

We would never edit it there though, it'd be impossible,

quite, the CF7 plugin was never conceived for anything more complex than a contact form unfortunately. You may want to play around with the Smart Grid-layout extension which was specifically designed for creating and maintaining complex forms. It has a modular functionality that allows you to build a form using sub-forms, making form maintenance much easier.

It also uses the excellent CodeMirror editor as its code editor, much more powerful than the ridiculous textarea used in CF7 plugin.

simply have CF7 use markup from this file directly?

yes, that the simplest way to solve your problem. Hook the following CF7 filter,

add_filter('wpcf7_default_template', 'load_custom_form_template',10,2);
function load_custom_form_template($template, $prop){
    if($prop !== 'form') return $template;
    include( 'path-to-your-default-form.php');
    return $template;
}

Now, if you do use the Smart Grid extension which has the ability to load form specific js and css scripts (only on the pages where the form is loaded), then you can hook the following actions to add JavaScript template,

add_action('cf7sg_default_custom_js_template', 'use_js_script',10,1);
function use_js_script($cf7_key){
  include_once 'path-to-your-default-js';
}

and CSS stylesheet template,

add_action('cf7sg_default_custom_css_template', 'use_css_script',10,1);
function use_css_script($cf7_key){
  include_once 'path-to-your-default-css';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.