Alright, for anyone seeking a solution here.
Here are the steps that work for me. Please note this is a hack, but it works. It is done using CF7 and ACF.
- Create your form on the main site.
Create a function in your functions.php to print out (using do_shortcode) the general shortcode of your form (given by CF7)
function get_general_field($variable) {
if (get_current_blog_id() != 1) {
switch_to_blog(1);
}
$custom_fields = do_shortcode(get_field($variable, 17284));
if (ms_is_switched()) {
restore_current_blog();
}
return $custom_fields;
}
Now create a general ACF field group for your main site (make it appear only on site 1 for example, which include a text field for your "GENERAL FORM"
You will create a private page on your main site, call it "GENERAL FIELDS" in which you will paste the shortcode from CF7.
Now in your theme file, you can simply call the get_general_field('FIELD_NAME').
At this point, the form will show, but you need to add css and js for ajax validation.
if( class_exists( 'WPCF7' ) ) {
function yuna_design_manually_enqueue_wpcf7_scripts() {
global $post;
if( is_single() || strpos( $post->post_name, 'group' ) ) {
if( function_exists( 'wpcf7_enqueue_scripts' ) )
wpcf7_enqueue_scripts();
if( function_exists( 'wpcf7_enqueue_styles' ) )
wpcf7_enqueue_styles();
}
}
add_filter( 'wpcf7_load_js', '__return_false' ); // Disable CF7 Js
add_filter( 'wpcf7_load_css', '__return_false' ); // Disable CF7 CSS
add_action( 'wp_enqueue_scripts', 'yuna_design_manually_enqueue_wpcf7_scripts' );
}
(Here is the hack) Now, everything is working but you have two errors in your console and a looping form who never calls back on subsites (other than main site #1). This is because it is calling the subsite for validation, and since you don't have a form with the proper id, it returns and error.
(after some thoughts and testing, I think this might be optional) You need to modify two files from CF7 (I am using Version 5.1.7). First one is: contact-form-7/includes/rest-api. add the following code right after the opening of wpcf7_rest_api_init(){ :
if (get_current_blog_id() != 1) {
switch_to_blog(1);
}
...where 1 is my main site id...
and also don't forget to add the following code, right at the end of that function (before the closing "}" ) :
if (ms_is_switched()) {
restore_current_blog();
}
you need to look for the 2 instances of "wpcf7.getId( $form )" inside contact-form-7/includes/js/scripts.js. You will find a dynamic link validation, which you'll need to make it static and pointing to your main site url. Basically this:
url: wpcf7.apiSettings.getRoute(
'/contact-forms/' + wpcf7.getId( $form ) + '/feedback' ),
will need to look like this:
url: 'HTTPS://YOURWEBSITEURL.COM/wp-json/contact-form-7/v1/contact-forms/' + wpcf7.getId( $form ) + '/feedback',
And voilà, test your form in a subsite and the ajax errors should be working fine. You now have one form usable for multiple sites in your wordpress setup.
Please note, this is quite temporary solution, a hack made for a project of mine and it would be great to have instead a multisite support for CF7, since this is not really a big tweaking to be done from plugin authors.
Please feel free to comment and add on this solution. I was looking a long time online and did not find a thing working properly.
Cheers,
Maxime Bellefleur
Wordpress integrator for small bussiness
www.yunadesign.com