<?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>Smaart Web</title>
	<atom:link href="http://smaartweb.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://smaartweb.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Mon, 02 Apr 2012 18:56:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Right way of adding javascript in WordPress Plugins and Themes</title>
		<link>http://smaartweb.com/right-way-of-adding-javascript-in-wordpress-plugins-and-themes/</link>
		<comments>http://smaartweb.com/right-way-of-adding-javascript-in-wordpress-plugins-and-themes/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 18:06:55 +0000</pubDate>
		<dc:creator>siteadmin</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://smaartweb.com/?p=26</guid>
		<description><![CDATA[<p>There are so many ways of adding javascript in wordpress plugins and themes. I would like to show you the right methods I use and recommend.</p> <p>You can include javascript in your wordpress backend as well on the frondend ( &#8230; <a href="http://smaartweb.com/right-way-of-adding-javascript-in-wordpress-plugins-and-themes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are so many ways of adding javascript in wordpress plugins and themes. I would like to show you the right methods I use and recommend.</p>
<p>You can include javascript in your wordpress backend as well on the frondend ( The pages which are visible to the normal visitors of the site ). If you are working with a plugin then the right way to do that is as follows.</p>
<p>If you are planning to include the javascript in the frontend pages then include this in your wordpress plugin&#8217;s constructor function if you are using a plugin.</p>
<pre class="brush:applescript">&lt;?php

/*
  Plugin Name: How to include scripts in wp plugins
  Plugin URI: http://www.smaartweb.com
  Description: Test plugin on how to include javascript in WordPress Plugins.
  Version: 1.0.0
  Author: anver
  Author Email : anvergdr@gmail.com
  Author URI: http://www.smaartweb.com

  ------------------------------------------------------------------------
  Copyright 2012 smaartweb

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

if ( !class_exists( 'clssnippet' ) )
{

    class clssnippet
    {

        /**
         * class default constructor
         */
        public function __construct()
        {
            add_action( 'wp_enqueue_scripts', array( $this, 'insert_scripts' ) );
        }

        /**
         * inserts all the necessary scripts on the fronend
         * @access public
         */
        public function insert_scripts()
        {
            if ( !is_admin() )
            {
                wp_enqueue_script( 'your-unique-script-id', plugins_url( 'js/your-script.js', __FILE__ ), array( 'jquery' ), '1.0', TRUE );
            }
        }

    }

}

if ( class_exists( 'clssnippet' ) )
{
    $obj_snippet = new clssnippet();
}</pre>
<p>I&#8217;ll explain the line</p>
<pre class="brush:js">wp_enqueue_script( 'your-unique-script-id', plugins_url( 'js/your-script.js', __FILE__ ), array( 'jquery' ), '1.0', TRUE );</pre>
<p>The first parameter to the function is your javascript id, it can anything but has to be unique if you have to use this script in your pages but if you want to use the same scripts in two or more plugins and you want to load the script only once from any one of the plugins then make sure you put the script id same on all the plugins so that only one copy of the script gets loaded which makes the loading time quick and makes sense too. We don&#8217;t want the same script to load more than once on the same page.</p>
<p>The second parameter is the script url, the path where the script is lying. This is a wordpress function which many wp plugin authors ignore to use. I highly recommend using this function because it finds the correct js file even if you rename the directory of the plugin later if you decide to, still the plugin works perfectly without changing any coding inside the plugin by using this handy function. __FILE__ detects the correct directory of the plugin.</p>
<p>The third parameter is there to include any supporting javascript files for your script to work, most of the designers now use js frameworks like jquery and prototype. So if your script is dependent on some js libraries you can include here in an array. If you want to include multiple frameworks just seperate them by commas inside the array and everything works fine.</p>
<p>The fourth parameter is your scripts version number, you can put your revision code here if you want to.</p>
<p>The last parameter decides if you want to include this script in the footer or not. The default is false which includes the script in the header, still that depend on the way the main framework is inserted on the page if you are using jquery or other libraries. If those libraries are included in the footer then your script will be printed on the footer as well as your script loads only after loading the main dependent script.</p>
<p>There are so many things left, I&#8217;ll continue and complete this topic when i get some time.</p>
<p>Thanks and Regards,</p>
<p>Anver.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://smaartweb.com/right-way-of-adding-javascript-in-wordpress-plugins-and-themes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

