Add a “Share on Facebook” Button in WordPress Posts

Estimated reading time: 4 min

If you want a manual function in WordPress that allows you to share posts on your Facebook Page with a button, you can achieve this using a custom function in your theme’s functions.php file. Here’s how:


Title: Add a “Share on Facebook” Button in WordPress Posts

Why Add a Facebook Share Button?

Adding a “Share on Facebook” button to your WordPress posts allows visitors to easily share your content with their audience, increasing engagement and traffic to your website.

Method 1: Using a Plugin (Easy Way)

If you want a quick solution without coding, you can use a plugin like AddToAny Share Buttons or Shared Counts.

Steps to Install a Plugin:

  1. Log in to your WordPress dashboard.
  2. Go to Plugins > Add New.
  3. Search for AddToAny Share Buttons or Shared Counts.
  4. Click Install Now and then Activate.
  5. Go to Settings > AddToAny and configure the button display.
  6. Save changes, and the share button will appear on your posts.

Method 2: Adding a Custom Facebook Share Button (Manual Code)

If you prefer a manual approach, you can add a custom Facebook Share button using a simple code snippet.

Steps to Add the Code:

  1. Open your WordPress functions.php file (or use a custom plugin for better management).
  2. Add the following function:
function custom_facebook_share_button($content) {
    if (is_single()) {
        $post_url = get_permalink();
        $post_title = get_the_title();
        $fb_share_url = "https://www.facebook.com/sharer/sharer.php?u=" . urlencode($post_url);
        
        $button_html = '<div style="margin-top:20px;">
            <a href="' . esc_url($fb_share_url) . '" target="_blank" style="background:#1877F2; color:#fff; padding:10px 15px; text-decoration:none; border-radius:5px; font-weight:bold;">
                πŸ“’ Share on Facebook
            </a>
        </div>';
        
        $content .= $button_html;
    }
    return $content;
}
add_filter('the_content', 'custom_facebook_share_button');

What This Code Does:

  • It checks if the post is a single post (not homepage or archive).
  • It generates a Facebook Share URL for the post.
  • It appends a styled button at the end of the post.

Method 3: Using a Shortcode (More Flexibility)

If you want to place the button anywhere in your post, you can use a shortcode.

Add this code to functions.php:

function facebook_share_shortcode() {
    $post_url = get_permalink();
    $fb_share_url = "https://www.facebook.com/sharer/sharer.php?u=" . urlencode($post_url);
    
    return '<a href="' . esc_url($fb_share_url) . '" target="_blank" style="background:#1877F2; color:#fff; padding:10px 15px; text-decoration:none; border-radius:5px; font-weight:bold;">πŸ“’ Share on Facebook</a>';
}
add_shortcode('facebook_share', 'facebook_share_shortcode');

How to Use the Shortcode:

Simply insert [facebook_share] anywhere in your post where you want the button to appear.


Method 4: Adding a Manual Share Button in Admin Dashboard

If you want a button inside the WordPress admin panel to manually share a post, you can add this to the post edit screen.

Steps:

  1. Add the following function to functions.php:
function add_facebook_share_meta_box() {
    add_meta_box('facebook_share', 'Manual Facebook Share', 'facebook_share_meta_box_callback', 'post', 'side', 'high');
}
add_action('add_meta_boxes', 'add_facebook_share_meta_box');

function facebook_share_meta_box_callback($post) {
    $post_url = get_permalink($post->ID);
    $fb_share_url = "https://www.facebook.com/sharer/sharer.php?u=" . urlencode($post_url);
    echo '<a href="' . esc_url($fb_share_url) . '" target="_blank" class="button button-primary">πŸ“’ Share on Facebook</a>';
}

What This Does:

  • Adds a “Manual Facebook Share” button inside the WordPress post editor.
  • Clicking it opens Facebook’s share window for the current post.
  • You can customize the caption before posting.

Manual Facebook Share

Final Thoughts

This is a manual function that allows you to share posts on Facebook with a simple button:

  • Frontend Button: Adds a button below each post for visitors.
  • Admin Dashboard Button: Lets admins share posts directly from WordPress.

Conclusion

Adding a Facebook Share button to WordPress posts helps increase visibility and engagement. Whether you use a plugin, custom code, or shortcode, you now have multiple options to make sharing easy for your audience.

Would you like additional features, such as tracking shares or adding other social platforms? πŸš€

Let me know if you need any modifications or enhancements! πŸš€


Discover more from HelpZone

Subscribe to get the latest posts sent to your email.

Want to support us? Let friends in on the secret and share your favorite post!

Photo of author

Flo

Add a “Share on Facebook” Button in WordPress Posts

Published

Update

Welcome to HelpZone.blog, your go-to hub for expert insights, practical tips, and in-depth guides across technology, lifestyle, business, entertainment, and more! Our team of passionate writers and industry experts is dedicated to bringing you the latest trends, how-to tutorials, and valuable advice to enhance your daily life. Whether you're exploring WordPress tricks, gaming insights, travel hacks, or investment strategies, HelpZone is here to empower you with knowledge. Stay informed, stay inspired because learning never stops! πŸš€

πŸ‘ Like us on Facebook!

Closing in 10 seconds

Leave a Reply