<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WP Engineer &#187; JavaScript</title>
	<atom:link href="http://wpengineer.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpengineer.com</link>
	<description>WordPress News, Hacks, Tips, Tutorials, Plugins and Themes</description>
	<lastBuildDate>Mon, 21 May 2012 22:48:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress Options Passed To JavaScript #2</title>
		<link>http://wpengineer.com/2457/wordpress-options-passed-to-javascript-2/</link>
		<comments>http://wpengineer.com/2457/wordpress-options-passed-to-javascript-2/#comments</comments>
		<pubDate>Fri, 18 May 2012 10:08:39 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2457</guid>
		<description><![CDATA[In our first article of this mini series, I explained how to pass with JSON from PHP to JS. Alternativly you can do this with the WordPress function wp_localize_script(), but contains some pitfalls. Therefore this little tutorial, also you should think about it in advance about these two solutions. The functions serves to pass strings, [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://wpengineer.com/?p=2315" title="Article: WordPress Options Passed To JavaScript #1">our first article</a> of this mini series, I explained how to pass with JSON from PHP to JS. Alternativly you can do this with the WordPress function <code>wp_localize_script()</code>, but contains some pitfalls. Therefore this little tutorial, also you should think about it in advance about these two solutions.<br />
<span id="more-2457"></span><br />
The functions serves to pass strings, at first to localize; so for the translate theme via Javascript. Therefore you can easily pass value and query them in Javascript. The most important point is, that this function can decode HTML Entities. Also important, that the script is recognized in advance; include it via <code>wp_enqueue_script()</code>.</p>
<p>This example should explain it.</p>
<pre>
// on admin area enqueue the scripts
add_action( &#039;admin_enqueue_scripts&#039;, &#039;fb_admin_enqueue_scripts&#039; );
function fb_admin_enqueue_scripts( $where ) {
	// separation in dev and live
	$suffix = defined(&#039;SCRIPT_DEBUG&#039;) &amp;&amp; SCRIPT_DEBUG ? &#039;.dev&#039; : &#039;&#039;;

	// enqueue of script, identifikation on the string-ID
	wp_enqueue_script(
		&#039;example_script_id&#039;,
		plugins_url( &#039;/js/my-example-script&#039; . $suffix. &#039;.js&#039;, __FILE__ ),
		array( &#039;jquery&#039; ),
		&#039;&#039;,
		TRUE
	);

	// data via Array
	// array with data; also possible via get_option( &#039;my_option_string&#039; )
	$data = array(
		&#039;some_string&#039; =&gt; __( &#039;Some string to translate&#039; ),
		&#039;a_value&#039; =&gt; &#039;10&#039;
	);
	// localize the data, identifier via script-ID and create object for JS
	wp_localize_script( &#039;example_script_id&#039;, &#039;js_object_name&#039;, $data );
}
</pre>
<p>With this little example everything is done on the PHP-side; the content of the array are known now as object <code>js_object_name</code> and can be use via the name-string <code>js_object_name</code> in JS.</p>
<p>Now some hints, which should help, if it comes to problems or which link to <a href="http://wpengineer.com/?p=2315">solutions via JSON</a> directly.<br />
The function <code>wp_localize_script()</code> decodes HTML Entities, which is kind of important, but not always the perfect solution. The function uses <code>json_encode()</code>, which can let to problems in multi-dimensional arrays in connection with the decoding of entities. As soon as the array has more than one dimension, I recommend <a href="http://wpengineer.com/?p=2315">JSON without the WP Core solution</a> via <code>wp_localize_script()</code>.</p>
<p>With WordPress 3.4 it will get better and the function will be more useful - let's see. I currently use on multiple dimensions rather JSON directly. The solution from the core would be nice, I like the handling and it would be clearly and comprehensively utilizable.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2457/wordpress-options-passed-to-javascript-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress Options Passed To JavaScript #1</title>
		<link>http://wpengineer.com/2315/wordpress-options-passed-to-javascript-1/</link>
		<comments>http://wpengineer.com/2315/wordpress-options-passed-to-javascript-1/#comments</comments>
		<pubDate>Wed, 16 May 2012 09:08:02 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2315</guid>
		<description><![CDATA[In WordPress you are not always in the PHP world and so you have to pass settings and data from the database to scripts sometimes. In many Plugins you can find solutions in loading the wp-load.php and therefore access to all features of WordPress. Long ago Otto (Samuel Wood) already referred to this fact and [...]]]></description>
			<content:encoded><![CDATA[<p>In WordPress you are not always in the PHP world and so you have to pass settings and data from the database to scripts sometimes. In many Plugins you can find solutions in loading the wp-load.php and therefore access to all features of WordPress. Long ago <a href="http://ottopress.com/2010/dont-include-wp-load-please/">Otto (Samuel Wood)</a> already referred to this fact and this articles shows solutions. Questions still there and still there are Plugins that load the wp-load.php precisely because of such problems.</p>
<p>A similar problem arises when the source of the scripts is not just written in the footer area of WordPress, but outsourced to a file and via <code>wp_enqueue_script()</code> included. Only then WordPress can manage, compromises and optimizes these scripts for delivery. Therefore I would like to show two examples, how to pass data from PHP to JS.<br />
<span id="more-2315"></span><br />
The first snippet uses the pass of values ​​via JSON to get the values ​​from the database with the current resources in PHP and the script stores directly in the header of the page the values ​​as an object.</p>
<pre>
add_action( &#039;admin_enqueue_scripts&#039;, &#039;fb_print_scripts&#039; );

function fb_print_scripts() {
	global $current_screen;

	if ( isset( $current_screen -&gt; id ) &amp;&amp; ! in_array( $current_screen -&gt; id, array( &#039;post&#039;, &#039;page&#039; ) ) )
		return;

	if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
		$options = get_site_option( &#039;my_options_id&#039; );
	else
		$options = get_option( &#039;my_options_id&#039; );

	if ( ! $options )
		return;
	?&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		var my_json_object = &lt;?php echo json_encode( $options ); ?&gt;;
	&lt;/script&gt;
	&lt;?php
}
</pre>
<p>The above function gives the values ​​from the database as a JSON object in the head of the backend, as the page was requested in the first step of the function. Via <code>$current_screen</code> it will be checked so it will be only delivered if you are on one of the defined pages (post, page).</p>
<p>The next step is common practice and best solution to include scripts in WordPress. Thereby I will include the JS file, which then has accesses to the JSON object.</p>
<pre>
add_action( &#039;admin_enqueue_scripts&#039;, &#039;fb_admin_enqueue_scripts&#039; );

function fb_admin_enqueue_scripts( $where ) {

	if ( ! in_array( $where, array( &#039;post.php&#039;, &#039;post-new.php&#039;, ) )
		return;

	$suffix = defined(&#039;SCRIPT_DEBUG&#039;) &amp;&amp; SCRIPT_DEBUG ? &#039;.dev&#039; : &#039;&#039;;

	wp_enqueue_script(
		self :: get_textdomain() . &#039;_script&#039;,
		plugins_url( &#039;/js/my_script&#039; . $suffix. &#039;.js&#039;, __FILE__ ),
		array( &#039;jquery&#039;, &#039;my_other_script&#039; ),
		&#039;&#039;,
		TRUE
	);

}
</pre>
<p>The script accesses directly the object that is processed.</p>
<pre>
jQuery( document ).ready( function( $ ) {

	if ( typeof my_json_object == &#039;undefined&#039; )
		return;

// debug in console of Browser
console.dir( my_json_object ); 

});
</pre>
<p>Another solution will be presented in the following post at this series.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2315/wordpress-options-passed-to-javascript-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Comment Length Limiter</title>
		<link>http://wpengineer.com/2321/comment-length-limiter/</link>
		<comments>http://wpengineer.com/2321/comment-length-limiter/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 07:02:14 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2321</guid>
		<description><![CDATA[If you have used Twitter, then you know that you are only allowed to type 140 characters in a single Tweet. There is a nice little number below the text field indicating how much is left to write. It would be nice to have this feature for WordPress comments too. Or any text field, for [...]]]></description>
			<content:encoded><![CDATA[<p>If you have used Twitter, then you know that you are only allowed to type 140 characters in a single Tweet. There is a nice little number below the text field indicating how much is left to write.<br />
<span id="more-2321"></span><br />
<img src="http://wpengineer.com/wp-content/uploads/Screen-Shot-2011-12-02-at-11.27.23-AM.png" alt="" title="Screen Shot 2011-12-02 at 11.27.23 AM" width="508" height="153" class="aligncenter size-full wp-image-2322" /></p>
<p>It would be nice to have this feature for WordPress comments too. Or any text field, for that matter. It can be done with the following piece of JavaScript:</p>
<pre>
jQuery(function($) {
	// configure
	var comment_input = $( &#039;#commentform textarea&#039; );
	var submit_button = $( &#039;#commentform .form-submit&#039; );
	var comment_limit_chars = 1000;
	// stop editing here

	// display how many characters are left
	$( &#039;&lt;div class=&quot;comment_limit_info&quot;&gt;&lt;span&gt;&#039; + comment_limit_chars + &#039;&lt;/span&gt; characters left&lt;/div&gt;&#039; ).insertAfter( comment_input );

	comment_input.bind( &#039;keyup&#039;, function() {
		// calculate characters left
		var comment_length = $(this).val().length;
		var chars_left = comment_limit_chars - comment_length;

		// display characters left
		$( &#039;.comment_limit_info span&#039; ).html( chars_left );

		// hide submit button if too many chars were used
		if (submit_button)
			( chars_left &lt; 0 ) ? submit_button.hide() : submit_button.show();
	});
});
</pre>
<p><cite><a href="https://gist.github.com/1422754">https://gist.github.com/1422754</a></cite></p>
<p>The first three <code>var</code>s below the <code>// configure</code> comment can be edited.<br />
<code>comment_input</code> is the DOM element of the text field.<br />
<code>submit_button</code> is the DOM element for the button to submit the form.<br />
Finally, <code>comment_limit_chars</code> is the amount of characters allowed.</p>
<p>This snippet automatically inserts a <code>div</code> tag below the text field and updates the character count when the user types. The <code>submit_button</code> is optional. Set the <code>var</code> to <code>null</code> if you don't want it to be grayed out.</p>
<p>Please keep in mind that this only validates the input on the client side. If you have to rely on the maximum text length, like Twitter does, you need to check the length on the back end side with PHP too.</p>
<div class="incontent">
<h4>Guest Post</h4>
<p><img src="http://wpengineer.com/wp-content/uploads/eric-teubert.jpg" alt="" title="selbst" width="150" height="150" class="alignleft size-full wp-image-2163" />This post is written by Eric Teubert - <a href="http://www.satoripress.com/">www.satoripress.com</a> and is a post in our Advent Calendar on WP Engineer about WordPress.<br />
Thank you very much from my part to <a href="http://twitter.com/#!/ericteubert" title="see Eric also on Twitter">Eric</a>.<br />
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!
</div>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2321/comment-length-limiter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Advent Calendar – WordPress Editor: Preserve the scroll position</title>
		<link>http://wpengineer.com/2340/advent-calendar-%e2%80%93-wordpress-editor-preserve-the-scroll-position/</link>
		<comments>http://wpengineer.com/2340/advent-calendar-%e2%80%93-wordpress-editor-preserve-the-scroll-position/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 06:59:38 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2340</guid>
		<description><![CDATA[WordPress has a nice editor, with which are several hundreds of articles written daily. But in my opinion the editor has an usability issue. Every time you save a post the scroll position of the editor will be on top again. If you want to continue writing the post you have first to find he [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has a nice editor, with which are several hundreds of articles written daily.</p>
<p>But in my opinion the editor has an usability issue.<br />
Every time you save a post the scroll position of the editor will be on top again. If you want to continue writing the post you have first to find he old position again. This can be time-consuming.<br />
<span id="more-2340"></span><br />
<div id="attachment_2342" class="wp-caption aligncenter" style="width: 510px"><img src="http://wpengineer.com/wp-content/uploads/wp-editor-preserve-scroll-position.png" alt="" title="wp-editor-preserve-scroll-position" width="500" height="225" class="size-full wp-image-2342" /><p class="wp-caption-text">Save editor scroll position on saving</p></div></p>
<p>To avoid this behavior I have written a snippet which I want to share now with you.</p>
<pre>
&lt;?php
/**
 * The class will help you to recover the old scoll position in your Editor.
 * Either HTML or visuel editor.
 */
final class Preserve_Editor_Scroll_Position {
	/**
	 * Init
	 */
	public static function init() {
		add_filter( &#039;redirect_post_location&#039;, array( __CLASS__, &#039;add_query_arg&#039; ) );
		add_action( &#039;edit_form_advanced&#039;, array( __CLASS__, &#039;add_input_field&#039; ) );
		add_action( &#039;edit_page_form&#039;, array( __CLASS__, &#039;add_input_field&#039; ) );
		add_filter( &#039;tiny_mce_before_init&#039;, array( __CLASS__, &#039;extend_tiny_mce&#039; ) );
	}

	/**
	 * Adds a hidden input field for scrolltop value
	 */
	public static function add_input_field() {
		$position = ! empty( $_GET&#091;&#039;scrollto&#039;&#093; ) ? $_GET&#091;&#039;scrollto&#039;&#093; : 0;

		printf( &#039;&lt;input type=&quot;hidden&quot; id=&quot;scrollto&quot; name=&quot;scrollto&quot; value=&quot;%d&quot;/&gt;&#039;, esc_attr( $position ) );

		// Print Javascript data
		add_action( &#039;admin_print_footer_scripts&#039;, array( __CLASS__, &#039;print_js&#039; ), 55 ); // Print after Editor JS.
	}

	/**
	 * Extend TinyMCE config with a setup function
	 */
	public static function extend_tiny_mce( $init ) {
		if ( &#039;tinymce&#039; == wp_default_editor() )
			$init&#091;&#039;setup&#039;&#093; = &#039;rich_scroll&#039;;

		return $init;
	}

	/**
	 * Returns redirect url with query arg for scroll position
	 */
	public static function add_query_arg( $location ) {
		if ( ! empty( $_POST&#091;&#039;scrollto&#039;&#093; ) )
			$location = add_query_arg( &#039;scrollto&#039;, (int) $_POST&#091;&#039;scrollto&#039;&#093;, $location );

		return $location;
	}

	/**
	 * Prints Javascript data
	 */
	public static function print_js() {
		?&gt;
	&lt;script&gt;
	( function( $ ) {
		$( &#039;#post&#039; ).submit( function() {
			scrollto =
				$( &#039;#content&#039; ).is( &#039;:hidden&#039; ) ?
				$( &#039;#content_ifr&#039; ).contents().find( &#039;body&#039; ).scrollTop() :
				$( &#039;#content&#039; ).scrollTop();

			$( &#039;#scrollto&#039; ).val( scrollto );
		} );

		$( &#039;#content&#039; ).scrollTop( $( &#039;#scrollto&#039; ).val() );
	} )( jQuery );

	function rich_scroll( ed ) {
		ed.onInit.add( function() {
			jQuery( &#039;#content_ifr&#039; ).contents().find( &#039;body&#039; ).scrollTop( jQuery( &#039;#scrollto&#039; ).val() );
		} );
	};
	&lt;/script&gt;
		&lt;?php
	}
}
add_action( &#039;plugins_loaded&#039;, array( &#039;Preserve_Editor_Scroll_Position&#039;, &#039;init&#039; ) );
</pre>
<p>If you want you can create your own plugin with this snippet or you can download the plugin <a href="http://wordpress.org/extend/plugins/preserve-editor-scroll-position/">Preserve Editor Scroll Position</a>.</p>
<div class="incontent">
<h4>Guest Post</h4>
<p><img src="http://wpengineer.com/wp-content/uploads/dominik-schilling-g+.png" alt="Dominik Schilling Avatar" title="selbst" width="150" height="150" class="alignleft" />This post is written by Dominik Schilling - <a href="http://wpgrafie.de/">wpgrafie.de</a> and is a post in our Advent Calendar on WP Engineer about WordPress. Dominik is Student, Web Developer, WordPress Contributing Developer - <a href="http://profiles.wordpress.org/users/ocean90">ocean90</a> and he ♥ WordPress.<br />
Thank you very much from my part to <a href="https://plus.google.com/101675293278434581718/about" title="see his G+ rofile">Dominik</a>.<br />
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!
</div>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2340/advent-calendar-%e2%80%93-wordpress-editor-preserve-the-scroll-position/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Localization with JavaScript in WordPress</title>
		<link>http://wpengineer.com/2181/localization-with-javascript-in-wordpress/</link>
		<comments>http://wpengineer.com/2181/localization-with-javascript-in-wordpress/#comments</comments>
		<pubDate>Thu, 19 May 2011 10:06:27 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2181</guid>
		<description><![CDATA[Creating Plugins and Theme functions with multilanguage capability has been established knowadays.Especially for us as German developers it is a must have. But there are some difficulties if you are using JavaScript, the question is how to provide it multilingual or provide option values in different languages. WordPress offers some possibilities and I like to [...]]]></description>
			<content:encoded><![CDATA[<p>Creating Plugins and Theme functions with multilanguage capability has been established knowadays.Especially for us as German developers it is a must have. But there are some difficulties if you are using JavaScript, the question is how to provide it multilingual or provide option values in different languages. WordPress offers some possibilities and I like to show them, since <a href="http://wordpress.stackexchange.com/questions/351/modifying-a-js-file-with-data-from-plugin-settings/357#357">this question</a> was asked by many and developers are searching for solutions. The following little example should show the realization and the easy output of strings demonstrates it. Wether strings for multilanguage or providing options values, it doesn't matter.<br />
<span id="more-2181"></span><br />
First we create a function with an array that contains the required content and can be accessed on the relevant content using variables.</p>
<pre>
function get_language_strings() {
    $strings = array(
        &#039;example&#039; =&gt; __( &#039;My Example String&#039;, TEXTDOMAIN_CONSTANT ),
        &#039;foo&#039;   =&gt; __( &#039;My foo string&#039;, TEXTDOMAIN_CONSTANT ),
        &#039;bar&#039;   =&gt; __( &#039;My bar&#039;, TEXTDOMAIN_CONSTANT )
    )

    return $strings;
}
</pre>
<p>When you call the extension, Plugin or Theme, the function must be loaded. For this task WordPress provides the function <code><a href="http://codex.wordpress.org/Function_Reference/wp_localize_script">wp_localize_script()</a></code> that takes care of everything:</p>
<pre>
wp_localize_script(
                       &#039;my_foo&#039;,
                       &#039;my_var_prefix&#039;,
                       $this-&gt;get_language_strings() // inside class
);
</pre>
<p>You can access the variables in the scripts:</p>
<pre>
alert( my_var_prefix.example );
</pre>
<p>No integration of <code>wp-load.php</code> or something like this, simply access the content. The strings are now translated into all languages using the classical methods and it applies only to create the appropriate files.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2181/localization-with-javascript-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fix Empty Searches</title>
		<link>http://wpengineer.com/2162/fix-empty-searches/</link>
		<comments>http://wpengineer.com/2162/fix-empty-searches/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 10:14:12 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2162</guid>
		<description><![CDATA[One minor problem for any web site is duplicate content: the same resource is available under different URIs. Yes, we have &#60;link rel=canonical&#62;, but anything that prevents such cases is better. Today, let’s look at something, that most professionals never see: empty searches. You offer a search input field, and someone hits the submit button [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-2010-21.jpg" alt="" title="WordPress-Christmas-2010-21" width="400" height="267" class="aligncenter size-full wp-image-2164" /><br />
One minor problem for any web site is <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=66359">duplicate content</a>: the same resource is available under different URIs. Yes, we have <code>&lt;link rel=canonical&gt;</code>, but anything that prevents such cases is better.</p>
<p>Today, let’s look at something, that most professionals never see: empty searches. You offer a search input field, and someone hits the submit button unintentionally, without any term entered. The resulting URI looks like this: <code>example.com/?s=</code>. It shows the same content as your front page. In fact, it <em>is</em> the front page.</p>
<p>No one needs that.</p>
<p>Therefore, I have added a simple rule to my .htaccess:</p>
<pre># Catch empty searches
RewriteCond %{QUERY_STRING} ^s=$
RewriteRule ^ /? &#091;L,R=301&#093;</pre>
<p>Add these lines right after the <code>RewriteBase</code> directive.</p>
<p>Next, I wanted to save my visitors the unnecessary request. I have <a href="http://toscho.de/2010/jquery-plugin-leere-formulare-verhindern/">written a simple jQuery plugin</a>:</p>
<pre>/**
 * Stop empty searches
 *
 * @author Thomas Scholz http://toscho.de
 * @param  $ jQuery object
 * @return bool|object
 */
(function( $ ) {
   $.fn.preventEmptySubmit = function( options ) {
       var settings = {
           inputselector: &quot;#s&quot;,
           msg          : &quot;Don’t waste your time with an empty search!&quot;
       };

       if ( options ) {
           $.extend( settings, options );
       };

       this.submit( function() {
           var s = $( this ).find( settings.inputselector );
           if ( ! s.val() ) {
               alert( settings.msg );
               s.focus();
               return false;
           }
           return true;
       });
       return this;
   };
})( jQuery );</pre>
<p>I call it in my footer scripts:</p>
<pre>jQuery( &quot;#searchform&quot; ).preventEmptySubmit();</pre>
<p>Note that my search form element has an id attribute with the value <code>searchform</code>. Adjust the selector to match your current theme. To change the message, just add an option for it:</p>
<pre>jQuery( &quot;#searchform&quot; ).preventEmptySubmit({ msg: &quot;This won’t work!&quot; });</pre>
<p>You may use the script for other input elements too. Set the selector in the second option <code>inputselector</code>. Say, you have a form with the id <code>discountcode</code> and a field with the id <code>dcodenumber</code>:</p>
<pre>jQuery( &quot;#discountcode&quot; ).preventEmptySubmit(
{
   msg: &quot;No number, no discount, sorry!&quot;,
   inputselector: &quot;dcodenumber&quot;
});</pre>
<p>Useful? Any hints?</p>
<div class="incontent">
<h4>Guest Post</h4>
<p><img src="http://wpengineer.com/wp-content/uploads/selbst.png" alt="" title="selbst" width="150" height="150" class="alignleft size-full wp-image-2163" />This post is written by Thomas Scholz - <a href="http://toscho.de/">toscho.de</a> and is a post in our Advent Calendar on WP Engineer about WordPress.<br />
Thank you very much from my part to <a href="http://toscho.de/">Thomas</a>.<br />
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!
</div>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2162/fix-empty-searches/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Category And Archive Dropdown With Unobtrusive JavaScript</title>
		<link>http://wpengineer.com/2131/category-and-archive-dropdown-with-unobstrusive-javascript/</link>
		<comments>http://wpengineer.com/2131/category-and-archive-dropdown-with-unobstrusive-javascript/#comments</comments>
		<pubDate>Sat, 18 Dec 2010 13:54:18 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2131</guid>
		<description><![CDATA[A good website works when a user comes along with JavaScript disabled, just as well as with scripting enabled. You separate the JS layer of the site from anything else (and waived for example, onclick in HTML) and places the scripts in a way that only improves the already existing functionality of the site. So [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-2010-18.jpg" alt="" title="WordPress-Christmas-2010-18" width="400" height="267" class="aligncenter size-full wp-image-2145" />A good website works when a user comes along with JavaScript disabled, just as well as with scripting enabled. You separate the JS layer of the site from anything else (and waived for example, onclick in HTML) and places the scripts in a way that only improves the already existing functionality of the site. So the visitors can navigate through the website with and without JS, even though it might be a little more difficult or less attractive without scripting. This approach to scripting on sites is called Unobtrusive JavaScript.  <a href="http://jsconf.blip.tv/file/4002810/">Jenn Lukas gives in this talk at the JSConf 2010</a> reasons why it's a good thing. Besides all the good reasons, the main argument for unobtrusive JavaScript that is to be implemented with appropriate planning so easily that it would be a wasted potential if you would without it. The problem can be alone if the used CMS makes problem - which brings us to WordPress.</p>
<p>Occasionally you like to have in your blog sidebar drop down menus (select-elements) to navigate the categories and the archive and the WordPress Codex provides the documentation and appropriate solutions for <a href="http://codex.wordpress.org/Function_Reference/get_categories#Dropdown_Box'>Categories</a> and <a href="http://codex.wordpress.org/Function_Reference/wp_get_archives#Dropdown_Box">archive</a> in which, however, both with only activated JavaScript function. In both cases, the URL of the destination page is stored in the value-value of the built-in option elements of the drop-down menu and when you select an entry, you will be forwarded via JavaScript on the very URL. The almost same functionality could be achieved also without scripts:</p>
<ol>
<li>Create an old-fashioned GET form with submit button that sends to the base address of the site</li>
<li>Put the <code>select</code> in this very form and specify name="cat"</li>
<li>Give the option elements the slug of the categories</li>
</ol>
<p>Submitting the form will result in a call to blog.de/?cat=category and thus the desired category, possibly with a redirect to the search engine friendly version of the URL. This works for the visitors just as good as the solution of the Codex, leaving aside the fact that you have to click on a button. But this is where you can engage with JavaScript to hide the button and sending the form via, by a script inserted, onclick handle . The end result looks like the script for users with Codex solution and all others come then just by clicking on the button to its destination and everyone is happy. So let's get to work!</p>
<p>The basis for the category drop forms creates a normal HTML form:</p>
<pre>
&lt;form id=&quot;kategorienform&quot; action=&quot;&lt;?php bloginfo(&#039;url&#039;); ?&gt;&quot; method=&quot;get&quot;&gt;
    &lt;label for=&quot;kategorienselect&quot;&gt;Jump to Category&lt;/label&gt;
    &lt;select id=&quot;kategorienselect&quot; name=&quot;cat&quot;&gt;
        &lt;option value=&quot;&quot;&gt;-- Please Select&lt;/option&gt;
        &lt;?php
            $categories = get_categories(&#039;hierarchical=0&#039;);
            foreach($categories as $category){
                $selected = (is_category($category-&gt;cat_ID)) ? &#039;selected&#039; : &#039;&#039;;
                echo &#039;&lt;option &#039;.$selected.&#039; value=&quot;&#039;.$category-&gt;cat_ID.&#039;&quot;&gt;&#039;.$category-&gt;cat_name.&#039; (&#039;.$category-&gt;count.&#039;)&lt;/option&gt;&#039;;
            }
        ?&gt;
    &lt;/select&gt;
    &lt;input id=&quot;kategorienbutton&quot; value=&quot;Kategorie abrufen&quot; type=&quot;submit&quot;&gt;
&lt;/form&gt;
</pre>
<p>You could use this in your side bar and it would work, but we still want to get rid of the submit button and start forwarding directly when selecting a category. For this purpose, it does not take more than 6 lines of JavaScript that can be inserted directly below the form:</p>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
    document.getElementById(&#039;kategorienselect&#039;).onchange = function(){
        if(this.value){
            document.getElementById(&#039;kategorienform&#039;).submit();
        }
    };
    document.getElementById(&#039;kategorienbutton&#039;).style.display = &#039;none&#039;;
&lt;/script&gt;
</pre>
<p>If the user changes his selection in select element, is (if not the entry "Please select" is selected) submitting the form automatically, without having to activate the with style.display = 'none' made invisible button - done is the most user-friendly and still working without JavaScript category dropdown! This is a bit more code than the three lines from the Codex, but it works guaranteed on every visitor. And the principle is quite simple, provided you plan accordingly in advance. Since this is not happening in WordPress not even happen, we need to tap a little more - or in the case of the archive dropdown even much more:</p>
<pre>
&lt;form id=&quot;archivform&quot; action=&quot;&lt;?php bloginfo(&#039;url&#039;); ?&gt;&quot; method=&quot;get&quot;&gt;
    &lt;label for=&quot;archivselect&quot;&gt;skip to month&lt;/label&gt;
    &lt;select id=&quot;archivselect&quot; name=&quot;m&quot;&gt;
        &lt;option value=&quot;&quot;&gt;-- Please select&lt;/option&gt;
        &lt;?php
            $query = &quot;SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, COUNT(ID) as `posts`
                FROM $wpdb-&gt;posts
                WHERE post_type = &#039;post&#039; AND post_status = &#039;publish&#039;
                GROUP BY YEAR(post_date), MONTH(post_date)
                ORDER BY post_date DESC&quot;;
            $key = md5($query);
            $cache = wp_cache_get(&#039;select_archives&#039;, &#039;general&#039;);
            if(!isset($cache&#091;$key&#093;)){
                $arcresults = $wpdb-&gt;get_results($query);
                $cache&#091;$key&#093; = $arcresults;
                wp_cache_set(&#039;select_archives&#039;, $cache, &#039;general&#039;);
            }
            else{
                $arcresults = $cache&#091;$key&#093;;
            }
            if($arcresults){
                global $wp_locale;
                foreach((array) $arcresults as $arcresult){
                    $value = $arcresult-&gt;year.$arcresult-&gt;month;
                    $text = sprintf(__(&#039;%1$s %2$d&#039;), $wp_locale-&gt;get_month($arcresult-&gt;month), $arcresult-&gt;year);
                    $count = &#039;&nbsp;(&#039;.$arcresult-&gt;posts.&#039;)&#039;;
                    $selected = (is_month() &amp;&amp; get_query_var(&#039;year&#039;).get_query_var(&#039;monthnum&#039;) == $value) ? &#039;selected&#039; : &#039;&#039;;
                    echo &#039;&lt;option &#039;.$selected.&#039; value=&quot;&#039;.$value.&#039;&quot;&gt;&#039;.$text.$count.&#039;&lt;/option&gt;&#039;;
                }
            }
        ?&gt;
    &lt;/select&gt;
    &lt;input id=&quot;archivbutton&quot; value=&quot;Archiv abrufen&quot; type=&quot;submit&quot;&gt;
&lt;/form&gt;
</pre>
<p>The problem with the Archive, is that without a custom SQL query, you cannot get to the information, which you need for a monthly archive. So there is no other way then to manually enter the database and cache the result manually. The principle is identical: simply output a conventional form and then six small lines of JavaScript:</p>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
    document.getElementById(&#039;archivselect&#039;).onchange = function(){
        if(this.value){
            document.getElementById(&#039;archivform&#039;).submit();
        }
    };
    document.getElementById(&#039;archivbutton&#039;).style.display = &#039;none&#039;;
&lt;/script&gt;
</pre>
<p>To summarize: many lines of code, but the principle is not complicated. Unobtrusive JavaScript is a question of intelligent planning and if it is not included as with WordPress automatically, you have to do it. With traditional websites, there is no reason to operate completely dependent on JavaScript, and should there be more effort, it is worth the clean separation of the layers and the accessibility bonus.</p>
<h4>Guest Post</h4>
<p><img src="http://wpengineer.com/wp-content/uploads/twitter_bigger.png" alt="" title="twitter_bigger" width="73" height="73" class="alignleft size-full wp-image-2132" />This post was written by Peter Kröner <a href="http://www.peterkroener.de/">peterkroener.de</a>.</p>
<p>Peter Kröner is freelance web designer and developer and also author of the <a href="http://html5-buch.de/">HTML5-book</a>.<br />
On <a href="http://www.peterkroener.de/weblog/">peterkroener.de</a> he blogs on all kind of topics in web technology.</p>
<p>Thank you very much from our part to Peter.</p>
<p>If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2131/category-and-archive-dropdown-with-unobstrusive-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WordPress 	Implements Jcrop</title>
		<link>http://wpengineer.com/1020/wordpress-implements-jcrop/</link>
		<comments>http://wpengineer.com/1020/wordpress-implements-jcrop/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 11:00:05 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress News]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Jcrop]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp2.8]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1020</guid>
		<description><![CDATA[With the upcoming version 2.8 of WordPress, the extension to the JavaScript library jQuery Jcrop is implemented. Jcrop allows directly into a web application to crop images. A first peak shows the site about Jcrop and I'm curious how it will be included in WordPress, currently is no live demo available. &#169; WP Engineer Team, [...]]]></description>
			<content:encoded><![CDATA[<p>With the upcoming <a href="http://wpengineer.com/wordpress-28-small-insights/">version 2.8 of WordPress</a>, the extension to the JavaScript library <a href="http://jquery.com/">jQuery</a> <a href="http://deepliquid.com/content/Jcrop.html">Jcrop</a> is implemented. Jcrop allows directly into a web application to crop images.</p>
<p><img src="http://wpengineer.com/wp-content/uploads/jcrop.png" alt="jcrop" title="jcrop" width="453" height="89" class="aligncenter size-full wp-image-1021" /></p>
<p>A first peak shows the site about <a href="http://deepliquid.com/content/Jcrop.html">Jcrop</a> and I'm curious how it will be included in WordPress, currently is no live demo available.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1020/wordpress-implements-jcrop/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Use JavaScript Libraries In And Of WordPress</title>
		<link>http://wpengineer.com/415/use-javascript-libraries-in-and-of-wordpress/</link>
		<comments>http://wpengineer.com/415/use-javascript-libraries-in-and-of-wordpress/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 16:16:08 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Lightbox]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Thickbox]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=415</guid>
		<description><![CDATA[If you develop in and for WordPress, it's advisable to use a library of the core and a Plugin for the the library with the desired effect. Same for theme and Plugin authors. If you use a function of WordPress to implement JavaScript, you will have less complications and the compatibility between Plugins is much [...]]]></description>
			<content:encoded><![CDATA[<p>If you develop in and for WordPress, it's advisable to use a library of the core and a Plugin for the the library with the desired effect.</p>
<p>Same for theme and Plugin authors. If you use a function of WordPress to implement JavaScript, you will have less complications and the compatibility between Plugins is much higher.<br />
I will show you an example with Thickbox how to use JavaScript from the core.</p>
<p>There are many possibilities to implement the „Lightbox“ effect on your website. Let's take a close look on a way to use it without a Plugin.<br />
WordPress has <a href="http://jquery.com/demo/thickbox/">Thickbox</a> in their standard installation, so it's not very complicated to use it.<br />
<span id="more-415"></span></p>
<h3><code>wp_enqueue_script()</code></h3>
<p>Since version 2.1 of WordPress, there is the function <code>wp_enqueue_script()</code> included, which makes the implementation of JavaScript libraries very easy and also checks it. There is no need to use additional JavaScripts, the integration is easy and clean.</p>
<h3><code>wp_enqueue_style()</code></h3>
<p>Additionally pay attention to <code>wp_enqueue_style()</code> . With this function you can integrate CSS files.<br />
Both functions understand many parameters. For the example we need Thickbox, which exists as Plugin for jQuery. That's why you have to load both files, right?</p>
<pre lang="php">
wp_enqueue_script( &#039;jquery&#039; );
wp_enqueue_script( &#039;thickbox&#039; );
</pre>
<p>WordPress manages the dependencies , so it would be enough if you present the extension and WordPress will detect that jQuery is necessary. It's enough to use <code>wp_enqueue_script( 'thickbox' );</code>. Therefor jQuery will be load and be placed into the right spot of the code. The same happens if other calls are needing jQuery. WordPress pays attention and only allows one call of the library or extension to prevent that they get loaded several times.</p>
<p>So we integrate in our example the necessary JavaScript files. Additionally we need a style to get the effect of the Thickbox.<br />
It's enough to include the standard design of WordPress by calling the key <em>Thickbox</em> for the CSS call <code>wp_enqueue_style( 'thickbox' )</code>.</p>
<p>You can adjust the design for your own preferences.<br />
Here a small example to add the close icon into your core.</p>
<pre lang="css">
#TB_window a:link {
	color: transparent !important;
	background: url(&#039;http:/examble.com/wp-includes/js/thickbox/tb-close.png&#039;) no-repeat;
	padding: 0 4px !important;
}

#TB_window a:hover {
	background: red;
}
</pre>
<p>If you don't want to look after it, which is only possible in your WordPress backend (otherwise the icons are not available, since they are defined in JS), you can just use the call <code>add_thickbox()</code>. With this everything will be included in Thickbox.</p>
<p>To connect links with this effect, you have to add to the link the class <em>thickbox</em> .<br />
In the most simple case it would look like this:</p>
<pre lang="php">
&lt;a href=&quot;http://examble.com/image.png&quot; class=&quot;thickbox&quot;&gt;
	&lt;img src=&quot;http://examble.com/image-thumb.png&quot; alt=&quot;Image&quot; /&gt;
&lt;/a&gt;
</pre>
<h3>Set class automatically</h3>
<p>Alternatively you can set the class automatically. The following function in <code>functions.php</code> of the theme does that, but it's not guaranteed that it works in all cases.</p>
<pre lang="php">
/*
 * Thickbox scan, add class for a
 */
function fb_add_thickbox($content){

	$content = preg_replace(&#039;/&lt;a(.*?)href=&quot;(.*?).(jpg|jpeg|png|gif|bmp|ico)&quot;(.*?)&gt;&lt;img/U&#039;, &#039;&lt;a$1href=&quot;$2.$3&quot; $4 class=&quot;thickbox&quot;&gt;&lt;img&#039;, $content);

	return $content;
}
add_filter(&#039;the_content&#039;, &#039;fb_add_thickbox&#039;, 2);
</pre>
<h3>Other JavaScripts</h3>
<p>WordPress offers the developers a range of other libraries and extensions to use them. The implementation of the displayed example above shows that there are no or just few difficulties and the different libraries don't disturb each other. Also it's not necessary to load some libraries several times, which happens because of some Plugins.</p>
<p>You can find quite a lot of keys in <code>/wp-includes/script-loader.php</code>. It's worth it to check them out. Here is just a selection of keys:</p>
<ul>
<li><code>prototype</code></li>
<li><code>scriptaculous-root</code></li>
<li><code>scriptaculous-builder</code></li>
<li><code>scriptaculous-dragdrop</code></li>
<li><code>scriptaculous-effects</code></li>
<li><code>scriptaculous-slider</code></li>
<li><code>scriptaculous-sound</code></li>
<li><code>scriptaculous-controls</code></li>
<li><code>jquery</code></li>
<li><code>jquery-form</code></li>
<li><code>jquery-color</code></li>
<li><code>interface</code></li>
<li><code>suggest</code></li>
<li><code>schedule</code></li>
<li><code>jquery-hotkeys</code></li>
<li><code>thickbox</code></li>
<li><code>swfupload</code></li>
<li><code>swfupload-degrade</code></li>
<li><code>swfupload-swfobject</code></li>
<li><code>jquery-ui-core</code></li>
<li><code>jquery-ui-tabs</code></li>
<li><code>jquery-ui-sortable</code></li>
<li><code>jquery-ui-draggable</code></li>
<li><code>jquery-ui-resizable</code></li>
<li><code>jquery-ui-dialog</code></li>
<li><code>wp-gears</code></li>
<li><code>farbtastic</code></li>
</ul>
<p>If you deliver a key, you can also deliver it in an array, which extension is necessary.</p>
<pre lang="php">
wp_enqueue_script( &#039;scriptaculous&#039;, &#039;&#039;, array(&#039;scriptaculous-dragdrop&#039;, &#039;scriptaculous-slider&#039;, &#039;scriptaculous-controls&#039;), &#039;1.8.0&#039;)
</pre>
<h3>Own scripts</h3>
<p>Alternatively you can include your own scripts with the help of the function. Here an example:</p>
<pre lang="php">
wp_enqueue_script( &#039;my_script&#039;,  plugins_url($path = &#039;my_plugin_folder/js/my_script.js&#039;), array(&#039;jquery&#039;) );
</pre>
<p>It is not necessary to use just js-files, you can also work with PHP, if you like to use some variables of PHP</p>
<pre lang="php">
wp_enqueue_script( &#039;my_script&#039;,  plugins_url($path = &#039;my_plugin_folder/js/my_script.php&#039;), array(&#039;jquery&#039;) );
</pre>
<p>Do you assume a specific version of the library, you can also deliver the version.</p>
<pre lang="php">
wp_enqueue_script( &#039;my_script&#039;,  plugins_url($path = &#039;my_plugin_folder/js/my_script.php&#039;), array(&#039;jquery&#039;), &#039;1.2.1.3&#039; );
</pre>
<p>This post should show you how to avoid complications with other libraries by using Plugins. The usage of this function is not only for Plugins, you can use it in your WordPress theme too.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/415/use-javascript-libraries-in-and-of-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

