0

I want to include the facebook JSSDK only on pages where I want to use it. On the facebook dev site it sais that ideally its included right after the opening body tag, which sits in header.php

My thought was to add in header.php

<?php do_action( 'topofthetop'); ?>

right after the opening body tag.

And then later (in comments.php just before <?php if ( have_comments() ) : ?>), if I need to call

add_action( 'topofthetop', array('someclass','FB_JS_SDK') );

FB_JS_SDK simply echoes the code needed to included the FB-JSSDK. But the code doesn't appear after the body tag. I guess because header.php is allready output before my add_action call?

How could I solve this?

  • When exactly do you call add_action()? – fuxia Dec 28 '13 at 18:01
  • in comments.php just before "if ( have_comments() ) :" (I'm using a twenty twelve child theme) – cytofu Dec 28 '13 at 18:09
  • This cannot work. Try registering the callback before you use add_action(). – fuxia Dec 28 '13 at 18:10
0

You must register callbacks before they are called (add_action() and add_filter() are registration functions, they remember what to do when).

Try the following in your functions.php or in your plugin:

add_action( 'topofthetop', 'load_FB_JS_SDK' );

function load_FB_JS_SDK()
{
    if ( is_singular() && have_comments() )
        someclass::FB_JS_SDK();
}
  • but how do I then manage to run someclass::FB_JS_SDK() only on the pages I want to use it? – cytofu Dec 28 '13 at 18:16
  • Inspect the current page in the callback function. There are many other conditional functions available and the global $post object too. – fuxia Dec 28 '13 at 18:20
  • one more question on this, as far as I understand it, the callback is run, when do_action is called, therefore right after the body tag. Doesn't that mean I'm not yet in the loop? How does have_comments() work then? – cytofu Dec 28 '13 at 18:38
  • have_comments() looks at the global $wp_query object. This is already set up, otherwise WordPress couldn’t even load the template. – fuxia Dec 28 '13 at 18:41
  • so it refers to comments for the page, not to comments made on a post? – cytofu Dec 28 '13 at 18:46
0

Why don't you hook it in from your child themes functions file using a conditional tag.

function facebook_comments_sdk_script() {


wp_enqueue_script( 'facebook-sdk', get_bloginfo( 'stylesheet_directory' ) . 

'/js/fb-comments.js', array(), '1.0.0', true );

}

add_action( 'wp_enqueue_scripts', 'facebook_comments_sdk_script' );


function load_facebook_comments_form() {

if ( comments_open() ) {

echo '<div class="fb-comments" data-href="http://example.com/comments"   

data-numposts="5" data-colorscheme="light"></div>';

   }

}

add_action('comment_form', 'load_facebook_comments_form');

I also removed the scripts tags from the Javascript SDK and put it in a file like this:

(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=123456789224439";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

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.