• Hi, I’m developing a plugin for work and I’m basically learning how to develop plugins for the first time. I have a question about using javascript hardcoded in the main php file for the plugin and linking to the external javascript files.

    Basically I can use document.write() if I hard code it into the plugin but, if I try to use wp_enqueue_script the javascript works but, document.write doesn’t seem to work.

    So this works

    echo '<h1>Clean Post Search Settings</h1></p>
    <p>		<h2>Add/Edit Topics</h2>';?><br />
    		<script><br />
    		document.open();<br />
    		document.write("<h1>Main title</h1>");<br />
    		document.close();<br />
    		</script></p>
    <p>

    But, document.write doesn’t work here

    PHP

    </p>
    <p>function cps_zt_settings_page(){<br />
    	echo '<h1>Clean Post Search Settings</h1></p>
    <p>		<h2>Add/Edit Topics</h2>';<br />
    		wp_enqueue_script(cps_zt_js);<br />
    }<br />

    External Javascript file

    <br />
    document.open();<br />
    document.write("<h1>Hello World</h1>");<br />
    document.close();<br />
Viewing 1 replies (of 1 total)
  • Take a look at the examples on the wp_enqueue_scripts documentation page: https://developer.ww.wp.xz.cn/reference/functions/wp_enqueue_script/

    When using wp_enqueue_script, you are letting WordPress decide where to put your included script file, and in what order relative to other scripts that are included in the page.

    Before the script can be enqueued, WordPress needs to know where to find the file. Did you register the script somewhere earlier? (using wp_register_script)?

    If you call wp_enque_script in the middle of rendering the page, your script should be added at the bottom of the page. Do you see a script tag showing the full URL to your script file at the bottom of the page?

Viewing 1 replies (of 1 total)

The topic ‘Javascript Plugin Development (external JS files vs wp_enqueue_script)’ is closed to new replies.