Skip to content

Instantly share code, notes, and snippets.

@KZeni
Last active September 20, 2018 06:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KZeni/454a0d7777c78cd2bfae0d0728a3ac56 to your computer and use it in GitHub Desktop.
Save KZeni/454a0d7777c78cd2bfae0d0728a3ac56 to your computer and use it in GitHub Desktop.
Contact Form 7 - Success Page Redirects version 1.2.0 plugin fix (fixes conflict with Flamingo add-on and also makes it work with & without AJAX enabled on the form) as mentioned on https://wordpress.org/support/topic/compatibility-issue-with-flamingo-official-plugin-for-storing-cf7-submissions/
<?php
/**
* Plugin Name: Contact Form 7 - Success Page Redirects
* Description: An add-on for Contact Form 7 that provides a straightforward method to redirect visitors to success pages or thank you pages.
* Version: 1.2.0
* Author: Ryan Nevius
* Author URI: http://www.ryannevius.com
* License: GPLv3
*/
/**
* Verify CF7 dependencies.
*/
function cf7_success_page_admin_notice() {
// Verify that CF7 is active and updated to the required version (currently 3.9.0)
if ( is_plugin_active('contact-form-7/wp-contact-form-7.php') ) {
$wpcf7_path = plugin_dir_path( dirname(__FILE__) ) . 'contact-form-7/wp-contact-form-7.php';
$wpcf7_plugin_data = get_plugin_data( $wpcf7_path, false, false);
$wpcf7_version = (int)preg_replace('/[.]/', '', $wpcf7_plugin_data['Version']);
// CF7 drops the ending ".0" for new major releases (e.g. Version 4.0 instead of 4.0.0...which would make the above version "40")
// We need to make sure this value has a digit in the 100s place.
if ( $wpcf7_version < 100 ) {
$wpcf7_version = $wpcf7_version * 10;
}
// If CF7 version is < 3.9.0
if ( $wpcf7_version < 390 ) {
echo '<div class="error"><p><strong>Warning: </strong>Contact Form 7 - Success Page Redirects requires that you have the latest version of Contact Form 7 installed. Please upgrade now.</p></div>';
}
}
// If it's not installed and activated, throw an error
else {
echo '<div class="error"><p>Contact Form 7 is not activated. The Contact Form 7 Plugin must be installed and activated before you can use Success Page Redirects.</p></div>';
}
}
add_action( 'admin_notices', 'cf7_success_page_admin_notice' );
/**
* Adds a box to the main column on the form edit page.
*
* CF7 < 4.2
*/
function cf7_success_page_add_meta_boxes() {
add_meta_box( 'cf7-redirect-settings', 'Success Page Redirect', 'cf7_success_page_metaboxes', '', 'form', 'low');
}
add_action( 'wpcf7_add_meta_boxes', 'cf7_success_page_add_meta_boxes' );
/**
* Adds a tab to the editor on the form edit page.
*
* CF7 >= 4.2
*/
function cf7_success_add_page_panels($panels) {
$panels['redirect-panel'] = array( 'title' => 'Redirect Settings', 'callback' => 'cf7_success_page_panel_meta' );
return $panels;
}
add_action( 'wpcf7_editor_panels', 'cf7_success_add_page_panels' );
// Create the meta boxes (CF7 < 4.2)
function cf7_success_page_metaboxes( $post ) {
wp_nonce_field( 'cf7_success_page_metaboxes', 'cf7_success_page_metaboxes_nonce' );
$cf7_success_page = get_post_meta( $post->id(), '_cf7_success_page_key', true );
// The meta box content
$dropdown_options = array (
'echo' => 0,
'name' => 'cf7-redirect-page-id',
'show_option_none' => '--',
'option_none_value' => '0',
'selected' => $cf7_success_page
);
echo '<fieldset>
<legend>Select a page to redirect to on successful form submission.</legend>' .
wp_dropdown_pages( $dropdown_options ) .
'</fieldset>';
}
// Create the panel inputs (CF7 >= 4.2)
function cf7_success_page_panel_meta( $post ) {
wp_nonce_field( 'cf7_success_page_metaboxes', 'cf7_success_page_metaboxes_nonce' );
$cf7_success_page = get_post_meta( $post->id(), '_cf7_success_page_key', true );
// The meta box content
$dropdown_options = array (
'echo' => 0,
'name' => 'cf7-redirect-page-id',
'show_option_none' => '--',
'option_none_value' => '0',
'selected' => $cf7_success_page
);
echo '<h3>Redirect Settings</h3>
<fieldset>
<legend>Select a page to redirect to on successful form submission.</legend>' .
wp_dropdown_pages( $dropdown_options ) .
'</fieldset>';
}
// Store Success Page Info
function cf7_success_page_save_contact_form( $contact_form ) {
$contact_form_id = $contact_form->id();
if ( !isset( $_POST ) || empty( $_POST ) || !isset( $_POST['cf7-redirect-page-id'] ) ) {
return;
}
else {
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['cf7_success_page_metaboxes_nonce'], 'cf7_success_page_metaboxes' ) ) {
return;
}
// Update the stored value
update_post_meta( $contact_form_id, '_cf7_success_page_key', $_POST['cf7-redirect-page-id'] );
}
}
add_action( 'wpcf7_after_save', 'cf7_success_page_save_contact_form' );
/**
* Copy Redirect page key and assign it to duplicate form
*/
function cf7_success_page_after_form_create( $contact_form ){
$contact_form_id = $contact_form->id();
// Get the old form ID
if ( !empty( $_REQUEST['post'] ) && !empty( $_REQUEST['_wpnonce'] ) ) {
$old_form_id = get_post_meta( $_REQUEST['post'], '_cf7_success_page_key', true );
}
// Update the duplicated form
update_post_meta( $contact_form_id, '_cf7_success_page_key', $old_form_id );
}
add_action( 'wpcf7_after_create', 'cf7_success_page_after_form_create' );
/**
* Redirect the user after the submission is made & was confirmed to be successful
*/
function cf7_success_page_form_submitted_ajax( $items, $result ) {
// Set array of acceptible statuses/cases
$cases = (array) apply_filters( 'wpcf7_flamingo_submit_if',
array( 'mail_sent' ) );
// Reject the submission if it wasn't a valid status/case
if ( empty( $result['status'] )
|| ! in_array( $result['status'], $cases ) ) {
return $items;
}
// Otherwise, continue forward as it was a successful form submission
$contactform = WPCF7_ContactForm::get_current();
$contact_form_id = $contactform->id();
// Send user to a success page, if there is one
$success_page = get_post_meta( $contact_form_id, '_cf7_success_page_key', true );
if ( ! isset( $items['onSentOk'] ) ) {
$items['onSentOk'] = array();
}
if ( ! empty( $success_page ) ) {
$items['onSentOk'][] = sprintf(
'window.location = "%s";',
get_permalink( $success_page )
);
}
return $items;
}
function cf7_success_page_form_submitted_noajax( $contactform, $result ) {
// AJAX is enabled; use JavaScript to perform the redirect instead
if ( isset( $_POST['_wpcf7_is_ajax_call'] ) || ! isset( $result['status'] ) ) {
return;
}
// Set array of acceptible statuses/cases
$cases = (array) apply_filters( 'wpcf7_flamingo_submit_if',
array( 'mail_sent' ) );
// Reject the submission if it wasn't a valid status/case
if ( empty( $result['status'] )
|| ! in_array( $result['status'], $cases ) ) {
return;
}
// Otherwise, continue forward as it was a successful form submission
$contact_form_id = $contactform->id();
// Send user to a success page, if there is one
$success_page = get_post_meta( $contact_form_id, '_cf7_success_page_key', true );
if ( !empty($success_page) ) {
wp_redirect( get_permalink( $success_page ) );
die();
}
}
add_filter( 'wpcf7_ajax_json_echo', 'cf7_success_page_form_submitted_ajax', 10, 2 ); // AJAX is enabled
add_action( 'wpcf7_submit', 'cf7_success_page_form_submitted_noajax', 900, 2 ); // AJAX is disabled (use really late priority to ensure all other submission actions are completed first as this will inturrupt actions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment