Skip Navigation

[Resolved] call to whatsapp button is not working as suggested from Jamal

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to save a URL field automatically when a form is submitted. The URL will be built based on the phone number passed in another field.

Solution:
This will need a custom code to automatically save the URL after submitting the form. Check an example here
https://toolset.com/forums/topic/call-to-whatsapp-button-is-not-working-as-suggested-from-jamal/#post-1898551

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 12 replies, has 2 voices.

Last updated by irfanA 3 years, 12 months ago.

Assisted by: Jamal.

Author
Posts
#1892695

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_action('cred_save_data','whatsapp_link_from_phone_number',10,2);
function whatsapp_link_from_phone_number($post_id,$form_data) {
if ($form_data['id']==397) {
$phone = get_post_meta($post_id, 'wpcf-phone', true);
$whatsapp = "hidden link" . $phone;
update_post_meta($post_id, 'wpcf-whatsapp-link', $whatsapp);
}
}

and activated it as well but still not working can you please check that out,

#1893073

Hello Irfan,

Can you provide a test example so I can check it locally. For example:
- I go to the form.
- I enter the phone number xxx in the phone field. And save.
- I expect to have the following URL yyy in the whatsapp link.
- Instead, I got this zzz.

You may want to allow me temporary access to your website to check this further. If yes, your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1893257

The error in step 5 would be expected, because you have already declared the function in Toolset->Settings->Custom Code.

Then, I'll need examples of phone numbers and the expected WhatsApp links. I know, that may seem a trivial or a stupid question, but that's just how stupid is programming 🙂 , provide me some phone number examples for my tests.

I'll also need FTP access because I can't edit the snippet from Toolset Settings, check this screenshot hidden link
Your next reply will be private.

Can you also let me know where the form is being used?

#1894207

So any update?

#1894751

First of all, the slug of the WhatsApp number field was not correct. It is not "phone", it is currently "wpcf-whatsapp-numbe". Check this screenshot hidden link
Which means that we will need to update the meta_key "wpcf-wpcf-whatsapp-numbe" in the custom code. The WhatsApp link slug is also "wpcf-whatsapp-link", we need to use "wpcf-wpcf-whatsapp-link" in the custom code
So the code should be:

add_action('cred_save_data','whatsapp_link_from_phone_number',10,2);
function whatsapp_link_from_phone_number($post_id,$form_data) {
    if ($form_data['id']==397) {
        $phone = get_post_meta($post_id, 'wpcf-wpcf-whatsapp-numbe', true);
        // We need to add the following line to remove the leading 00
        $phone = ltrim($phone, '0');
        $whatsapp = "<em><u>hidden link</u></em>" . $phone;
        update_post_meta($post_id, 'wpcf-wpcf-whatsapp-link', $whatsapp);
    }
}

I assume that you have changed the field slug during your tests. So, I uploaded the "Adminer" script into your website in order to check the database. And I found that some records were saved with the older slug and some others were saved with the new slug. Check this screenshot hidden link
So, you may find the numbers lost for posts that were saved with the old slugs.

Let's work with the existing slugs "wpcf-whatsapp-numbe" and "wpcf-whatsapp-link" the above code will work.

add_action('cred_save_data','whatsapp_link_from_phone_number',10,2);
function whatsapp_link_from_phone_number($post_id,$form_data) {
    if ($form_data['id']==397) {
        $phone = get_post_meta($post_id, 'wpcf-wpcf-whatsapp-numbe', true);
        // We need to add the following line to remove the leading 00
        $phone = ltrim($phone, '0');
        $whatsapp = "<em><u>hidden link</u></em>" . $phone;
        update_post_meta($post_id, 'wpcf-wpcf-whatsapp-link', $whatsapp);
    }
}

I updated it with FTP in the Toolset Snippet and tested it and it works. Check this screenshot hidden link

I am removing the Adminer script from the website.

I hope this helps. Let me know if you have any questions.

#1896339

Great it is working for whats app I have now created for click to call same script but again it is not working ... can you guide where I am making mistake. Secondly as there are different forms which I am using. I guesss I do need to add the same script for each from with respect to Form ID and Slugs do i need to take care of any other else?

for Phone .. so that I can achieve tel:004915735589800

add_action('cred_save_data','phone_link_from_phone_number',10,2);
function phone_link_from_phone_number($post_id,$form_data) {
if ($form_data['id']==397) {
$phone = get_post_meta($post_id, 'phone-number', true);
$phoneapp = "tel:" . $phone;
update_post_meta($post_id, 'phone-number-link', $phoneapp);
}
}

#1898551

Can you elaborate more on this I have now created for click to call the same script but again it is not working
Can you give steps to follow to see the same issue?

If you want to use the above code for multiple forms, it will need to be adapted. Something like:

add_action('cred_save_data','whatsapp_link_from_phone_number',10,2);
function whatsapp_link_from_phone_number($post_id,$form_data) {
    $forms = array(397, 999);
    if ( in_array($form_data['id'], $forms) ) {
        $phone = get_post_meta($post_id, 'wpcf-wpcf-whatsapp-numbe', true);
        // We need to add the following line to remove the leading 00
        $phone = ltrim($phone, '0');
        $whatsapp = "<em><u>hidden link</u></em>" . $phone;
        update_post_meta($post_id, 'wpcf-wpcf-whatsapp-link', $whatsapp);
    }
}

Add the form IDs, separated by a comma(,), in the second line.

For telephone links, you won't need anything, Toolset supports it out of the box. If you can't get it working let me know where do you want to have the link and I'll check it. However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. So, I'll create a second ticket to handle it there.

#1901303

for second form where I have to put the slug as there is slug different as of "wpcf-wpcf-whatsapp-numbe" and for link as well. And for Phone let me open a new Ticket

#1901469

For the second form, there is no need to add another custom code snippet. You just need to add the form's ID inside the array at line 3.

You can see the form ID in the URL. Check this screenshot hidden link
So, if the form's ID is 59 as per my screenshot. Update line 3 from:

    $forms = array(397, 999);

To:

    $forms = array(397, 59);

Because 999 is just an ID I added to the code to explain how to use the same code for multiple forms.

If you had a 3rd form(ID 1234) where you want the same feature, update the code to:

    $forms = array(397, 59, 1234);

Does it make sense?

#1901587

Thanks You now I got your point and have used same slug for whatsapp Number and Link and it is working find .. Thanks again just a simple request if you can check the other Ticket with Phone Number as have shared earlier or can reply here as well if possible otherwise will mark this ticket as resolved on next reply.

#1901707

My issue is resolved now. Thank you!

#1902025

Your second ticket is now handled by Shane. I am sure he will help you through it.

Feel free to open a new ticket or chat for any other question/request.

#1902207

Thanks Great I am getting it 🙂