Facebook like button not displaying properly - wordpress

My goal is to have dynamic Facebook like buttons using a php variable in the url. I figured I'd first just try inserting a static like button to test it out, but I'm encountering some issues. When I first loaded the page, after inserting the FB code, the like button displayed correctly. Though, every time after that it just displays a white box, the size of the like button content, which flickers, then disappears.
My situation may be unique as this is a wordpress site, but I don't use any of the wordpress features, such as the post loop, or any design layout features. I just use wordpress to publish posts, which get forwarded to my own database, and I display them with my own code.
Right after <?php get_header(); ?> in my index.php, I've put:
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=293105884122762";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
Then in a random place on the site I've put:
<fb:like href="mysite.com" send="false" layout="button_count" width="450" show_faces="true"></fb:like>
I know I need to add the XML namespace for IE, but I figured it wasn't necessary yet.
If anyone has any suggestions, I'd really appreciate them.

include the below lines of code on the page where your like button is:
<script>
if (typeof(FB) != 'undefined' && FB != null ) {
FB.XFBML.parse();
}
</script>
hope it helps. this had solved my very similar problem.

The Facebook code just runs inline, so if it isn't included at the bottom of the page, you have to call FB.XFBML.parse() to re-parse the page HTML looking for <fb:/> tags. Another option would be to either place this javascript at the bottom of the page, or better yet use a hook in your functions.php to include it in wp_footer.
<?php
// this function will output the facebook javascript code
function add_facebook_javascript(){ ?>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=293105884122762";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<?php
}
// this will hook your function to the "wp_footer" action
add_action('wp_header', 'add_facebook_javascript');
?>

Related

How to remove facebook comment box on PAGES & only show on POSTS?

I'm using Wordpress for my website. I did add Facebook comment box to my website successfully with qualified user id and app id.
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.8&appId=165596420562496";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
...but I don't know how to remove/disable/delete/cancel facebook comment box on my PAGES and only show it on my POSTS. I've been struggling and sad many days without the solution to this (because I'm really bad at coding new things). I'd be grateful to anyone helping me with this issue. Note: I've tried using many different plugins for this matter (since I know nothing) but not once has worked. Please help!
You could add the following code to your functions.php (child theme).
function facebook_comment_add_to_content( $content ) {
if( is_singular( 'post' ) ) {
$content .= "<div id='fb-root'></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = '//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.8&appId=165596420562496';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script> <h1>Yhea men</h1>";
}
return $content;
}
add_filter('the_content', 'facebook_comment_add_to_content');
Note: you can also use https://nl.wordpress.org/plugins/facebook-comments-plugin/ in the settings tab you can enable 'Singular Posts'. Then the facebook comment section will not display on pages (other custom post types will show the facebook section).

Official twitter widget fails in Wordpress blog

I started blogging on free wordpress platform and I wanted to add official twitter widget to my blog.
I add following code as text widget to blog's side bar in twenty fourteen theme.
<a class="twitter-timeline" href="https://twitter.com/Menuka_cs3" data-widget-id="695529194466504704">Tweets by #Menuka_cs3</a>
<script>
! function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
p = /^http:/.test(d.location) ? 'http' : 'https';
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = p + "://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");
</script>
but it look like this
what I was able to detect when I save the widget it lost it's <script> tags so it just show the content letters. Here is what I get when I save the widget
<a class="twitter-timeline" href="https://twitter.com/Menuka_cs3">Tweets by #Menuka_cs3</a> !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
Please help. Thanks
I think you are trying to add code javasript in text widget of WordPress.com blogs. You can't.
Users are not allowed to post JavaScript on WordPress.com blogs.
JavaScript can be used for malicious purposes. As an example,
JavaScript has taken sites such as MySpace.com and LiveJournal offline
in the past. The security of all WordPress.com blogs is a top priority
for us, and until we can guarantee scripting languages will not be
harmful, they will not be permitted.
JavaScript from trusted partners, such as YouTube and Google Video, is
converted into a WordPress shortcode when a post is saved.
https://en.support.wordpress.com/code/#javascript
But if you are in WordPress.com VIP, and need to embed your Twitter timeline, you could follow this post How To Embed a Twitter Timeline Widget.
When Wordpress saves the content, it filters it because it thinks its text, not code. One possible solution is to create a shortcode like this:
Open you theme/functions.php file and paste this at the end
function twitter_shortcode( $atts) {
$twitter = '<a class="twitter-timeline" href="https://twitter.com/Menuka_cs3" data-widget-id="695529194466504704">Tweets by #Menuka_cs3</a>
<script>
! function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
p = /^http:/.test(d.location) ? 'http' : 'https';
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = p + "://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");
</script>';
return $twitter;
}
add_shortcode( 'twitter', 'twitter_shortcode' );
Then, on your post you call the shortcode:
[twitter]
Please note that this function is completely untested but was taken from the official wordpress page at:
https://codex.wordpress.org/Shortcode_API
Try this code
<a class="twitter-timeline" href="https://twitter.com/Menuka_cs3" data-widget-id="710522346977824769">tweets by #Menuka_cs3</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

