Need help adding javascript

  • Chris

    #4851

    First of all I want to reiterate that this theme is awesome. I just read an article that was suggested on this site about how to add action hooks and widgets, and it opened up so many options. Also read the interview with Syahir. Dude, you are brilliant.

    I’m trying to add the javascript that is necessary for the facebook like button and activity box and such. I had a plug-in, but am having fun deleting the plug-ins one by one and doing things manually with my child theme.

    I added this to my functions.php file:

    <?php
    function facebook_script_code(){
    ?>
    <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";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

    <?php }
    add_action( 'wp_footer', 'facebook_script_code' ); ?>

    I can view my site fine and javascript is inserted in the footer area (just like the plugin did), but when I try to log in to the dashboard I get:

    “Warning: Cannot modify header information – headers already sent by (output started at /home/myname/public_html/wp-content/themes/graphene-child/functions.php:8) in /home/myname/public_html/wp-includes/pluggable.php on line 881”

    I may have added that code wrong. I found a post in the forum where Syahir explained something similar to someone asking about how to insert phone tracking code for a hotel site.

    I’ve taken one little PHP class at college, but it was very basic. That’s why I’m enjoying the theme and playing around with it because it seems about the only legit way to learn.

    I’m going to remove the function and use the plug-in for now, but if you guys know what I need to change, I will add it back so I can get rid of the plug-in.

    Thanks again!

    Mod

    Kenneth John Odle

    #29521

    “headers already sent by” — you have a space before the opening <?php or after the closing ?> tag in your functions file. (This can also occur in your config file.) Edit the file to remove those spaces and reupload it.

    Quite frankly, I don’t think your functions file is where this bit belongs. You should try adding it in a PHP widget:

    1) Install the PHP widget

    2) Create a Graphene action hook widget area where you want the facebook button

    3) Drag a PHP widget to that widget area

    4) Paste this code into that PHP widget

    To be honest, this Facebook stuff is a major pain, and just when you get it working, they will probably upgrade it. (They do that from time to time.) That’s why I just use the code from AddThis.com and let them worry about keeping up with FB.

    Chris

    #29522

    Yes, this Facebook stuff is a HUGE pain I’ve discovered. I’m going to spend some time tonight looking at the AddThis feature of the theme. It looks like everything you need is already here in the theme. I’ve just got to learn how to use it all and I will be dangerous, lol.

    When you say “PHP widget,” is that a plug-in? I have exec-php plug-in installed, but I can grab the other if it’s better.

    Thanks for your help!

    Mod

    Kenneth John Odle

    #29523

    Yup, PHP Widget is a plugin. It works a lot like a text widget, except that you can also use PHP, in addition to the CSS and HTML that a text widget allows.

    All social media sites update their stuff from time to time; but Facebook makes it really difficult just to get started.

    It looks like everything you need is already here in the theme.

    That’s the idea. 🙂

    I’ve just got to learn how to use it all and I will be dangerous, lol.

    Have you seen these resources, bac?

    https://forum.graphene-theme.com/graphene-support/beginners-resources-possible-sticky

    The whole point of those is to make you less dangerous, actually. Enjoy.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Do NOT follow this link or you will be banned from the site!