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 have a plugin that, among other things, has a javascript file that requires a few user specific settings. What would be the best way to get those settings into javascript from the plugin's settings parameters?

In other words, if I make those settings part of the settings page where the user can enter them, how would I best be able to get those values into javascript? Would I have to use something to append some script tags and set them via PHP on every page load? Would setting a cookie be a better way to do this?

share|improve this question
2  
This is a really great question and one that I've been planning to ask. Thanks for asking it! –  MikeSchinkel Aug 14 '10 at 21:46

2 Answers 2

up vote 8 down vote accepted

better is, you use the functions of WP for this, a example for multilanguage:

    add_action( 'admin_enqueue_scripts', 'add_scripts' );
    function add_scripts($where) {
        wp_localize_script( 'post2media', 'post2media_strings', $this->localize_vars() );
    }
    function localize_vars() {

        $strings = array(
                'btntext'    => __( 'Link with post', INPSYDE_P2M_TEXTDOMAIN ),
                'txtallnone' => __( 'Include in gallery:', INPSYDE_P2M_TEXTDOMAIN ),
                'txtall'     => __( 'All', INPSYDE_P2M_TEXTDOMAIN ),
                'txtnone'    => __( 'None', INPSYDE_P2M_TEXTDOMAIN ),
                'ttlcb'      => __( 'Include image in this gallery', INPSYDE_P2M_TEXTDOMAIN )
            );

        return $strings;
    }

use this in js-file:

jQuery(function ($) {
buttonaddfunc = function() {
    btntext = post2media_strings.btntext;

    reg = /\d+/;
    $( '.savesend > .button' ) . each( function() {
        inputname = $( this ) . attr( 'name' );
        number = reg . exec( inputname );
        $( this ) . after( '<input type="submit" value="' + btntext + '" name="link[' + number + ']" class="button">' );
    } );
    $( '.describe-toggle-on' ).unbind( 'click', buttonaddfunc );
};
$( '.describe-toggle-on' ).bind( 'click', buttonaddfunc );

} );

Also see the post from Otto

share|improve this answer
    
I didn't really understand your code too well but I found the link very useful. –  Ryan Elkins Aug 14 '10 at 20:26
1  
This is really excellent, thank you! I have been wondering how to do this for a really long time; thank you! I'd been wanting to ask it on the wp-hackers list but never did. Thank you again. –  MikeSchinkel Aug 14 '10 at 21:52
1  
You can see on my last plugin post2media (wordpress.org/extend/plugins/post2media) for see an example on live plugin; the plugin has not so much source and i think this is great for read source and understand the solution. –  bueltge Aug 15 '10 at 10:01
1  
Here's a simpler example that gets it down to the basics (you have a lot going on in your example): prelovac.com/vladimir/… –  Viper007Bond Aug 17 '10 at 2:55

There are a couple of ways that you could do this, one of which I've done before, the other I haven't, but I have used for XML configuration files.

The first one is to include the variables in a script tag inside the WP header or footer, before the script tag where you include your JS file, for example:

<script type="text/javascript">
    var test = "<?php echo "hello world"; /* the relevant PHP code to echo the data you require */ ?>";
    var slider_type = "<?php echo "nivo"; /* same again */ ?>";
</script>
<script type="text/javascript" src="<?php bloginfo("template_url"); ?>/js/your_js_here"></script>

The other alternative would be to include the JS inside of a PHP file which is included inside of a script tag.

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts.php"></script>

Inside this file you would include your javascript, and as PHP would parse it you would be able to include PHP calls in a similar fashion to above, simply echoing out the data/options which you require. One thing to note is that you might need to set the headers for the output as text/javascript.

Personally I much prefer the first method, and is what I use when I have user changeable settings which affect javascript files.

share|improve this answer
    
Nice writeup. This is exactly what I've been doing. OTOH, @bueltge has the answer we've probably all been looking for; I know I have. –  MikeSchinkel Aug 14 '10 at 21:47

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.