Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Sep 27 '11 at 13:25

This question came from our site for professional and enthusiast programmers.

1  
When are you actually calling socialNetworks()? Defining it in functions.php is not enough –  Pekka 웃 Sep 27 '11 at 8:42
    
Well I'm calling it in my post section –  kidwon Sep 27 '11 at 13:23
add comment

3 Answers 3

up vote 1 down vote accepted

You need to be hooking the function that calls the enqueue, personally I actually like to register scripts and enqueue them separately, rather than just enqueueing them, as it allows the flexibilty to enqueue conditionally and can save keystrokes down the line at little to no performance penalty. The code you are going to need should look something like this:

add_action( 'wp_enqueue_scripts', 'my_script_holder' );

function my_script_holder() {
    wp_register_script( 'svejo_script', 'http://svejo.net/javascripts/svejo-button.js', array() ); //put any dependencies (including jQuery) into the array
    wp_enqueue_script( 'svejo_script' );
}

I have not tested that code specifically, but I have used it in almost every site I've done. The only caveat I'd give you is that (and this is a bit outside of the scope of your question, but I think it's good to know) if you're planning on conditionally enququeing, you may want to hook init to register the scripts to make sure that an enqueue is not called for an unregistered script, as that can cause errors.

share|improve this answer
add comment

IIRC enqueue_script is triggered before init ( or very close to that ), while the short tags is triggered a little bit later.

share|improve this answer
add comment

Will it slow up the page? It only needs to be loaded once per page, and once it's loaded it'll be held by the browser cache meaning (hopefully) no delay loading it on future pages.

It may be worth just removing the entire wp_enqueue_script bit and adding:

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

Or, if you want to add it to the <head>, you could use something like:

<?php
    function my_scripts_method() {
        wp_enqueue_script( 'svejo_script', 'http://svejo.net/javascripts/svejo-button.js', array(), false, true);          
    }    

    add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

NB: This code is untested - but you should be able to find everything you need here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.