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.

2
  • 1
    When are you actually callingsocialNetworks()? Defining it in functions.php is not enough
    – Pekka
    CommentedSep 27, 2011 at 8:42
  • Well I'm calling it in my post section
    – kidwon
    CommentedSep 27, 2011 at 13:23

3 Answers 3

2

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.

    2

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

      1

      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

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.