Setting up Facebook Comments and Like Button on Wordpress

I would like to install Facebook Comments and Like button for Wordpress blog. So I set up this block of code right after <body>. The first part is Facebook SDK set for web, and I think that there is no error here. And the second part is a block of code for web application. (I have replaced appId with XXXXX.)
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXX',
xfbml : true,
version : 'v2.4'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.4&appId=XXXXXX";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
Then I place Facebook Like Button right after single post.
<div class="fb-like" data-layout="box_count" data-action="like" data-show-faces="true" data-share="false"></div>
And Facebook Comments after it.
<div class="fb-comments" data-numposts="3" data-width="100%" data-href="http://<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ?>" data-mobile="false"></div>
But in my Wordpress temp demo, Facebook Comments shows up and Like Button does not. Is there any wrong here?
Originally, I'd like to make the Facebook Comments flexible so give it a value 100% width. But in that case, I also unintentionally specified some div inside Facebook Like Button. So the 100% width of the child just make the parent collapsed, and the whole Like Button collapsed.
You can check the whole current structure of Facebook Like Button in this temp demo or image.

Trying to get the Facebook SDK for JavaScript to work on my wordpress website

i have been trying to get the Facebook SDK for JavaScript working on my wordpress website for a few days now but it keep failing for some reason
i followed the exact steps on this page https://developers.facebook.com/docs/javascript/quickstart/v2.2
i got it to work yesterday (the like button showed up in my header) but when i refresed the website today it was gone and the SDK code stopped working.
i have no idea how it happend since i didn't change anything on the website so i redid the whole thing but without results.
this is how most of my header.php (where the code is suppose to be) looks like
</head>
<body <?php body_class(); ?> data-layout="<?php echo esc_attr( stag_site_layout() ); ?>">
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<?php
/**
* Aesop Story Engine theme hook.
*
* #since 1.1.0
*
*/
do_action('ase_theme_body_inside_top');
?>
<div
class="fb-like"
data-send="true"
data-width="450"
data-show-faces="true">
</div>
as you can see the code is directly below the body code. and below that the facebook like code, but it's not working...
if anyone could help me with this than that would be really great.
Thanks!
You need to create a new App ID here:
https://developers.facebook.com/docs/opengraph/getting-started#create-app
Choose "Website" as the platform.
Once you create a new app, you will see your unique code for "appID". In your code above where it says "your-app-id", place in your new unique code.
Also, your code can be updated to v2.3:
https://developers.facebook.com/docs/javascript/quickstart/v2.3

Facebook comment in asp.net with dynamic urls

I am creating facebook comment box,it works fine with the static urls, but my url contains the query string parameters with it like this: http://bugnull.com/article_content.aspx?id=19&title=code+testing.
I want to have the different comments for each unique url.
Here is the code I am using :
Code just after the body tag:
<div id="fb-root"></div>
<script> (function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=XXXXXXXXXXX";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));</script>
**and code for the comment box:**
<div class="fb-comments" data-href="http://bugnull.com/article_content.aspx?id=(.*)&title=(.*)" data-num-posts="2" data-width="500"></div>
Here I want to change the url such that the above url is manipulated such that it allows me to have unique comment thread for each unique page.
I found the solution which was quite fascinating,here is the manipulation in the code required:
In the div tag :
<div id="fbdiv" runat="server "class="fb-comments" data-href="http://bugnull.com/article_content.aspx?id=(.*)&title=(.*)" data-num-posts="2" data-width="500"></div>
I included runat="server" and in aspx.cs it was accessible:
fbdiv.Attributes["data-href"]="xyz.com?title="+Request.QueryString["title"];
You can simply have this in your javascript code (in case you're using jQuery)
$("#fbdiv").attr("data-href", document.location.href);
Try this
<div class="fb-comments" data-href="http://www.zajelcard.com/<%=Request.QueryString("pagename")%>" data-numposts="5"></div>

Resources