Skip to content

Instantly share code, notes, and snippets.

@iSWORD
Last active July 27, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iSWORD/7c108e9d3d479c602b07b05ecfd3389f to your computer and use it in GitHub Desktop.
Save iSWORD/7c108e9d3d479c602b07b05ecfd3389f to your computer and use it in GitHub Desktop.
Facebook Like button code snippet for WordPress
<?php
add_filter('the_content', function ($content) {
$combine_fn = function ($glue, $pattern, $array) {
return implode($glue, array_map(function ($value, $key) use ($pattern) {
$ret = $pattern;
$ret = str_replace('{$key}', $key, $ret);
$ret = str_replace('{$value}', $value, $ret);
return $ret;
}, $array, array_keys($array)));
};
if(is_singular('post')) {
// Edit values below
$width = 450;
$height = 80;
$params = [
'layout' => 'standard',
'action' => 'like',
'size' => 'small',
'show_faces' => 'true',
'share' => 'false',
];
$styles = [
'border' => 'none',
'overflow' => 'hidden',
];
$attributes = [
'scrolling' => 'no',
'frameborder' => '0',
'allowTransparency' => 'true',
'allow' => 'encrypted-media',
];
// Stop editing here
$params['href'] = get_the_permalink();
$attributes['width'] = $params['width'] = $width;
$attributes['height'] = $params['height'] = $height;
$attributes['src'] = "https://www.facebook.com/plugins/like.php?" . http_build_query($params);
$attributes['style'] = $combine_fn('; ', '{$key}: {$value}', $styles);
$attributes_combined = $combine_fn(' ', '{$key}="{$value}"', $attributes);
$content .= "<iframe {$attributes_combined}></iframe>";
}
return $content;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment