How to Create a WhatsApp Share Link in WordPress to Share Post and Page

Categories:

Do you want to make it easy for your visitors to share your WordPress posts and pages on WhatsApp? Adding a WhatsApp share link to your website can encourage your visitors to share your content with their friends and family.

In this article, we’ll show you how to create a WhatsApp share link in WordPress that shares the current post or page title with a link. This will allow your visitors to easily share your content on WhatsApp with just one click.

whatsapp icon

To create a WhatsApp share link in WordPress, you’ll need to use some PHP code. You can add this code to your WordPress theme’s functions.php file, or you can use a plugin like Code Snippets to add the code to your site.

PHP Code for Whatsapp Share:

function create_whatsapp_share_link() {
$title = urlencode(get_the_title());
$url = urlencode(get_permalink());
$link = 'https://wa.me/?text=' . $title . '%20-%20' . $url;
echo '<a class="whatsappshare" href="' . $link . '" target="_blank">Share on WhatsApp</a>';
}

To display the WhatsApp share link in your WordPress theme, you can call the create_whatsapp_share_link() function wherever you want the link to appear, such as in your single post template file or page template file:

<?php create_whatsapp_share_link(); ?>

This code creates a function called create_whatsapp_share_link() that uses the get_the_title() function to get the current post or page title and the get_permalink() function to get the current post or page URL. The function then uses the urlencode() function to encode the title and URL into a format that can be used in a URL.

Finally, the function concatenates the encoded title and URL into a WhatsApp share link and echoes the link as HTML. The link will appear on your WordPress site as a “Share on WhatsApp” button, and when clicked, it will open WhatsApp and allow your visitors to share the current post or page title and link.

CSS Code to design button

.whatsappicon{
position: fixed;
right: 0;
top: 50%;
transform: translateY(-50%);
box-shadow: -5px 4px 10px 0 rgba(0,0,0,.2);
width: 51px;
height: 44px;
background-color: #25d366;
border-radius: 8px 0 0 8px;
z-index: 9991;
background-image: url('image/whatsapp.png');
}

By adding a WhatsApp share link to your WordPress site, you can make it easy for your visitors to share your content on WhatsApp and reach a wider audience. With just a few lines of code, you can encourage your visitors to share your content and help your site grow.

Call the above function using theme footer.php

To call the create_whatsapp_share_link() function on every page of your WordPress site, you can add it to your theme’s footer.php file. The footer.php file is called on every page of your site, so this is an easy way to ensure that the WhatsApp share link appears on every page.

Here’s how to do it:

  1. Open your theme’s footer.php file. This file is typically located in the /wp-content/themes/your-theme-name/ directory.
  2. Add the following code to the bottom of the file:
<?php create_whatsapp_share_link(); ?>

his code calls the create_whatsapp_share_link() function, which creates the WhatsApp share link with the current post or page title and URL.

  1. Save the footer.php file and refresh your website to see the WhatsApp share link on every page.

That’s it! By adding the create_whatsapp_share_link() function to your theme’s footer.php file

Using Function.php file

If you want to call the create_whatsapp_share_link() function using the functions.php file in your WordPress theme, you can add an action hook to the wp_footer() function.

Here’s how to do it:

  1. Open your theme’s functions.php file. This file is typically located in the /wp-content/themes/your-theme-name/ directory.
  2. Add the following code to the bottom of the file:
function create_whatsapp_share_link() {
$title = urlencode(get_the_title());
$url = urlencode(get_permalink());
$link = 'https://wa.me/?text=' . $title . '%20-%20' . $url;
echo '<a class="whatsappshare" href="' . $link . '" target="_blank">Share on WhatsApp</a>';
}

add_action( 'wp_footer', 'create_whatsapp_share_link' );

This code creates a new function called add_whatsapp_share_link_to_footer() that calls the create_whatsapp_share_link() function. It then adds an action hook to the wp_footer() function, which calls the add_whatsapp_share_link_to_footer() function.

  1. Save the functions.php file and refresh your website to see the WhatsApp share link on every page.

That’s it! By adding an action hook to the wp_footer() function, you can ensure that the create_whatsapp_share_link() function is called on every page of your WordPress site.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest comments