0

I'm trying to add this social-network button:

<a href="http://svejo.net/submit/?url=[your url]"
     data-url="[your url]"
     data-type="compact"
     id="svejo-button">Add in Svejo</a>
<script type="text/javascript" src="http://svejo.net/javascripts/svejo-button.js"></script>

to my blog posts. But loading this in every post would really slow my page so I've try to implement it with wp_enqueue_script() inside my functions.php

function socialNetworks(){
    $opentag = '<ul class="socials">';
    $closetag = '</ul>';

    $socials['svejonet'] =
        '<a href="http://svejo.net/submit/?url=['.get_permalink().']" data-url="['.get_permalink().']" data-type="compact" id="svejo-button">Add in Svejo</a>'.
        wp_enqueue_script( 'none', 'http://svejo.net/javascripts/svejo-button.js', array(), false, true);

    echo $opentag;
    foreach ( $socials as $key=>$val ){
       echo '<li class="'.$key.'">'.$val.'</li>';
    }
    echo $closetag;   
}

However the script does not load.
Appreciate any help BR, Stephan

1

The social network button you posted comes in two distinct parts.

First, the button itself is defined by the anchor tag.

<a href="http://svejo.net/submit/?url=[your url]"
     data-url="[your url]"
     data-type="compact"
     id="svejo-button">Add in Svejo</a>

Then, presumably the script replaced that button with an iframe or something similar so you don't have to send your users elsewhere to get it work work.

<script type="text/javascript" src="http://svejo.net/javascripts/svejo-button.js"></script>

The first part, the anchor tag, should go in your single.php file, and probably look something like this:

<a href="http://svejo.net/submit/?url=<?php the_permalink(); ?>"
     data-url="<?php the_permalink(); ?>"
     data-type="compact"
     id="svejo-button">Add in Svejo</a>

That's not going to slow down your load time. Next you can use wp_enqueue_script in your functions.php file. Here we'll hook into wp_enqueue_scripts and enqueue our script inside our hooked function.

<?php
add_action( 'wp_enqueue_scripts', 'wpse29581_print_scripts' );
function wpse29581_print_scripts()
{
    wp_enqueue_script( 'svejo-wpse29581', 'http://svejo.net/javascripts/svejo-button.js', array(), NULL, true );
}

The last argument of wp_enqueue_script tells WordPress to load the script in the footer. This will help keep the script from holding up the page load.

We can make this function a bit better, however, by checking to see if we're on a single post page and only enqueue the script if we are. Only load scripts if they're needed.

<?php
add_action( 'wp_print_scripts', 'wpse29581_print_scripts' );
function wpse29581_print_scripts()
{
    // if this isn't a single post page, bail
    if( ! is_single() ) return;
    wp_enqueue_script( 'svejo-wpse29581', 'http://svejo.net/javascripts/svejo-button.js', array(), NULL, true );
}

That would be the proper way to use wp_enqueue_script in this case. I think you're a bit confused as to how it works.

  • Thanks for your answer but I don't have single.php at all so I guess I have to fix that first and come back to your solution. 10x again appreciate your quick response. – kidwon Sep 27 '11 at 2:58

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.