Modifying a JS file with data from plugin settings

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

Leave a Comment