Last active
April 9, 2024 12:02
-
-
Save campusboy87/8c4819fff7a51f96787da37773f67023 to your computer and use it in GitHub Desktop.
Allows you to use html in cf7 notifications + replaces a successfully submitted form with a success message
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 | |
( new CF7_Notification_Shortcodes() )->hooks(); | |
/** | |
* Allows you to use shortcodes in the CF7 form settings in the "Notifications when submitting a form" tab. | |
*/ | |
class CF7_Notification_Shortcodes { | |
private array $shortcodes = []; | |
public function hooks(): void { | |
add_filter( 'wpcf7_submission_result', [ $this, 'special_mail_tags' ] ); | |
add_action( 'wp_enqueue_scripts', [ $this, 'attach_assets' ] ); | |
} | |
public function attach_assets() { | |
wp_enqueue_script( | |
'wpcf7submit_notification_with_html_tags', | |
// IMPORTANT: Replace the path to the file js to the one where you will have it | |
get_template_directory_uri() . '/js/wpcf7submit_notification_with_html_tags.js', | |
[ 'contact-form-7' ] | |
); | |
} | |
public function special_mail_tags( $result ) { | |
if ( empty( $result['message'] ) ) { | |
return $result; | |
} | |
$this->register_shortcode_link(); | |
$result['message'] = do_shortcode( $result['message'] ); | |
$this->remove_shorcodes(); | |
return $result; | |
} | |
private function register_shortcode_link(): void { | |
$shortcode = 'link'; | |
add_shortcode( $shortcode, static function ( $atts ) { | |
return sprintf( | |
'<a href="%s" target="_blank">%s</a>', | |
esc_url( $atts['url'] ), | |
esc_html( $atts['text'] ) | |
); | |
} ); | |
$this->shortcodes[] = $shortcode; | |
} | |
public function remove_shorcodes(): void { | |
foreach ( $this->shortcodes as $shortcode ) { | |
remove_shortcode( $shortcode ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment