Skip to content

Instantly share code, notes, and snippets.

@yeriepiscesa
Created April 20, 2019 04:01
Show Gist options
  • Select an option

  • Save yeriepiscesa/69b256933c763c6c6213c5744454109a to your computer and use it in GitHub Desktop.

Select an option

Save yeriepiscesa/69b256933c763c6c6213c5744454109a to your computer and use it in GitHub Desktop.
<?php
add_filter( 'wpcf7_skip_mail', 'solusiprss_cf7_send_whatsapp', 10, 2 );
add_filter( 'wpcf7_display_message', 'solusipress_cf_wa_sent_message', 10, 2 );
add_action( 'wp_footer', 'solusipress_cf7_add_script_footer' );
function solusiprss_cf7_send_whatsapp( $skip_mail, $contact_form ) {
if( $contact_form->id() == 105 ) {
$skip_mail = true;
}
return $skip_mail;
}
function solusipress_cf_wa_sent_message( $message, $status ) {
if( $status == 'mail_sent_ok' ) {
$message = 'Terima kasih telah mengisi kontak form';
}
return $message;
}
function solusipress_cf7_add_script_footer() {
if( is_page( 'contact-form-whatsapp' ) ):
?><script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
var inputs = event.detail.inputs;
var the_text = '';
jQuery.each( inputs, function( index, detail ) {
if( the_text != '' ) { the_text += "\n"; }
switch( detail.name ) {
case 'your-name':
the_text += "Nama : " + detail.value;
break;
case 'your-email':
the_text += "Email : " + detail.value;
break;
case 'your-subject':
the_text += "Subject : " + detail.value;
break;
case 'your-message':
the_text += "Message : " + detail.value;
break;
}
} );
the_text = window.encodeURIComponent( the_text );
var the_phone = '6281314997198'; // ganti nomor dengan milik Anda
var url = 'https://api.whatsapp.com/send?phone=' + the_phone + '&text=' + the_text;
var isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if( isSafari && iOS ) {
location = url;
} else {
window.open( url, '_blank' );
}
}, false );
</script><?php
endif;
}
@gamalkamal
Copy link

does it need custom fields plugin to add custom filters in code ?

@yeriepiscesa
Copy link
Author

does it need custom fields plugin to add custom filters in code ?
not necessary, use code snippets plugin (https://id.wordpress.org/plugins/code-snippets/) to write the code above.
please read the article https://translate.google.com/translate?hl=en&sl=id&tl=en&u=https%3A%2F%2Fsolusipress.com%2Fmengarahkan-isian-contact-form-7-ke-pesan-whatsapp%2F

@gamalkamal
Copy link

i followed the tutorial link in the first place !! .. although the code js idea is magnificent with simplicity .. there is a questions !
if i right . when cliking submit button the snippet will open js script , send customer info to the registered wa phone no
1- is it vaild on desktop web versions , and for that i must valid my wa web?( it didnt work on mobile chrome browser )
2- the cf7 due to the code snippet was in footer( using inspect ) but not executed i think !
3- sould i have put the url of c7 form page or just its id ?

@yeriepiscesa
Copy link
Author

i followed the tutorial link in the first place !! .. although the code js idea is magnificent with simplicity .. there is a questions !
if i right . when cliking submit button the snippet will open js script , send customer info to the registered wa phone no
1- is it vaild on desktop web versions , and for that i must valid my wa web?( it didnt work on mobile chrome browser )
2- the cf7 due to the code snippet was in footer( using inspect ) but not executed i think !
3- sould i have put the url of c7 form page or just its id ?

yes, you're right.

  1. on my test site, it works on web and mobile browser.
  2. on this line of code (line 20): if( is_page( 'contact-form-whatsapp' ) ) make sure your page slug (which contain cf7 shortcode) is "contact-form-whatsapp", otherwise use your own slug page there.
  3. just the id, you can see on line 6 : if( $contact_form->id() == 105 ) { <-- 105 is contact form ID

check the working demo: https://businessprofile.solusipress.com/contact-form-whatsapp/
as you can see, the slug is "contact-form-whatsapp"

@gamalkamal
Copy link

ok it just not works for me ..
and you dont have to answer the following i you wont !
when you create filters to your code , did you use web-hooks plugin or some customization for apply filters before adding them ?
the second question .. the 'solusiprss_cf7_send_whatsapp & solusipress_cf_wa_sent_message are functions .. so why and how you applied them to filters ?

@yeriepiscesa
Copy link
Author

ok it just not works for me ..
and you dont have to answer the following i you wont !
when you create filters to your code , did you use web-hooks plugin or some customization for apply filters before adding them ?
the second question .. the 'solusiprss_cf7_send_whatsapp & solusipress_cf_wa_sent_message are functions .. so why and how you applied them to filters ?

first... no, just search for "apply_filters" or "do_action" at the plugin's( or theme or wp core ) files. doing customizations directly to theme/plugin's files are not recomended.
second... I need to alter(add or change) something(behavior or value) for the plugin ( or theme or wp core itself ), you may need to read this article: https://wpshout.com/wordpress-hooks-actions-filters-work/

@gamalkamal
Copy link

ok it just not works for me ..
and you dont have to answer the following i you wont !
when you create filters to your code , did you use web-hooks plugin or some customization for apply filters before adding them ?
the second question .. the 'solusiprss_cf7_send_whatsapp & solusipress_cf_wa_sent_message are functions .. so why and how you applied them to filters ?

first... no, just search for "apply_filters" or "do_action" at the plugin's( or theme or wp core ) files. doing customizations directly to theme/plugin's files are not recomended.
second... I need to alter(add or change) something(behavior or value) for the plugin ( or theme or wp core itself ), you may need to read this article: https://wpshout.com/wordpress-hooks-actions-filters-work/

thanks and i appreciate that!

@praswono
Copy link

praswono commented Oct 1, 2019

Hey gan, thanks! Code nya bagus :) Sy sdh coba di website sy nih: marchand.id

Tapi ya gan, wktu sy buka website sy di iPhone, kenapa setiap kali sy submit form di website sy, harus click ulang 'Open this page in WhatsApp"?' ya?

Tapi di form agan, sekali klik submit form sdh langsung mulai chat nya?

Terima kasih ya :)

Screenshot

@ariel-men
Copy link

function solusipress_cf7_add_script_footer() {
if( is_page( 'contact-form-whatsapp' ) ):
?><script>

ijin tanya, code ini berati menuju page yang ditentukan, jika saya ingin menampilkan diseluruh page saya bagaimana pak?
terima kasih

@yeriepiscesa
Copy link
Author

yeriepiscesa commented Nov 7, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment