3

So, I've just recently added the Facebook Javascript SDK to my site to be able to enable a "like" button to my pages.

Facebook recommends adding this right after the opening <body> tag. The only I could find to do this was to modify the index.php file of my template.

The danger here is that my code will be wiped if the template is updated. This is not a huge deal, but would make future-proofing the site a lot easier to do this correctly.

So, what's the best way to do this?

2

Complementing the answer from @veritas399, you could also use a third party extension such as EasyScript or similar to insert the code into your web page without worrying about future template or other updates overwriting your code.

Facebook may recommend adding this right after the opening <body> tag but this may not be the best advice from a performance point of view and is likely not essential.

2

If you don't want to edit the template's index.php file, then I'd suggest that you use a plugin and utilise JApplication to get and set the body.

You can then use the following in your plugin:

public function onAfterRender()
{
    $app = JFactory::getApplication();

    if ($app->isSite())
    {
        $buffer = $app->getBody();

        $insert = '<div>YOUR HTML CODE TO INSERT</div>';

        $buffer = preg_replace('/<body[^>]+>\K/i', $insert, $buffer);

        // Use the replaced HTML body.
        $app->setBody($buffer);
    }
}

The regular expression finds the opening body tag and any attributes that you may have inside it, then "forgets the matched string" with the \K metachatacter so that the $insert text is immediately added after the opening body tag (no text will be removed).

1

Is your template based on one of the popular frameworks? (Gantry 5 by RocketTheme, Warp by YooTheme, T3 by JoomlaArt, etc.) If so, that framework provider should have documented how and where to make changes that will survive upgrades. For example, one can add custom code to Gantry 5 templates in the head and body tags on the admin panel.

  • I'm not sure. It's Theme 1498 by template monster, but thanks I will look into their documentation. – nizz0k Jan 20 '17 at 9:34
1

To add to @Neil Robertson's answer - there are tons of plugins/extensions that implement Facebook Like/Share and other Social Sites Integrations - which might be a better alternative for you, since your actual requirement is to simply add FB Like Buttons.

Have a look at JED:

Also, some templates provide such features and all you have to do is to add your own FB App ID - so make sure you have search in your template about this possibility.

  • I saw the plugins, and I've considered looking under the hood to see how they add the SDK. I chose not to go with a plugin because most seemed to have more functionality than I needed, and seemed like overkill to add a few lines of code. – nizz0k Jan 20 '17 at 9:36
0

The template usually would be the place to do that. You could add the code to a separate facebook.php file in yout template directory and the add that to the index.php file through a php include:

<body>
<?php include('facebook.php'); ?>
...
  • 1
    I had just read that template updates would overwrite anything in the index.php; don't know how often or likely that is – nizz0k Jan 19 '17 at 15:07
0

Or, alternately, instead of installing a bunch of stuff, you could put it where you want it and then right click and select all and copy and paste it into a wordpad file, save it and name it SITE-index-php and then just copy that file back in any time there's an update. Takes like 1 minute and nothing else to install or mess around with.

  • This doesn't seem like professional-grade advice. Using this technique will require the developer to do more work in the long run and there is the risk of forgetting to overwrite the content on each upgrade. – mickmackusa Nov 27 '18 at 19:33
  • You're probably right. I just try to minimize installing extensions thinking the more there are the more vulnerable your site becomes. – Mythic Nov 29 '18 at 11:59

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.