How to Add a Facebook Like Button in Drupal
In this article
To add a Facebook Like button in Drupal, the most reliable method is to use the Social Media Share module (or a similar social sharing module) and configure the Facebook Like button through its settings, or to paste Facebook's official iframe embed code into a block or a field. The module approach requires no coding and keeps the button consistent across content types; the embed approach works on any Drupal version but needs manual placement. Before you start, make sure your site is publicly accessible (Facebook needs to fetch the page), that you have permission to edit blocks or content types, and that you know which Drupal version you are running (the steps below cover Drupal 8, 9, and 10, which share the same module system).
What You'll Need
- Administrator access to your Drupal site (ability to install modules and edit blocks).
- A publicly reachable URL — Facebook's Like button will not render correctly on localhost or behind a strict firewall.
- The Facebook Like button embed code from Facebook's developer tools (you can generate it at developers.facebook.com/docs/plugins/like-button without an app ID, though an app ID is recommended for analytics).
- Optional: the Social Media Share module (machine name: social_media_share) or the ShareThis module if you prefer a no-code configuration.
Step-by-Step
- Decide on your method. Use a contributed module if you want the button added automatically to all nodes of a content type. Use a custom block with the iframe embed if you want precise placement or need to support a specific Drupal version without extra modules.
- Install the module (module method). Go to Extend (/admin/modules), search for “Social Media Share” or “ShareThis,” check the box, and click Install. If the module is not listed, download it from drupal.org and install it via the Extend page's “Install new module” option (or with Composer:
composer require drupal/social_media_share). - Configure the module. After installation, go to Configuration > Web services > Social Media Share (the exact path may vary slightly by module version; look for a “Social Media” section under Configuration). Enable the Facebook Like button, choose the button layout (standard, button_count, box_count), and select which content types should display it.
- Set visibility. In the module's settings, choose whether the button appears on full content pages, teasers, or both. Save the configuration.
- Clear the cache. Go to Configuration > Development > Performance and click “Clear all caches.” This ensures the new block or field appears.
- Alternative: use a custom block (embed method). Go to Structure > Block layout > Custom block library > Add custom block. Give it a title like “Facebook Like” and in the body, switch the text format to “Full HTML” (or a format that allows iframes). Paste the Facebook iframe code you generated. Save the block.
- Place the block. In Block layout, find your new block in the list and click “Place block” next to the region where you want it (for example, Sidebar first or Content below). Configure visibility (e.g., only on Article content type) and save.
- Test the button. Visit a page where the button should appear. If you see a blank space or an error, check that the page is publicly accessible and that the iframe code is correct. Use your browser's developer tools to look for blocked content or JavaScript errors.
Common Mistakes
- Using a private or local URL. Facebook's servers must be able to reach the page to render the Like button. Localhost or intranet sites will show an error or nothing.
- Forgetting to clear caches. Drupal caches block and module configuration aggressively; changes often do not appear until you clear caches.
- Pasting the embed code into a text format that strips iframes. Use Full HTML or a custom text format that allows iframe tags. Otherwise the button will not render.
- Placing the block in a region that is not displayed on the intended pages. Check the block's visibility settings and the theme's region layout.
- Using an outdated embed code. Facebook occasionally updates its embed code; always generate fresh code from the official Like Button configurator.
Tips
- Use an app ID in the embed code if you want to access Facebook Insights for your domain. You can create a free app at developers.facebook.com.
- For performance, consider loading the Facebook SDK asynchronously (the default embed code does this) and placing the button below the fold.
- If you need the button on many content types, the module method is easier to maintain than editing each node.
- Test on mobile: the iframe embed is responsive by default, but check that it does not overflow narrow screens.
- Keep the button size consistent with your theme; the “large” button may look out of place on a minimal design.
Troubleshooting
If the button does not appear at all, first clear Drupal's caches and check that the block is enabled and assigned to a visible region. If you see a blank box or an error message inside the iframe, the most common cause is that Facebook cannot reach your page — verify the URL is public and not password-protected. If the button appears but the count is zero or incorrect, Facebook may not have crawled the page yet; use the Sharing Debugger at developers.facebook.com/tools/debug to scrape the page again. If you are using a module and the button shows on the wrong pages, review the module's visibility settings and the block's page-specific visibility. If the button breaks your site's layout, wrap the iframe in a div with a fixed width or use the module's built-in styling options. For JavaScript errors in the browser console, check that no other script is blocking Facebook's SDK; try disabling other social modules temporarily to isolate the conflict.
Frequently Asked Questions
Do I need a Facebook app ID to add a Like button?
No, the Like button works without an app ID, but an app ID lets you use Facebook Insights and ensures the button is associated with your domain. You can create a free app at developers.facebook.com.
Will the Like button work on a Drupal site that is not public yet?
No. Facebook's servers must be able to fetch the page to render the button. On a private or local site, the button will not display correctly. You can test on a staging site only if it is publicly accessible.
Can I add the Like button to specific content types only?
Yes. If you use a module like Social Media Share, you can select which content types display the button. With a custom block, use the block's visibility settings to restrict it to pages of a certain type.
Why does the Like button show a “Confirm” or error message?
This usually means Facebook cannot access the page or the URL is malformed. Check that the page is public, that the URL in the iframe matches the canonical URL, and that no redirects or authentication are blocking Facebook's crawler.
Does the Like button slow down my Drupal site?
The standard embed code loads asynchronously and has minimal impact. However, if you place many social widgets, consider lazy-loading them or using a module that defers loading until the user scrolls.
Can I customize the appearance of the Like button?
You can choose from Facebook's predefined layouts (standard, button_count, box_count) and sizes (small, large). Custom CSS styling is limited because the button is rendered inside an iframe; you can only control the container around it.