Last active
April 11, 2021 13:55
-
-
Save campusboy87/547a1694dfe6d6b2f8b40cf17e1e0056 to your computer and use it in GitHub Desktop.
Contact Form 7 Redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WPCF Redirect | |
*/ | |
add_filter( 'shortcode_atts_wpcf7', 'shortcode_atts_wpcf7' ); | |
add_filter( 'wpcf7_editor_panels', 'wpcf7_add_redirect_panels' ); | |
add_action( 'wpcf7_after_save', 'wpcf7_redirect_save_field' ); | |
add_action( 'wp_footer', 'wpcf7_redirect_enqueue_assets' ); | |
/** | |
* Добавляет вкладку в редактор формы. | |
* | |
* @param array $panels | |
* | |
* @return array | |
*/ | |
function wpcf7_add_redirect_panels( $panels ) { | |
$panels['redirect-panel'] = [ | |
'title' => 'Перенаправления', | |
'callback' => 'wpcf7_redirect_panel_form', | |
]; | |
return $panels; | |
} | |
/** | |
* Выводит на экран содержимое вкладки. | |
* | |
* @param WPCF7_ContactForm $post | |
*/ | |
function wpcf7_redirect_panel_form( $post ) { | |
$value = get_post_meta( $post->id(), '_redirect', true ); | |
?> | |
<h2>Перенаправления</h2> | |
<fieldset> | |
<legend>Перенаправление после успешной отправки формы.</legend> | |
<input | |
type="url" | |
name="wpcf7-redirect" | |
style="width: 100%;" | |
placeholder="Укажите ссылку" | |
value="<?php echo esc_attr( $value ); ?>" | |
> | |
</fieldset> | |
<?php | |
} | |
/** | |
* Валидирует и сохраняет значение поля. | |
* | |
* @param WPCF7_ContactForm $form | |
*/ | |
function wpcf7_redirect_save_field( $form ) { | |
if ( ! empty( $_POST['wpcf7-redirect'] ) ) { | |
update_post_meta( $form->id(), '_redirect', esc_url( $_POST['wpcf7-redirect'] ) ); | |
} else { | |
delete_post_meta( $form->id(), '_redirect' ); | |
} | |
} | |
/** | |
* Регистрирует данные об обработанных формах (шорткодах) плагина CF7. | |
* | |
* @param array $out | |
* | |
* @return array | |
*/ | |
function shortcode_atts_wpcf7( $out ) { | |
wpcf7_redirect_add_form_data( $out['id'] ); | |
return $out; | |
} | |
/** | |
* Возвращает данные об обработанных формах (шорткодах) плагина CF7. | |
* | |
* @param int|null $form_id | |
* | |
* @return array | |
*/ | |
function wpcf7_redirect_add_form_data( $form_id = null ) { | |
static $forms = []; | |
if ( $form_id && empty( $forms[ $form_id ] ) ) { | |
$url = get_post_meta( $form_id, '_redirect', true ); | |
if( $url ){ | |
$forms[ $form_id ] = esc_url( $url ); | |
} | |
} | |
return $forms; | |
} | |
/** | |
* Выводит на экран JavaScript и стили плагина. | |
* | |
* @return void | |
*/ | |
function wpcf7_redirect_enqueue_assets() { | |
$forms = array_filter( wpcf7_redirect_add_form_data() ); | |
$scripts = $styles = ''; | |
foreach ( $forms as $id => $link ) { | |
$scripts .= "if ('$id' == event.detail.contactFormId) { location.assign('$link'); }"; | |
$styles .= ".wpcf7[id*='f{$id}-'] .wpcf7-mail-sent-ok { display: none !important; }"; | |
} | |
if ( $scripts ) { | |
?> | |
<script> | |
document.addEventListener('wpcf7mailsent', function () { | |
<?php echo $scripts; ?> | |
}, false); | |
</script> | |
<style> | |
<?php echo $styles; ?> | |
</style> | |
<?php | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WPCF Redirect | |
*/ | |
add_filter( 'wpcf7_editor_panels', 'wpcf7_add_redirect_panels' ); | |
add_filter( 'shortcode_atts_wpcf7', 'shortcode_atts_wpcf7' ); | |
add_action( 'wpcf7_after_save', 'wpcf_redirect_save_field' ); | |
/** | |
* Добавляет вкладку в редактор формы. | |
* | |
* @param array $panels | |
* | |
* @return array | |
*/ | |
function wpcf7_add_redirect_panels( $panels ) { | |
$panels['redirect-panel'] = [ | |
'title' => 'Перенаправления', | |
'callback' => 'wpcf7_redirect_panel_form', | |
]; | |
return $panels; | |
} | |
/** | |
* Выводит на экран содержимое вкладки. | |
* | |
* @param WPCF7_ContactForm $post | |
*/ | |
function wpcf7_redirect_panel_form( $post ) { | |
$value = get_post_meta( $post->id(), '_redirect', true ); | |
?> | |
<h2>Перенаправления</h2> | |
<fieldset> | |
<legend>Перенаправление после успешной отправки формы.</legend> | |
<input | |
type="url" | |
name="wpcf7-redirect" | |
style="width: 100%;" | |
placeholder="Укажите ссылку" | |
value="<?php echo esc_attr( $value ); ?>" | |
> | |
</fieldset> | |
<?php | |
} | |
/** | |
* Валидирует и сохраняет значение поля. | |
* | |
* @param WPCF7_ContactForm $form | |
*/ | |
function wpcf_redirect_save_field( $form ) { | |
if ( ! empty( $_POST['wpcf7-redirect'] ) ) { | |
update_post_meta( $form->id(), '_redirect', sanitize_text_field( $_POST['wpcf7-redirect'] ) ); | |
} else { | |
delete_post_meta( $form->id(), '_redirect' ); | |
} | |
} | |
/** | |
* Регистриует хук для вывода JS под каждую запрошенную форму, у которой указан редирект. | |
* | |
* @param array $out | |
* | |
* @return array | |
*/ | |
function shortcode_atts_wpcf7( $out ) { | |
add_action( 'wp_footer', function () use ( $out ) { | |
if ( ! empty( $out['id'] ) && $id = (int) $out['id'] ) { | |
$form = WPCF7_ContactForm::get_instance( $id ); | |
$link = get_post_meta( $id, '_redirect', true ); | |
if( $link ): | |
?> | |
<script> | |
document.addEventListener('wpcf7mailsent', function () { | |
location.assign("<?php echo esc_url( $link ); ?>"); | |
}, false); | |
</script> | |
<style> | |
.wpcf7[id*='f<?php echo $id; ?>-'] .wpcf7-mail-sent-ok { | |
display: none !important; | |
} | |
</style> | |
<?php | |
endif: | |
} | |
} ); | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment