Skip to content
Be-smart

Be-smart

  • Home
  • Blog
    • Most Visited Posts
    • Stories
    • Unisex
      • Kids
      • Man
      • Woman
    • ADS
    • Social Media
  • About
    • Contact
  • Login

Add a Like button to your WordPress posts that links directly to the post’s permalink

Photo of author
Flora
18/01/2025
Views: 15

Estimated Reading Time: 3 min

To add a “Like” button to your WordPress posts that links directly to the post’s permalink on Facebook, you can embed Facebook’s Like Button code in your WordPress theme or use a plugin for more flexibility.


Option 1: Manual Code Addition

Generate the Facebook Like Button Code

Go to the Facebook Like Button Generator.

Enter the URL as https://yourdomain.com/your-post-url. For dynamic posts in WordPress, this will be replaced with the post permalink.

Configure the options (layout, width, etc.).

Copy the generated HTML code.

Add the Code to Your Theme Edit your theme’s single.php or wherever you want the Like button to appear.

<?php
// Get the post permalink dynamically
$post_url = get_permalink();
?>

<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v17.0"></script>

<div class="fb-like" 
     data-href="<?php echo esc_url($post_url); ?>" 
     data-width="" 
     data-layout="button_count" 
     data-action="like" 
     data-size="small" 
     data-share="true">
</div>

Save and Test

Save the file and open a post on your WordPress site.

You should see the Like button.


Option 2: Use a Plugin

If you want a more user-friendly solution:

  1. Install a plugin like AddToAny Share Buttons or Social Media Share Buttons.
  2. Configure the plugin to include a Facebook Like button on your posts.
  3. The plugin will automatically handle the dynamic URL and placement.

Option 3: Use a Shortcode

To make it reusable, you can create a shortcode for the Like button. Add the following code to your theme’s functions.php:

function facebook_like_button_shortcode() {
    $post_url = get_permalink();
    return '
    <div id="fb-root"></div>
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v17.0"></script>
    <div class="fb-like" 
         data-href="' . esc_url($post_url) . '" 
         data-width="" 
         data-layout="button_count" 
         data-action="like" 
         data-size="small" 
         data-share="true">
    </div>';
}
add_shortcode('facebook_like', 'facebook_like_button_shortcode');

To use the shortcode, add [facebook_like] to any post or page.


Customization Options

  • Button Layouts: Change the data-layout attribute to standard, box_count, or button_count.
  • Include Share Button: Set data-share="true" to include a share option.
  • Styling: Use CSS to adjust the appearance of the Like button container.

Would you like help customizing this further or debugging integration issues?

Categories Html, WordPress
Here are 20 actionable tips to write better and more efficient PHP functions
Create wordpress template page with latest post in format image an title link
Share on Facebook Share on WhatsApp Share on X (Twitter) Share via Email

Related Posts

  • How to Allow Upload of Custom MIME Types in WordPress
  • How many tags are normal in a wordpress post
  • How to add code to head (via functions.php)
  • ✅ Boost WordPress Speed & Security with These PHP Tricks
Photo of author

Flora

Add a Like button to your WordPress posts that links directly to the post’s permalink

Published 18/01/2025

Update 19/01/2025

Contact

  • X
  • TikTok
  • Facebook
  • WhatsApp
I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.

Bitcoin Price (BTC)

$105,296.20

Download BTC Ticker

Trending Posts

  • Here are ten of the most iconic and celebrated BMW models of all time
  • BMW M4 Competition (F82): Redefining Performance and Style
  • How to Clear Cache BlueHost with Cron Job Command
  • Legendary ‘Killing Me Softly’ singer, dead at 88
  • Euro 2024 Qualification Groups: All You Need to Know

Recent Posts

  • Fresh and Healthy Heart of Palm Salad Recipe
  • Ultimate WordPress Countdown Timer: PHP, CSS & JavaScript Guide
  • How to create a related posts section
  • How to uninstall Windows programs using a batch script
  • Here are 5 cool WhatsApp tricks you might not know about:

Archives

  • March 2025 (1)
  • February 2025 (65)
  • January 2025 (94)
  • December 2024 (42)
  • November 2024 (69)
  • October 2024 (92)
  • September 2024 (31)

Recent Comments

No comments to show.

Partners Group

Amazon AliExpress BlueHost
GeneratePress SnepBelgium Takeaway

BlogRoll

Mirror

The MixedZone

Finance Digest

  • Facebook
  • X
  • WhatsApp
  • RSS Feed
  • Facebook
  • X
  • WhatsApp
  • RSS Feed

BSB (228) Celebrity (24) Europe (37) Food (14) Html (41) Lifestyle (23) Love (18) Medical (15) Movies (18) My Health (28) PC World (36) PHP (22) Science (15) Social Media (13) Software (14) Stories (96) Weekly Horoscopes (13) Windows (29) Woman (20) WordPress (76)

  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Cookie Policy

© 2024 Be-smart

All Rights Reserved

Built with GeneratePress

This website uses cookies to ensure you get the best experience. By continuing to browse, you agree to our Privacy Policy.