• Is there any way to set JavaScript variables in an external .js file (ie, edit/generate an external text file) from a WordPress plugin?

    Or is the only solution to set them in a script tag in the head of every page? Which obviously seems a little ‘clunky’ ??

    Cheers,

    Mark

Viewing 8 replies - 1 through 8 (of 8 total)
  • yes. and no.

    Thread Starter markwheeler

    (@markwheeler)

    Would you care to share a little more than that? How DO you set variables in a js file?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Is there any way to set JavaScript variables in an external .js file (ie, edit/generate an external text file) from a WordPress plugin?

    Have the plugin insert a <script src="url-to-plugin?get_script=1"> tag into the page to make a script call to itself, then have it check for calls with a $_GET["get_script"] variable to make it generate the necessary javascript code.

    I am assuming you are interested in using internal WP variables?

    I’ve managed to do it the same way if I were simply using js within <script> tags

    function blah()
    {
    	var thing = prompt('blahblahblah:', '<?php if (empty($paged)) { echo "1";} else { echo $paged; } ?>'); ... and so on

    You just need to enclose php properly.

    Thread Starter markwheeler

    (@markwheeler)

    Cheers, just using PHP directly in the script file will probably be best. But how do I go about including the appropriate WordPress functions so I can use get_option() to read from the database?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    markwheeler: require_once('../../../wp-blog-header.php');

    Thread Starter markwheeler

    (@markwheeler)

    Thanks very much guys ??

    HOLY CRAP… I’ve been looking for wp-blog-headers for at least 3 hours. Thanks guys.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Setting JavaScript variables’ is closed to new replies.