Using jQuery in WordPress Posts

This post by Chris Coyier shows how to use jQuery inside a WordPress post. This helped me work around an Apache web server security configuration issue that was not allowing me to post SQL code snippets inside of my posts.

403 response when I try to post certain SQL code snippets:


You don’t have permission to access /wp-admin/post.php on this server.

First, follow the instructions in Chris’s post which basically has you modify the your themes header.php to load jQuery that is included with WordPress just before the wp_head() function call as follows.

<?php wp_head(); ?>;
<?php wp_enqueue_script("jquery");
wp_head(); ?>;

Next, create a htm file to hold SQL code snippets. The pre element id attribute allows for jQuery loading of individual fragments instead of the entire .htm file. Additional SQL code snippets can be added to this file each separated by a pre element with a unique id attribute:

		<pre id="sp_Procedure1">
		<!-- insert SQL Code here -->
		<pre id="sp_Procedure2">
		<!-- insert SQL Code here -->

Add a placeholder div to your post for the jQuery load target:

<div id="sql"></div>

Now add this JavaScript to the bottom of your post to load the .htm fragment*.

<script type="text/javascript">
var $j = jQuery.noConflict();
	$j('#sql').load(' #sp_Procedure2');

*Change the path as needed. More info: jQuery .load().

Published by

Jim Frenette

Web Developer - views here are my own except those taken from people more clever than me.