Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm learning about shortcodes and javascript.

I've simplified my problem as much as possible. In my shortcode's php file I wp_enqueue script1_javascript.js and script2_jquery.js. The first script is javascript and the second is a jQuery script.

In script1_javascript.js, I have:

function hello() {
    alert('Hello');
}

In script2_jquery.js, I call the function:

hello();

Why don't I get "Hello" when I view my page? The shortcode is activated. I've got the shortcode on a page. I wp_enqueued script2 after script1. Will WordPress allow me to call a function from another script? Any suggestion on what's going on or how to fix it?

Thank you.

share|improve this question

closed as too localized by toscho Jul 16 '12 at 23:31

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

2 Answers

If your call to wp_enqueue_script is within your shortcode callback function, it's not going to work (but it will in 3.3). Instead, hook into wp_enqueue_scripts, which fires very late in the WP start up process (as the page is rendering), where you can check to see if (1) it's a single post and (2) if that post contains your short code. If it is, then you can enqueue the script.

<?php
add_action( 'wp_enqueue_scripts', 'wpse29672_enqueue_scripts' );
function wpse29672_enqueue_scripts()
{
    // Not a single post? get out of here
    if( ! is_single() ) return;

    // get our post
    global $post;
    if( empty( $post ) ) $post = get_queried_object();

    // See if the post content contains our shortcode
    if( isset( $post->post_content ) && false !== strpos( $post->post_content, '[your-shortcode' ) )
    {
        wp_enqueue_script( 'wpse29672-script', 'http://www.example.com/path/to/script.js', array( 'jquery' ) );
    }
}

Alternatively, you could just hook into wp_enqueue_scripts and enqueue your script. But that would mean it gets loaded on every page. Very sloppy. Try to only load scripts when they're needed.

share|improve this answer
Hi @Christopher Davis, Thank you very much for your comment. I'm using ajax, so I used launched it with: add_action('wp_ajax_the_ajax_hook', 'the_action_function'); add_action('wp_ajax_nopriv_the_ajax_hook', 'the_action_function'); How should I change your code above in this case? Thank you. – Laxmidi Sep 28 '11 at 16:39
You need to modify your question with your complete code. I'm not even really clear on what you're after here. – chrisguitarguy Sep 28 '11 at 16:54
Hi, @Christopher Davis, Sorry, I over-simplified my example. I'm following this byronyasgur.wordpress.com/2011/06/27/…. The main difference between my code and the example is that I added a jQuery script in addition to the javascript. In the javascript file, I set-up a google map. In the jQuery script, I've got datepickers and I use jQuery's post method to send the form data. My problem is that in the jQuery script, I want to call a function defined in the javascript file. How can I define hello() in one script and call it in the other? Thank you – Laxmidi Sep 28 '11 at 17:36
Okay, I'm making some progress. I can call hello() and it will fire. This is the real fucntion I need to call: function csf_viewport_bounds() { var bounds = map.getBounds(); var ne = bounds.getNorthEast(); var sw = bounds.getSouthWest(); var maxLat = ne.lat(); var maxLong = ne.lng(); var minLat = sw.lat(); var minLong = sw.lng(); var the_maps_bounds = [maxLat, maxLong, minLat, minLong]; return(maxLat); } – Laxmidi Sep 28 '11 at 18:09

maybe you can try wp_print_scripts instead of wp_enqueue_scripts.

share|improve this answer

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