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.

So I should probably mention up front that I'm developing a plugin so my url is 'mysite.com/plugin' (Just in case this is related to my issue.

So, In my main plugin file (myplugin.php) I have added the following:

$plugin_url = plugins_url('/my-plugin-directory/');

immediately followed by

wp_enqueue_style('wp_enqueue_scripts', $plugin_url . 'css/boxes.css');

Great, so far so good. The plugin url is properly concatenated and the script is added to the page and appears in the source as

<link rel='stylesheet' id='wp_enqueue_scripts-css'  href='http://mysite.com/plugin/wp-content/plugins/my-plugin-directory/css/mystyle.css?ver=3.5.1' type='text/css' media='all' />

at this point I can even echo $plugin_url; which returns:

http://mysite.com/plugin/wp-content/plugins/my-plugin-directory/ 

Directly below that I have written the following function:

add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
    function my_jquery_enqueue()    {
        wp_enqueue_script('jquery');
        wp_register_script('jquery_masonry', $plugin_url.'js/jquery.masonry.min.js', array('jquery'), '2.0.110526' );
        wp_enqueue_script('jquery_masonry');
    }

Should work right? It doesn't. Instead, in the source it looks like this:

<script type='text/javascript' src='http://georgespake.com/pluginjs/jquery.masonry.min.js?ver=2.0.110526'></script>

Boooo...

So for the time being, I just wrote it out:

/****Add jquery stuff to header****/
add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
    function my_jquery_enqueue()    {
        wp_enqueue_script('jquery');
        wp_register_script('jquery_masonry', $plugin_url . '/wp-content/plugins/my-plugin-directory/js/jquery.masonry.min.js', array('jquery'), '2.0.110526' );
        wp_enqueue_script('jquery_masonry');
    }

This feels sloppy though because this is the only place I have to do this. Everywhere else, the $plugin_url seems to return the proper string. What am I missing here?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This is a PHP issue, not specific to WordPress. I suggest reading the PHP documentation on variable scope to understand why this doesn't work.

There is no $plugin_url variable defined within the scope of your my_jquery_enqueue function, you would have to call global $plugin_url; first within the function to access it, though I don't suggest using something as generic as $plugin_url.

Personally I would create a class for the plugin to encapsulate everything and not pollute the global scope.

share|improve this answer
    
Oh, duh! It's in a function; I guess I was up too late last night. That fixed it. As for creating a class, that's at the top of my to do list. Thanks! –  ItsGeorge Mar 5 '13 at 16:24

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.