The share button is an essential component in a blog, and the share button makes it easy for visitors to share essential articles from your site to everyone in the world. But using too many plugins reduce your web dan WordPress hosting performance. If you can make it simpler, why not. Creating your media social button without plugin is the best solution.
Lots of WordPress social media button plugins can be downloaded for free. These plugins have various advantages. But the more features in a plugin, the lower the performance and speed of our WordPress websites. That is due to the increasing number of Javascript files used.
In this WordPress tutorials, we create the share button WordPress without plugin and Javascript code in it. So the results are more satisfying in improving the performance of your WordPress site.
The WordPress Share Button that we create include:
- Create a WordPress share button for a standard theme.
- Create a WordPress share button for the genesis framework.
- Wrap your share button in the form of a plugin (Important.)
Let’s get started: create your share button
In this article, we create share buttons for 4 types of social media sharing buttons (Twitter, Facebook, Telegram, and WhatsApp). The fourth social media is the most commonly used.
1. Create a WordPress share button for a standard theme
Add the following code in functions.php in the WordPress site theme folder you are using.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php function sp_social_sharing_buttons($content) { global $post; if(is_single() or is_page()){ // will show share button in post or page article $spURL = urlencode(get_permalink()); $spTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8'); $spThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); $twitterURL = 'https://twitter.com/intent/tweet?text='.$spTitle.'&url='.$spURL.'&via=sp'; $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$spURL; $whatsappUrl = 'whatsapp://send?text='.$spTitle.' '.$spURL; $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$spURL.'&media='.$spThumbnail[0].'&description='.$spTitle; $telegramUrl = 'https://t.me/share?url='.$spURL; $sosbutton = '<div class="sp-social">'; $sosbutton .= '<p> Share : <a class="sp-link sp-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>'; $sosbutton .= '<a class="sp-link sp-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>'; $sosbutton .= '<a class="sp-link sp-telegram" href="'.$telegramUrl.'" target="_blank">Telegram</a>'; $sosbutton .= '<a class="sp-link sp-whatsapp" href="'.$whatsappUrl.'" target="_blank">Whatsapp</a>'; $sosbutton .= '</div>'; //showing share button before content return $sosbutton.$content; // to showing share button bottom of content use the following code : // echo $content.$sosbutton; }else{ return ''; } }; add_filter( 'the_content', 'sp_social_sharing_buttons'); |
Add the following CSS code to the style.css file in your theme folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .sp-social a{ padding: 10px; text-align: center; border-radius: 5px; color: white; font-weight: bold; margin-right: 5px; border-radius: 45px; box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1); } .sp-twitter{ background-color: #1C9CEA; } .sp-facebook{ background-color: #1774EA ; } .sp-whatsapp{ background-color: #4FCE5D; } .sp-telegram{ background-color: #34A7D9; } |
2. Create a WordPress share button for the Genesis framework
Like this website, I use the Genesis framework to create themes. The use of the genesis framework is different from the standard theme. Use the following code.
Add the following code in functions.php in the genesis child theme folder you are using.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function sp_social_sharing_buttons($content) { global $post; if(is_single() or is_page()){ // will show share button in post or page article $spURL = urlencode(get_permalink()); $spTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8'); $spThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); $twitterURL = 'https://twitter.com/intent/tweet?text='.$spTitle.'&url='.$spURL.'&via=sp'; $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$spURL; $whatsappUrl = 'whatsapp://send?text='.$spTitle.' '.$spURL; $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$spURL.'&media='.$spThumbnail[0].'&description='.$spTitle; $telegramUrl = 'https://t.me/share?url='.$spURL; $sosbutton = '<div class="sp-social">'; $sosbutton .= '<p> Share : <a class="sp-link sp-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>'; $sosbutton .= '<a class="sp-link sp-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>'; $sosbutton .= '<a class="sp-link sp-telegram" href="'.$telegramUrl.'" target="_blank">Telegram</a>'; $sosbutton .= '<a class="sp-link sp-whatsapp" href="'.$whatsappUrl.'" target="_blank">Whatsapp</a>'; $sosbutton .= '</div>'; echo $sosbutton; }else{ echo ''; } wp_enqueue_style( 'sp_social_sharing_buttons_css', plugin_dir_url( __FILE__ ) .'sp_social_sharing_buttons.css',array(),'1.0' ); }; add_action('genesis_entry_header', 'sp_social_sharing_buttons', 13 ); |
Add CSS to the previous code to enhance the sharing button.
In this example, I place the sharing button after the Entry Titles and Entry Meta. For other placement locations, please replace genesis_entry_header with the other genesis hook. You can see the reference hooks here.
3. Wrap your share button as a plugin.
I consider this important because the title of this article makes a WordPress share button without a plugin, but I teach you to make a share button plugin. My point is that the plugins you create are to represent your desires and don’t need a JS file that makes your website’s speed slow down. Also, this plugin can be used on all your websites without edit the file function in the theme folder.
1. Create a folder with name sp_social_sharing_buttons
2. Create a PHP file in the sp_social_sharing_buttons folder with the name sp_social_sharing_buttons.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function sp_social_sharing_buttons($content) { global $post; if(is_single() or is_page()){ // will show share button in post or page article $spURL = urlencode(get_permalink()); $spTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8'); $spThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); $twitterURL = 'https://twitter.com/intent/tweet?text='.$spTitle.'&url='.$spURL.'&via=sp'; $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$spURL; $whatsappUrl = 'whatsapp://send?text='.$spTitle.' '.$spURL; $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$spURL.'&media='.$spThumbnail[0].'&description='.$spTitle; $telegramUrl = 'https://t.me/share?url='.$spURL; $sosbutton = '<div class="sp-social">'; $sosbutton .= '<p> Share : <a class="sp-link sp-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>'; $sosbutton .= '<a class="sp-link sp-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>'; $sosbutton .= '<a class="sp-link sp-telegram" href="'.$telegramUrl.'" target="_blank">Telegram</a>'; $sosbutton .= '<a class="sp-link sp-whatsapp" href="'.$whatsappUrl.'" target="_blank">Whatsapp</a>'; $sosbutton .= '</div>'; //showing share button before content return $sosbutton.$content; // to showing share button bottom of content use the following code : // echo $content.$sosbutton; }else{ return ''; } wp_enqueue_style( 'sp_social_sharing_buttons_css', plugin_dir_url( __FILE__ ) .'sp_social_sharing_buttons.css',array(),'1.0' ); }; add_filter( 'the_content', 'sp_social_sharing_buttons'); |
3. Create a CSS file in the sp_social_sharing_buttons folder with the name sp_social_sharing_buttons.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .sp-social a{ padding: 10px; text-align: center; border-radius: 5px; color: white; font-weight: bold; margin-right: 5px; border-radius: 45px; box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1); } .sp-twitter{ background-color: #1C9CEA; } .sp-facebook{ background-color: #1774EA ; } .sp-whatsapp{ background-color: #4FCE5D; } .sp-telegram{ background-color: #34A7D9; } |
4. Compress the folder with ZIP format.
5. Upload and install the plugin on your all WordPress site
Note : you can adding social sharing icons to beautify your button in your CSS file.
Conclusion :
- Adding social button is important to increase your SEO rank.
- Not all the best-share button plugins and have a high review can adjust to your site’s needs.
- Too many Javascript files interfere with your WordPress hosting performance and websites speed. Check your website’s performance at pagespeed google.
- Please don’t use the official social media sharing buttons, because it contains Javascript code for every social media.
- Website performance determines your position in the Google search engine.
Leave a Reply