Social Media Buttons in Joomla 2.5

Where to embed?
Different types of button bars
  Share42 horizontal panel
  Share42 floating vertical panel
Selective placement


While writing an article about the customization of page 404, I raised the issue of how to get rid of unwanted items in a page interface. Another article is devoted to the selective hiding of the breadcrumbs module. I also came to the conclusion not to display social buttons on page 404. There are other cases where it's worth to hide social media icons on certain pages, or vice versa - to display them only on some. Arranging articles of this series, I just faced a similar need: some button panels are displayed for illustration only in the relevant articles. So I decided to add this topic to the series.

So, let's specify a condition to filter the output of the buttons panel. If the goal is to display it only on the pages of certain articles, then for each of them it takes the following form:

$article_id = explode(':', JRequest::getVar('id'));
if (JRequest::getVar('view') == 'article' &&
    $article_id[0] == '[Article ID]') ...

The value of [Article ID] can be easily determined in the list of articles, or else in the edit article interface - both in the administrative service. Accordingly, for hiding the buttons on some pages the negation operator ! is being added to the above construction.

Thus, the code of the button bar that is inserted into the file of article template as described in the first article of this series, is framed with conditional statement if. For example, on my bilingual website IDs of Russian and English pages 404 are 80 and 448 respectively. The code snippet of the social buttons panel is as follows:

<?php
  $article_id = explode(':', JRequest::getVar('id'));
  if (!(JRequest::getVar('view') == 'article' &&
        ($article_id[0] == '80' || $article_id[0] == '448'))) {
?>
  /* =====> !!! Script of social media buttons is HERE */
<?php } ?>

Replace 80 and 448 with values of your ​​pages' identifiers. By the way, if anyone interested, when SEF is enabled, the condition might look simpler:

if (JRequest::getVar('id') == '[Article ID]') ...

Hopefully you have been using SEF, but certainly there are cases when Joomla users are turning it off for one reason or another. And if in the former case JRequest::getVar('id') returns an identifier string, then in the second it returns the same, but with the article's alias as a considerable adjunct. The alias is separated from the number by a colon. For instance, when SEF is on, the string of this article's ID is '723', otherwise it would be '723:social-media-buttons-in-joomla-2-5-selective-placement'. Function explode has being included to make the condition universal for both options, and it guarantees you can disable SEF with no hidden side effects in the logic of the output of social service buttons in your Joomla web site pages.

Add comment