<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tech Hunt</title>
	<atom:link href="http://rsjeyakumar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rsjeyakumar.wordpress.com</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 12:30:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rsjeyakumar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Tech Hunt</title>
		<link>http://rsjeyakumar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rsjeyakumar.wordpress.com/osd.xml" title="Tech Hunt" />
	<atom:link rel='hub' href='http://rsjeyakumar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Settings API Explained &#124; Plugin creation</title>
		<link>http://rsjeyakumar.wordpress.com/2012/01/12/settings-api-explained-plugin-creation/</link>
		<comments>http://rsjeyakumar.wordpress.com/2012/01/12/settings-api-explained-plugin-creation/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 12:30:53 +0000</pubDate>
		<dc:creator>rsjeyakumar</dc:creator>
				<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://rsjeyakumar.wordpress.com/?p=361</guid>
		<description><![CDATA[Update: Plugin Options Starter Kit I have now written a Plugin options starter kit for people who want to learn how to put together a Plugin options form using the WordPress Settings API. You can see more information about the Plugin and download the latest version here. Have fun, and be sure to let me &#8230; <a href="http://rsjeyakumar.wordpress.com/2012/01/12/settings-api-explained-plugin-creation/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=361&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="border:2px dashed #cccccc;color:#b94c11;margin:10px 0 15px;padding:10px 10px 0;">
<h3 style="font-size:15px;margin:0 0 5px;">Update: Plugin Options Starter Kit</h3>
<p style="font-size:13px;">I have now written a Plugin options starter kit for people who want to learn how to put together a Plugin options form using the WordPress Settings API. You can see more information about the Plugin and download the latest version <a href="http://wordpress.org/extend/plugins/plugin-options-starter-kit/" target="_blank"><strong>here</strong></a>.</p>
<p style="font-size:13px;">Have fun, and be sure to let me know what other features you want added in future versions!</p>
</div>
<p><em>In this post I discuss the WordPress Settings API and how you can leverage them for use in your own plugins. Firstly there is coverage of the API in general and the various functions available together with their usage. After that there is a tutorial section with an example walk through of how to add options to a new sub menu page under the Settings Menu.</em></p>
<p>The <a href="http://codex.wordpress.org/Settings_API" target="_blank">Settings API</a> was first added in WordPress 2.7 to make things more efficient when it came to adding options pages for your plugins (and themes) to the WordPress admin area. Until recently I was still doing things the ‘old’ way which involved doing all the form creation and submission handling yourself, checking for $_POST variables, manual security checks etc. Whilst this worked fine it was also cumbersome and time consuming to implement, and making changes was awkward.</p>
<p>Adding new options to an existing plugin was not the breeze it should have been and you certainly thought twice before committing to a plugin options revision! Also, all of the security implementation had to be done manually too. Now, with the Settings API all the security checks are handled automatically which is a huge bonus! You don’t even need to create the options form table yourself if you don’t want to, this is taken care of for you too. Basically, all you have to do is to register your options with the Settings API and specify the input fields you wish to add to the options page, and away you go!</p>
<p>The motivation behind writing this post is to bring together the information on how the Settings API can really make your life easier when developing plugins. Now, there is a clean way you can construct your options pages quickly leaving you with more time to focus on the core plugin functionality. Also making amendments later on is no trouble at all now.</p>
<h3>Settings API Functions</h3>
<p>There are six general functions that you will be using to get the most out of the Settings API. For quick reference, all the functions you need to get started are listed below, with full parameter specification as defined in the core WordPress files. The coloured function parameters (red, green, blue) indicate how particular parameter values overlap between the various Settings API functions. Any coloured parameters should hold identical values between functions to make everything work properly. All the other parameters not coloured are allowed to have different values between functions (e.g. all the <code>$callback</code> parameters should contain different values as they refer to separate function calls).</p>
<ul>
<li><span class="pc_code">settings_fields(<em><span class="m1">$option_group</span></em>);</span></li>
<li><span class="pc_code">register_setting(<em><span class="m1">$option_group</span>, <span class="m3">$option_name</span>, $sanitize_callback=</em>“”);</span></li>
<li><span class="pc_code">unregister_setting(<em><span class="m1">$option_group</span>, <span class="m3">$option_name</span>, $sanitize_callback=</em>“”);</span></li>
<li><span class="pc_code">add_settings_section(<em><span class="m6">$id</span>, $title, $callback, <span class="m5">$page</span></em>);</span></li>
<li><span class="pc_code">add_settings_field(<em>$id, $title, $callback, <span class="m5">$page</span>, <span class="m6">$section</span>, $args = array()</em>);</span></li>
<li><span class="pc_code">do_settings_sections(<em><span class="m5">$page</span></em>)</span></li>
</ul>
<p>The parameters and what they do are listed next:</p>
<ul>
<li><span class="pc_code"><em>$option_group</em></span> – unique group name for option set</li>
<li><span class="pc_code"><em>$option_name</em></span> – name of each option (more than one option in the same <code>register_settings()</code> function requires an array of options)</li>
<li><span class="pc_code"><em>$sanitize_callback=</em>“”</span> – section/field callback function to validate option data</li>
<li><span class="pc_code"><em>$id</em></span> – unique ID for the section/field</li>
<li><span class="pc_code"><em>$title</em></span> – the title of the section/field (displayed on options page)</li>
<li><span class="pc_code"><em>$callback</em></span> – callback function to be executed</li>
<li><span class="pc_code"><em>$page</em></span> – options page name (use __FILE__ if creating new options page)</li>
<li><span class="pc_code"><em>$section</em></span> – ID of the settings section (needs to be the same as $id in add_settings_section)</li>
<li><span class="pc_code"><em>$args = array()</em></span> – additional arguments</li>
</ul>
<p>Let’s now go through each of these Settings API functions one by one. Firstly, is the <code>settings_fields($option_group)</code> function which renders the hidden input fields and handles the security aspects (nonce etc.).</p>
<p>This function has only one parameter, <code>$option_group</code>, used for the options group name which must be a unique string. This string is also used as the first parameter of <code>register_setting()</code> and <code>unregister_setting()</code> functions (discussed below) and should be identical in all three functions.</p>
<p>Next we have the <code>register_settings()</code> function. It job is to register the option(s) being defined with the Settings API, and accepts three parameters. The first one is the group options name which is the same as in the <code>settings_fields()</code> function. You can define different sets of group options with this option should you choose to but most often you will likely use only one option group.</p>
<p>The second parameter is the name of the option(s) you are registering which is the name used to store the options in the options db table. The option name can be a reference to a single option or an array of options. It is considered good practice to group all of your options into one option variable (i.e. all your individual options are stored in an array) which reduces the queries to the database.</p>
<p>The <code>unregister_settings()</code> function is very similar to the <code>register_settings()</code> function except that it unregisters (not surprisingly!) the option(s) in question from the Settings API.</p>
<p>Moving on, next we have the <code>add_settings_section($id, $title, $callback, $page)</code> function. This creates the ‘section’ for the options you will be adding. You can have more than one section on an options page but usually for most pages you will probably only need one. This time we have four parameters which are quite straightforward.</p>
<p>First is the section ID, which must be unique. Then we have the title of the section that is displayed on the options page. This is followed by the callback function that displays the actual HTML of the section (such as introductory information, perhaps donation links etc.). Finally we have the page parameter which needs to be the same string as specified in the <code>add_settings_field()</code>, and <code>do_settings_sections()</code> functions (covered below).</p>
<p>Next we have <code>add_settings_field($id, $title, $callback, $page, $section, $args = array())</code>, which is closely relate to the <code>add_settings_section()</code> function. This function adds the option field to the newly created section, and you will need to place a call to it in your code for every new option you wish to add. So, if you have three textboxes and two checkboxes on your options page you will need a total of five calls to the <code>add_settings_field()</code> function. Also it is worth mentioning that the order in which you place the calls to <code>add_settings_field()</code> will determine how the options are rendered on the options page. To re-order the options simply swap the function calls around! How easy is that?</p>
<p>The first four parameters are almost identical to the previous function where you specify a unique ID for each field added to the Settings API, give it a field title that is displayed on the page, a callback function that actually renders the field, and a page – which must be the same one specified in the <code>add_settings_section()</code> function. The fifth parameter specifies the ID of the settings section. This needs to be identical to ID you entered for the first parameter of <code>add_settings_section()</code> previously. Finally, the last parameter allows you to add additional arguments.</p>
<p>I think it is worth mentioning here that the ID used in the first parameter of the <code>add_settings_field()</code> function does NOT have to match the ID for the input field HTML tag specified in the callback function. Most of the time it is advisable to do so to keep things simple but, as you will see later on for radio buttons, the ID’s for the input field HTML tags can be different.</p>
<p>I initially thought that they had to be identical for everything to work properly but after experimentation this does not appear to be the case at all. This only seems to be useful when adding radio button options to your plugin – as it makes no logical sense to have multiple HTML input tags with identical ID’s. For other controls I would strongly suggest (for simplicity) that you match the <code>add_settings_field()</code> ID to the callback function HTML input field tag ID.</p>
<p>Finally we have the <code>do_settings_sections($page)</code> function. This function handles the rendering loop which displays all of your option input fields to the form on the options page. It has only one parameter, $page, which must be the same as specified in the last two functions: <code>add_settings_section()</code>, and <code>add_settings_field()</code>.</p>
<p>OK, so that is all the major Settings API functions covered. We will now discuss the various input field types that can be added to options pages and how you can use these with the settings API. There are various input field types that you can use on your options pages (checkbox, radio button, dropdown box, textbox, password box, and textarea). To add each one requires the correct integration with the Settings API. Basically the callback function you specified in the <code>add_settings_field()</code> function is where you place HTML code to render the options. Let’s now go through all the input field types and see an example callback function for each option control type.</p>
<h4 class="h4_bold">Drop Down Box</h4>
<p>The code to add a drop down combo style box is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span>  setting_dropdown_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#000088;">$items</span> <span style="color:#339933;">=</span> <span style="color:#990000;">array</span><span style="color:#009900;">(</span><span style="color:#0000ff;">"Red"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Green"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Blue"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Orange"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"White"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Violet"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Yellow"</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;select id='drop_down1' name='plugin_options[dropdown1]'&gt;"</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">foreach</span><span style="color:#009900;">(</span><span style="color:#000088;">$items</span> <span style="color:#b1b100;">as</span> <span style="color:#000088;">$item</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
		<span style="color:#000088;">$selected</span> <span style="color:#339933;">=</span> <span style="color:#009900;">(</span><span style="color:#000088;">$options</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'dropdown1'</span><span style="color:#009900;">]</span><span style="color:#339933;">==</span><span style="color:#000088;">$item</span><span style="color:#009900;">)</span> ? <span style="color:#0000ff;">'selected="selected"'</span> <span style="color:#339933;">:</span> <span style="color:#0000ff;">''</span><span style="color:#339933;">;</span>
		<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;option value='<span style="color:#006699;font-weight:bold;">$item</span>' <span style="color:#006699;font-weight:bold;">$selected</span>&gt;<span style="color:#006699;font-weight:bold;">$item</span>&lt;/option&gt;"</span><span style="color:#339933;">;</span>
	<span style="color:#009900;">}</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;/select&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Here, we basically get the current state of the option from the db, then loop through the available color options. If we find the one that is selected then we render it as the selected item otherwise it is added to the list of available options (but not selected). This good thing about this method is that it is nice and scalable for any number of options.</p>
<h4 class="h4_bold">Textarea</h4>
<p>The code to add a textarea control is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_textarea_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;textarea id='plugin_textarea_string' name='plugin_options[text_area]' rows='7' cols='50' type='textarea'&gt;<span style="color:#006699;font-weight:bold;">{$options['text_area']}</span>&lt;/textarea&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Nice and easy this one. Just get the current value (if any), and add it to the textarea!</p>
<h4 class="h4_bold">Textbox (single line)</h4>
<p>The code to add a textbox control is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_string_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='<span style="color:#006699;font-weight:bold;">{$options['text_string']}</span>' /&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Another easy this one. Again, just get the current value (if any), and add it to the textbox!</p>
<h4 class="h4_bold">Password Textbox (single line)</h4>
<p>The code to add a password textbox control is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_pass_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;input id='plugin_text_pass' name='plugin_options[pass_string]' size='40' type='password' value='<span style="color:#006699;font-weight:bold;">{$options['pass_string']}</span>' /&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>This one is pretty much identical to the textbox except the input is masked.</p>
<h4 class="h4_bold">Checkbox</h4>
<p>The code to add a checkbox control is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_chk1_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">if</span><span style="color:#009900;">(</span><span style="color:#000088;">$options</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'chkbox1'</span><span style="color:#009900;">]</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span> <span style="color:#000088;">$checked</span> <span style="color:#339933;">=</span> <span style="color:#0000ff;">' checked="checked" '</span><span style="color:#339933;">;</span> <span style="color:#009900;">}</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;input "</span><span style="color:#339933;">.</span><span style="color:#000088;">$checked</span><span style="color:#339933;">.</span><span style="color:#0000ff;">" id='plugin_chk1' name='plugin_options[chkbox1]' type='checkbox' /&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>To add a checkbox we need to toggle the setting each time it is selected (and if the Save Changes option button has been clicked). For each checkbox we check the db option setting to see if it has been selected/deselected, and if so then the control needs to be updated.</p>
<h4 class="h4_bold">Radio Buttons</h4>
<p>The code to add radio button controls is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_radio_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#000088;">$items</span> <span style="color:#339933;">=</span> <span style="color:#990000;">array</span><span style="color:#009900;">(</span><span style="color:#0000ff;">"Square"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Triangle"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"Circle"</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">foreach</span><span style="color:#009900;">(</span><span style="color:#000088;">$items</span> <span style="color:#b1b100;">as</span> <span style="color:#000088;">$item</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
		<span style="color:#000088;">$checked</span> <span style="color:#339933;">=</span> <span style="color:#009900;">(</span><span style="color:#000088;">$options</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'option_set1'</span><span style="color:#009900;">]</span><span style="color:#339933;">==</span><span style="color:#000088;">$item</span><span style="color:#009900;">)</span> ? <span style="color:#0000ff;">' checked="checked" '</span> <span style="color:#339933;">:</span> <span style="color:#0000ff;">''</span><span style="color:#339933;">;</span>
		<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;label&gt;&lt;input "</span><span style="color:#339933;">.</span><span style="color:#000088;">$checked</span><span style="color:#339933;">.</span><span style="color:#0000ff;">" value='<span style="color:#006699;font-weight:bold;">$item</span>' name='plugin_options[option_set1]' type='radio' /&gt; <span style="color:#006699;font-weight:bold;">$item</span>&lt;/label&gt;&lt;br /&gt;"</span><span style="color:#339933;">;</span>
	<span style="color:#009900;">}</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>To add radio buttons you obviously need more than one, or there isn’t much point to using them! So, you need to define a ‘set’ of buttons. Here I have created an example of a set of shapes. The current db option is obtained as usual and stored in $options. Similar to the drop down box, each possible option is looped through until the db selected option is encountered. The db item is rendered to the screen as selected, and the other options are just displayed unchecked.</p>
<p>OK, enough theory.. Now for the REALLY good stuff – the tutorial! The following section covers a complete walk through on how to add options to a new page under the Settings Menu. The full code is given at the end for you to copy/paste into your own plugins as a starting template.</p>
<h3>Tutorial – Adding a New Options Page</h3>
<p>OK, let’s build a complete options page using the new Settings API armed with all the theory we have learned so far. First though, a couple of notes. All the callback functions have been added with a suffix of <code>'_fn'</code> to denote it refers to a callback function. There are a lot of parameters flying around so I think it is useful to know just at a glance what is a callback function and what is an ID etc. Also, the tutorial uses the method of storing all page options in a single options db table entry, which is a cleaner way to add your options to the WordPress db.</p>
<p>You will see the PHP <a href="http://php.net/manual/en/language.constants.predefined.php" target="_blank">‘magic constant’</a> __FILE__ pop up a fair bit. All this fancy looking variable does is use the plugin file name as a constant in a place where a unique string is needed. __FILE__ is considered the correct constant to use in these circumstances as (hopefully!) your plugin name should be pretty much unique compared to the other plugins under your WordPress installation. Also, it has the added advantage that it saves us from having to come up with unique page names.</p>
<p>The first thing we need to do is create our sub menu page under the top level Settings Menu. This is done by using the <code>add_options_page('Options Example Page', 'Options Example', 'administrator', __FILE__, 'options_page_fn')</code> function. Here the parameters (in order) are: page title (displayed in the browser title bar), options menu title (displayed as a link in the Settings Menu), access level (who can access the options page), unique page name, and callback function to display the options form. The code to add the new menu page plus action hook is:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;">add_action<span style="color:#009900;">(</span><span style="color:#0000ff;">'admin_menu'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'sampleoptions_add_page_fn'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#666666;font-style:italic;">// Add sub page to the Settings Menu</span>
<span style="color:#000000;font-weight:bold;">function</span> sampleoptions_add_page_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	add_options_page<span style="color:#009900;">(</span><span style="color:#0000ff;">'Options Example Page'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Options Example'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'administrator'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'options_page_fn'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>While we are at it let’s define the <code>options_page_fn()</code> callback function:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;">function options_page_fn() {
?&gt;
	&lt;div class="wrap"&gt;
		&lt;div class="icon32" id="icon-options-general"&gt;&lt;br&gt;&lt;/div&gt;
		&lt;h2&gt;My Example Options Page&lt;/h2&gt;
		Some optional text here explaining the overall purpose of the options and what they relate to etc.
		&lt;form action="options.php" method="post"&gt;
		<span style="color:#000000;font-weight:bold;">&lt;?php</span> settings_fields<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span> <span style="color:#000000;font-weight:bold;">?&gt;</span>
		<span style="color:#000000;font-weight:bold;">&lt;?php</span> do_settings_sections<span style="color:#009900;">(</span><span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span> <span style="color:#000000;font-weight:bold;">?&gt;</span>
		&lt;p class="submit"&gt;
			&lt;input name="Submit" type="submit" class="button-primary" value="<span style="color:#000000;font-weight:bold;">&lt;?php</span> esc_attr_e<span style="color:#009900;">(</span><span style="color:#0000ff;">'Save Changes'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span> <span style="color:#000000;font-weight:bold;">?&gt;</span>" /&gt;
		&lt;/p&gt;
		&lt;/form&gt;
	&lt;/div&gt;
<span style="color:#000000;font-weight:bold;">&lt;?php</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>OK, so what is going on here? Well the nice thing about the Settings API is that it handles a lot of the tedious set up and rendering of your options form. The form is pretty standard apart from the two function calls inside the opening/closing form tags. Firstly is the <code>settings_fields('plugin_options');</code> function which basically outputs all the hidden fields automatically (including all the security stuff – nonces etc.). Next is the <code>do_settings_sections(__FILE__)</code> function which handles the rendering of the form input field HTML tags (it works by cycling through all the defined <code>add_settings_field()</code> functions and executing each callback function in turn to output the HTML).</p>
<p>To complete the setup (yes it is nearly done!) all you need to do is register your settings, and define the fields you wish to add to your options page plus the callback functions that define the HTML rendering of the option fields. So let’s do that now.</p>
<p>We place all the code to register settings in a callback function that runs when the ‘admin_init’ hook fires. The code looks like this:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;">add_action<span style="color:#009900;">(</span><span style="color:#0000ff;">'admin_init'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'sampleoptions_init_fn'</span> <span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#666666;font-style:italic;">// Register our settings. Add the settings section, and settings fields</span>
<span style="color:#000000;font-weight:bold;">function</span> sampleoptions_init_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span><span style="color:#009900;">{</span>
	register_setting<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'plugin_options'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'plugin_options_validate'</span> <span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_section<span style="color:#009900;">(</span><span style="color:#0000ff;">'main_section'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Main Settings'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'section_text_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_text_string'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Text Input'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_string_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_text_pass'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Password Text Input'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_pass_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_textarea_string'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Large Textbox!'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_textarea_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_chk2'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'A Checkbox'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_chk2_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'radio_buttons'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Select Shape'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_radio_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'drop_down1'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Select Color'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_dropdown_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	add_settings_field<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_chk1'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'Restore Defaults Upon Reactivation?'</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'setting_chk1_fn'</span><span style="color:#339933;">,</span> <span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'main_section'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Here we need to register our settings with the Settings API, and we do this with the <code>register_setting('plugin_options', 'plugin_options', 'plugin_options_validate' )</code> function. The first parameter is the option group name that identifies all of the options in the set. The second parameter is the options name that is stored in the options db. Finally the third function defines an optional validation function that can be used to validate user input (more on this later).</p>
<p>Next we define our section that will contain all our option fields: <code>add_settings_section('main_section', 'Main Settings', 'section_text_fn', __FILE__)</code>. The parameters in order are: ID for the section, title (to be displayed on the options page), callback function (that displays section information), and finally – page (defined as the constant __FILE__).</p>
<p>After the section definition we now define the individual fields themselves. Each option on your form needs to have a call made to a <code>add_settings_field('plugin_text_string', 'Text Input', 'setting_string_fn', __FILE__, 'main_section')</code> function. The parameters are similar to the <code>add_settings_section('main_section', 'Main Settings', 'section_text_fn', __FILE__)</code> function. They are specified as: field ID, title of field (displayed on the left hand side of the options page), callback function (to render the input field), page name (__FILE__ constant again), and finally a reference to the section name (same as the first parameter in the add_settings_section function).</p>
<p>Each callback function in <code>add_settings_field()</code> needs to be defined. We showed all the possible types earlier and so for completeness lets just show the callback function here for the field defined above:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> setting_string_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$options</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">echo</span> <span style="color:#0000ff;">"&lt;input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='<span style="color:#006699;font-weight:bold;">{$options['text_string']}</span>' /&gt;"</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Now we know how to structure the options page setup lets look at another couple of important points. Firstly, how do we specify default options? Also, how do we use the validate callback function defined in <code>register_settings()</code>. Firstly the default options…</p>
<p>To set these up we need to specify a hook that runs when the plugin is activated on the plugins page. This will trigger a callback function to be executed (which we specified in the hook definition). The callback function will then add the required defaults to the options db, as follows:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;">register_activation_hook<span style="color:#009900;">(</span><span style="color:#009900;font-weight:bold;">__FILE__</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">'add_defaults_fn'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#666666;font-style:italic;">// Define default option settings</span>
<span style="color:#000000;font-weight:bold;">function</span> add_defaults_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
    <span style="color:#000088;">$arr</span> <span style="color:#339933;">=</span> <span style="color:#990000;">array</span><span style="color:#009900;">(</span><span style="color:#0000ff;">"dropdown1"</span><span style="color:#339933;">=&gt;</span><span style="color:#0000ff;">"Orange"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"text_area"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Space to put a lot of information here!"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"text_string"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Some sample text"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"pass_string"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"123456"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"chkbox1"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">""</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"chkbox2"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"on"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"option_set1"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Triangle"</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
    update_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#339933;">,</span> <span style="color:#000088;">$arr</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Taking this one step further, it would be nice if we could specify one of our options as a switch to determine if we ALWAYS want the options db entries reset every time the plugin is first activated (or whenever the plugin is deactivated/reactivated), or whether to leave them as they were as sometimes you will want to temporarily want to deactivate a plugin but not delete/reset the options. This way, the next time you activate the plugin the options will still be there. Anyway, it is a nice feature to give to your users. The following modified <code>add_defaults_fn()</code> function handles this:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> add_defaults_fn<span style="color:#009900;">(</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#000088;">$tmp</span> <span style="color:#339933;">=</span> get_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
    <span style="color:#b1b100;">if</span><span style="color:#009900;">(</span><span style="color:#009900;">(</span><span style="color:#000088;">$tmp</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'chkbox1'</span><span style="color:#009900;">]</span><span style="color:#339933;">==</span><span style="color:#0000ff;">'on'</span><span style="color:#009900;">)</span><span style="color:#339933;">||</span><span style="color:#009900;">(</span><span style="color:#339933;">!</span><span style="color:#990000;">is_array</span><span style="color:#009900;">(</span><span style="color:#000088;">$tmp</span><span style="color:#009900;">)</span><span style="color:#009900;">)</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
		<span style="color:#000088;">$arr</span> <span style="color:#339933;">=</span> <span style="color:#990000;">array</span><span style="color:#009900;">(</span><span style="color:#0000ff;">"dropdown1"</span><span style="color:#339933;">=&gt;</span><span style="color:#0000ff;">"Orange"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"text_area"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Space to put a lot of information here!"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"text_string"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Some sample text"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"pass_string"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"123456"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"chkbox1"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">""</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"chkbox2"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"on"</span><span style="color:#339933;">,</span> <span style="color:#0000ff;">"option_set1"</span> <span style="color:#339933;">=&gt;</span> <span style="color:#0000ff;">"Triangle"</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
		update_option<span style="color:#009900;">(</span><span style="color:#0000ff;">'plugin_options'</span><span style="color:#339933;">,</span> <span style="color:#000088;">$arr</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#009900;">}</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Now the options db entries only ever get overwritten if the checkbox option to restore the defaults is checked. Also, the very first time the plugin is activated then there will be no current options to overwrite and this is where the second test in the line <code> if(($tmp['chkbox1']=='on')||(!is_array($tmp)))</code> comes in. Basically, if there is no options in the db then it creates them with default settings.</p>
<p>OK, deep breath, nearly done. Finally, it is worth showing you how to use the validation feature built in to the Settings API. If you remember this is specified as a callback function in the third parameter of the <code>register_settings()</code> function.</p>
<p>Note: Remember that if you define this callback function in <code>register_settings()</code> then you must define this function in code otherwise you may get unexpected results (as WordPress will not be able to find a function that it is expecting). The first time I specified a validation function I forgot to implement it and it took me a while to realise what I had done wrong, and why the options form was not displaying properly!</p>
<p>The validation feature is a really nice touch of the Settings API and is worth using, it can be very powerful too. As a simple example, for the textbox we defined, how would you like a super easy way to validate input so that no HTML tags were allowed. If any are found then they are simply striped out! Nice eh? The code to do this is as follows:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color:#000000;font-weight:bold;">function</span> plugin_options_validate<span style="color:#009900;">(</span><span style="color:#000088;">$input</span><span style="color:#009900;">)</span> <span style="color:#009900;">{</span>
	<span style="color:#666666;font-style:italic;">// Check our textbox option field contains no HTML tags - if so strip them out</span>
	<span style="color:#000088;">$input</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'text_string'</span><span style="color:#009900;">]</span> <span style="color:#339933;">=</span>  wp_filter_nohtml_kses<span style="color:#009900;">(</span><span style="color:#000088;">$input</span><span style="color:#009900;">[</span><span style="color:#0000ff;">'text_string'</span><span style="color:#009900;">]</span><span style="color:#009900;">)</span><span style="color:#339933;">;</span>
	<span style="color:#b1b100;">return</span> <span style="color:#000088;">$input</span><span style="color:#339933;">;</span> <span style="color:#666666;font-style:italic;">// return validated input</span>
<span style="color:#009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>This is especially nice as we can test some/all of our options and validate them as we see fit. In the above example we are only testing the text field but we can easily extend this and add whatever validation code we like!</p>
<p>OK, let’s now add the rest of our input field options as we defined earlier in the post. Putting everything we have learned together, the finished options page looks something like the image below.</p>
<p><a class="fancybox" title="settings_api_example" href="http://www.presscoders.com/wp-content/uploads/2010/05/settings_api_example.png" rel="fancybox"><img class="alignnone size-medium wp-image-492" title="settings_api_example" src="http://www.presscoders.com/wp-content/uploads/2010/05/settings_api_example-300x258.png" alt="" width="300" height="258" /></a></p>
<p>The full code to render the complete options page <a href="http://www.presscoders.com/wp-content/uploads/text/ExampleOptionsPage.txt" target="_blank">can be found here</a>. To test this in your own WordPress installation simply copy/paste the code into a new text file, save it and upload as a new plugin. You can then begin to experiment and build your own options pages from this example template. Enjoy..! <span class="no-img-border"> <img class="wp-smiley" src="http://www.presscoders.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" /> </span></p>
<h4 class="h4_bold">In Summary</h4>
<p>In this post we looked at the Settings API available in WordPress which makes it much(!) easier to add/maintain options for your plugins. The tutorial showed an example of adding a new sub page to the Settings Menu. Adding new options to existing menu pages as well as adding new sub pages to other top level menus (such as the Tools Menu, and Appearance Menu) are fairly straight forward. These will be covered in another post on the Settings API.</p>
<p>I hope you have found this post helpful, if so I would love to hear your comments…</p>
<br />Filed under: <a href='http://rsjeyakumar.wordpress.com/category/all/open-source-technology/wordpress/'>WordPress</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rsjeyakumar.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rsjeyakumar.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rsjeyakumar.wordpress.com/361/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=361&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rsjeyakumar.wordpress.com/2012/01/12/settings-api-explained-plugin-creation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f8f8425db9265a761a6efc327306688?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsjeyakumar</media:title>
		</media:content>

		<media:content url="http://www.presscoders.com/wp-content/uploads/2010/05/settings_api_example-300x258.png" medium="image">
			<media:title type="html">settings_api_example</media:title>
		</media:content>

		<media:content url="http://www.presscoders.com/wp-includes/images/smilies/icon_smile.gif" medium="image">
			<media:title type="html">:)</media:title>
		</media:content>
	</item>
		<item>
		<title>How to disable / remove the WordPress 3.1 admin bar, redux</title>
		<link>http://rsjeyakumar.wordpress.com/2012/01/12/how-to-disable-remove-the-wordpress-3-1-admin-bar-redux/</link>
		<comments>http://rsjeyakumar.wordpress.com/2012/01/12/how-to-disable-remove-the-wordpress-3-1-admin-bar-redux/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 11:47:59 +0000</pubDate>
		<dc:creator>rsjeyakumar</dc:creator>
				<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://rsjeyakumar.wordpress.com/?p=353</guid>
		<description><![CDATA[Steps: 1.Login admin panel. 2.Click &#8220;users&#8221; tab from Left side menu 3.Click your Profile and follow the screen shots.. Filed under: WordPress<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=353&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Steps:<br />
1.Login admin panel.<br />
2.Click &#8220;users&#8221; tab from Left side menu<br />
3.Click your Profile and follow the screen shots..</p>
<p style="text-align:center;"><a href="http://wpmu.org/how-to-disable-remove-the-wordpress-3-1-admin-bar-redux/"><img src='http://rsjeyakumar.files.wordpress.com/2012/01/showadminbarornotwordpress.jpg?w=530' alt='' /></a></p>
<br />Filed under: <a href='http://rsjeyakumar.wordpress.com/category/all/open-source-technology/wordpress/'>WordPress</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rsjeyakumar.wordpress.com/353/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rsjeyakumar.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rsjeyakumar.wordpress.com/353/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=353&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rsjeyakumar.wordpress.com/2012/01/12/how-to-disable-remove-the-wordpress-3-1-admin-bar-redux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f8f8425db9265a761a6efc327306688?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsjeyakumar</media:title>
		</media:content>

		<media:content url="http://rsjeyakumar.files.wordpress.com/2012/01/showadminbarornotwordpress.jpg" medium="image" />
	</item>
		<item>
		<title>Adding special characters between menu items using WP_LIST_PAGES</title>
		<link>http://rsjeyakumar.wordpress.com/2011/12/27/adding-special-characters-between-menu-items-using-wp_list_pages/</link>
		<comments>http://rsjeyakumar.wordpress.com/2011/12/27/adding-special-characters-between-menu-items-using-wp_list_pages/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 11:48:19 +0000</pubDate>
		<dc:creator>rsjeyakumar</dc:creator>
				<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://rsjeyakumar.wordpress.com/?p=348</guid>
		<description><![CDATA[This should do the trick,Here i am using Divider(&#124;) between the name,It&#8217;s working fine in my client site. You also try to do this one.. this code help for you Filed under: WordPress<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=348&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This should do the trick,Here i am using  Divider(|) between the name,It&#8217;s working fine in my client site. You also try to do this one.. this code  help for you<br />
<pre class="brush: plain;">
&lt;?php 
$links = wp_list_pages('title_li=&amp;categorize=0&amp;ort_column=ID&amp;include=5,7,9&amp;echo=0');
$pages = explode('&lt;/li&gt;', $links); //create array from string returned by wp_list_bookmarks
array_pop($pages); //pop last element off array
echo implode('&lt;span class=&quot;divider&quot;&gt; | &lt;/span&gt;', $pages);?&gt;
</pre></p>
<br />Filed under: <a href='http://rsjeyakumar.wordpress.com/category/all/open-source-technology/wordpress/'>WordPress</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rsjeyakumar.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rsjeyakumar.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rsjeyakumar.wordpress.com/348/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=348&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rsjeyakumar.wordpress.com/2011/12/27/adding-special-characters-between-menu-items-using-wp_list_pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f8f8425db9265a761a6efc327306688?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsjeyakumar</media:title>
		</media:content>
	</item>
		<item>
		<title>Regular Expressions</title>
		<link>http://rsjeyakumar.wordpress.com/2011/12/21/regular-expressions/</link>
		<comments>http://rsjeyakumar.wordpress.com/2011/12/21/regular-expressions/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 07:05:23 +0000</pubDate>
		<dc:creator>rsjeyakumar</dc:creator>
				<category><![CDATA[java script]]></category>
		<category><![CDATA[form validation scripts]]></category>
		<category><![CDATA[literal syntax]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[regular expression pattern]]></category>
		<category><![CDATA[social security number]]></category>

		<guid isPermaLink="false">http://rsjeyakumar.wordpress.com/?p=345</guid>
		<description><![CDATA[In this lesson of the JavaScript tutorial, you will learn&#8230; To use regular expressions for advanced form validation. To use regular expressions and backreferences to clean up form entries. Getting Started Regular expressions are used to do sophisticated pattern matching, which can often be helpful in form validation. For example, a regular expression can be &#8230; <a href="http://rsjeyakumar.wordpress.com/2011/12/21/regular-expressions/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=345&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1 id="LessonTitle"></h1>
<div id="LearnItems"><strong>In this lesson of the JavaScript tutorial, you will learn&#8230;</strong></p>
<ol>
<li>To use regular expressions for advanced form validation.</li>
<li>To use regular expressions and backreferences to clean up form entries.</li>
</ol>
<div></div>
</div>
<h1 id="h1.1">Getting Started</h1>
<p>Regular expressions are used to do sophisticated pattern matching, which can often be helpful in form validation. For example, a regular expression can be used to check whether an email address entered into a form field is syntactically correct. JavaScript supports Perl-compatible regular expressions.</p>
<p>There are two ways to create a regular expression in JavaScript:</p>
<ol>
<li>Using literal syntax
<div>
<pre>var reExample = /pattern/;</pre>
</div>
</li>
<li>Using the RegExp() constructor
<div>
<pre>var reExample = new RegExp("pattern");</pre>
</div>
</li>
</ol>
<p>Assuming you know the regular expression pattern you are going to use, there is no real difference between the two; however, if you don&#8217;t know the pattern ahead of time (e.g, you&#8217;re retrieving it from a form), it can be easier to use the RegExp() constructor.</p>
<h2 id="h2.1">JavaScript&#8217;s Regular Expression Methods</h2>
<p>The regular expression method in JavaScript has two main methods for testing strings: test() and exec().</p>
<h3 id="h3.1">The exec() Method</h3>
<p>The exec() method takes one argument, a string, and checks whether that string contains <em>one or more</em> matches of the pattern specified by the regular expression. If one or more matches is found, the method returns a result array with the starting points of the matches. If no match is found, the method returns null.</p>
<h3 id="h3.2">The test() Method</h3>
<p>The test() method also takes one argument, a string, and checks whether that string contains a match of the pattern specified by the regular expression. It returns true if it does contain a match and false if it does not. This method is very useful in form validation scripts. The code sample below shows how it can be used for checking a social security number. Don&#8217;t worry about the syntax of the regular expression itself. We&#8217;ll cover that shortly.</p>
<div>
<h2>Code Sample: RegularExpressions/Demos/SsnChecker.html</h2>
<div>
<div>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;ssn Checker&lt;/title&gt;
&lt;script type="text/javascript"&gt;<strong> var RE_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/; function checkSsn(ssn){ if (RE_SSN.test(ssn)) { alert("VALID SSN"); } else { alert("INVALID SSN"); } }</strong>
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;form onsubmit="return false;"&gt;
  &lt;input type="text" name="ssn" size="20"&gt;<strong> &lt;input type="button" value="Check" onclick="checkSsn(this.form.ssn.value);"&gt;</strong>
 &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
</div>
</div>
<div>Code Explanation</div>
<blockquote><p>Let&#8217;s examine the code more closely:</p>
<ol>
<li>First, a variable containing a regular expression object for a social security number is declared.
<div>
<pre>var RE_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;</pre>
</div>
</li>
<li>Next, a function called checkSsn() is created. This function takes one argument: ssn, which is a string. The function then tests to see if the string matches the regular expression pattern by passing it to the regular expression object&#8217;s test() method. If it does match, the function alerts &#8220;VALID SSN&#8221;. Otherwise, it alerts &#8220;INVALID SSN&#8221;.
<div>
<pre>function checkSsn(ssn){
 if (RE_SSN.test(ssn)) {
  alert("VALID SSN");
 } else {
  alert("INVALID SSN");
 }
}</pre>
</div>
</li>
<li>A form in the body of the page provides a text field for inserting a social security number and a button that passes the user-entered social security number to the checkSsn() function.
<div>
<pre>&lt;form onsubmit="return false;"&gt;
 &lt;input type="text" name="ssn" size="20"&gt;
 &lt;input type="button" value="Check"
  onclick="checkSsn(this.form.ssn.value);"&gt;
&lt;/form&gt;</pre>
</div>
</li>
</ol>
</blockquote>
<h2 id="h2.2">Flags</h2>
<p>Flags appearing after the end slash modify how a regular expression works.</p>
<ul>
<li>The i flag makes a regular expression case insensitive. For example, /aeiou/i matches all lowercase and uppercase vowels.</li>
<li>The g flag specifies a global match, meaning that all matches of the specified pattern should be returned.</li>
</ul>
<h2 id="h2.3">String Methods</h2>
<p>There are several String methods that use regular expressions.</p>
<h3 id="h3.3">The search() Method</h3>
<p>The search() method takes one argument: a regular expression. It returns the index of the first character of the substring matching the regular expression. If no match is found, the method returns -1.</p>
<div>
<pre>"Webucator".search(/cat/); //returns 4</pre>
</div>
<h3 id="h3.4">The split() Method</h3>
<p>The split() method takes one argument: a regular expression. It uses the regular expression as a delimiter to split the string into an array of strings.</p>
<div>
<pre>"Webucator".split(/[aeiou]/);
/*
 returns an array with the following values:
 "W", "b", "c", "t", "r"
*/</pre>
</div>
<h3 id="ReplaceMethod">The replace() Method</h3>
<p>The replace() method takes two arguments: a regular expression and a string. It replaces the first regular expression match with the string. If the g flag is used in the regular expression, it replaces all matches with the string.</p>
<div>
<pre>"Webucator".replace(/cat/, "dog"); //returns Webudogor
"Webucator".replace(/[aeiou]/g, "x"); //returns Wxbxcxtxr</pre>
</div>
<h3 id="h3.6">The match() Method</h3>
<p>The match() method takes one argument: a regular expression. It returns each substring that matches the regular expression pattern.</p>
<div>
<pre>"Webucator".match(/[aeiou]/g);
/*
 returns an array with the following values:
 "e", "u", "a", "o"
*/</pre>
</div>
<h1 id="h1.2">Regular Expression Syntax</h1>
<p>A regular expression is a pattern that specifies a list of characters. In this section, we will look at how those characters are specified.</p>
<h2 id="h2.4">Start and End ( ^ $ )</h2>
<p>A caret (^) at the beginning of a regular expression indicates that the string being searched must start with this pattern.</p>
<ul>
<li>The pattern ^foo can be found in &#8220;food&#8221;, but not in &#8220;barfood&#8221;.</li>
</ul>
<p>A dollar sign ($) at the end of a regular expression indicates that the string being searched must end with this pattern.</p>
<ul>
<li>The pattern foo$ can be found in &#8220;curfoo&#8221;, but not in &#8220;food&#8221;.</li>
</ul>
<h2 id="h2.5">Number of Occurrences ( ? + * {} )</h2>
<p>The following symbols affect the number of occurrences of the preceding character: ?, +, *, and {}.</p>
<p>A questionmark (?) indicates that the preceding character should appear zero or one times in the pattern.</p>
<ul>
<li>The pattern foo? can be found in &#8220;food&#8221; and &#8220;fod&#8221;, but not &#8220;faod&#8221;.</li>
</ul>
<p>A plus sign (+) indicates that the preceding character should appear one or more times in the pattern.</p>
<ul>
<li>The pattern fo+ can be found in &#8220;fod&#8221;, &#8220;food&#8221; and &#8220;foood&#8221;, but not &#8220;fd&#8221;.</li>
</ul>
<p>A asterisk (*) indicates that the preceding character should appear zero or more times in the pattern.</p>
<ul>
<li>The pattern fo*d can be found in &#8220;fd&#8221;, &#8220;fod&#8221; and &#8220;food&#8221;.</li>
</ul>
<p>Curly brackets with one parameter ( {n} ) indicate that the preceding character should appear exactly n times in the pattern.</p>
<ul>
<li>The pattern fo{3}d can be found in &#8220;foood&#8221; , but not &#8220;food&#8221; or &#8220;fooood&#8221;.</li>
</ul>
<p>Curly brackets with two parameters ( {n1,n2} ) indicate that the preceding character should appear between n1 and n2 times in the pattern.</p>
<ul>
<li>The pattern fo{2,4}d can be found in &#8220;food&#8221;,&#8221;foood&#8221; and &#8220;fooood&#8221;, but not &#8220;fod&#8221; or &#8220;foooood&#8221;.</li>
</ul>
<p>Curly brackets with one parameter and an empty second paramenter ( {n,} ) indicate that the preceding character should appear at least n times in the pattern.</p>
<ul>
<li>The pattern fo{2,}d can be found in &#8220;food&#8221; and &#8220;foooood&#8221;, but not &#8220;fod&#8221;.</li>
</ul>
<h2 id="h2.6">Common Characters ( . \d \D \w \W \s \S )</h2>
<p>A period ( . ) represents any character except a newline.</p>
<ul>
<li>The pattern fo.d can be found in &#8220;food&#8221;, &#8220;foad&#8221;, &#8220;fo9d&#8221;, and &#8220;fo*d&#8221;.</li>
</ul>
<p>Backslash-d ( \d ) represents any digit. It is the equivalent of <a href="http://www.learn-javascript-tutorial.com/RegularExpressions.cfm#Grouping">[0-9]</a>.</p>
<ul>
<li>The pattern fo\dd can be found in &#8220;fo1d&#8221;, &#8220;fo4d&#8221; and &#8220;fo0d&#8221;, but not in &#8220;food&#8221; or &#8220;fodd&#8221;.</li>
</ul>
<p>Backslash-D ( \D ) represents any character except a digit. It is the equivalent of <a href="http://www.learn-javascript-tutorial.com/RegularExpressions.cfm#Negation">[^0-9]</a>.</p>
<ul>
<li>The pattern fo\Dd can be found in &#8220;food&#8221; and &#8220;foad&#8221;, but not in &#8220;fo4d&#8221;.</li>
</ul>
<p>Backslash-w ( \w ) represents any word character (letters, digits, and the underscore (_) ).</p>
<ul>
<li>The pattern fo\wd can be found in &#8220;food&#8221;, &#8220;fo_d&#8221; and &#8220;fo4d&#8221;, but not in &#8220;fo*d&#8221;.</li>
</ul>
<p>Backslash-W ( \W ) represents any character except a word character.</p>
<ul>
<li>The pattern fo\Wd can be found in &#8220;fo*d&#8221;, &#8220;fo@d&#8221; and &#8220;fo.d&#8221;, but not in &#8220;food&#8221;.</li>
</ul>
<p>Backslash-s ( \s) represents any whitespace character (e.g, space, tab, newline, etc.).</p>
<ul>
<li>The pattern fo\sd can be found in &#8220;fo d&#8221;, but not in &#8220;food&#8221;.</li>
</ul>
<p>Backslash-S ( \S ) represents any character except a whitespace character.</p>
<ul>
<li>The pattern fo\Sd can be found in &#8220;fo*d&#8221;, &#8220;food&#8221; and &#8220;fo4d&#8221;, but not in &#8220;fo d&#8221;.</li>
</ul>
<h2 id="Grouping">Grouping ( [] )</h2>
<p>Square brackets ( [] ) are used to group options.</p>
<ul>
<li>The pattern f[aeiou]d can be found in &#8220;fad&#8221; and &#8220;fed&#8221;, but not in &#8220;food&#8221;, &#8220;faed&#8221; or &#8220;fd&#8221;.</li>
<li>The pattern f[aeiou]{2}d can be found in &#8220;faed&#8221; and &#8220;feod&#8221;, but not in &#8220;fod&#8221;, &#8220;fed&#8221; or &#8220;fd&#8221;.</li>
</ul>
<h2 id="Negation">Negation ( ^ )</h2>
<p>When used after the first character of the regular expression, the caret ( ^ ) is used for negation.</p>
<ul>
<li>The pattern f[^aeiou]d can be found in &#8220;fqd&#8221; and &#8220;f4d&#8221;, but not in &#8220;fad&#8221; or &#8220;fed&#8221;.</li>
</ul>
<h2 id="Subpatterns">Subpatterns ( () )</h2>
<p>Parentheses ( () ) are used to capture subpatterns.</p>
<ul>
<li>The pattern f(oo)?d can be found in &#8220;food&#8221; and &#8220;fd&#8221;, but not in &#8220;fod&#8221;.</li>
</ul>
<h2 id="h2.10">Alternatives ( | )</h2>
<p>The pipe ( | ) is used to create optional patterns.</p>
<ul>
<li>The pattern foo$|^bar can be found in &#8220;foo&#8221; and &#8220;bar&#8221;, but not &#8220;foobar&#8221;.</li>
</ul>
<h2 id="h2.11">Escape Character ( \ )</h2>
<p>The backslash ( \ ) is used to escape special characters.</p>
<ul>
<li>The pattern fo\.d can be found in &#8220;fo.d&#8221;, but not in &#8220;food&#8221; or &#8220;fo4d&#8221;.</li>
</ul>
<h1 id="h1.3">Backreferences</h1>
<p>Backreferences are special wildcards that refer back to a subpattern within a pattern. They can be used to make sure that two subpatterns match. The first subpattern in a pattern is referenced as \1, the second is referenced as \2, and so on.</p>
<p>For example, the pattern ([bmpw])o\1 matches â€œbobâ€, â€œmomâ€, â€œpopâ€, and â€œwowâ€, but not &#8220;bop&#8221; or &#8220;pow&#8221;.</p>
<p>A more practical example has to do matching the delimiter in social security numbers. Examine the following regular expression.</p>
<div>
<pre>^\d{3}([\- ]?)\d{2}([\- ]?)\d{4}$</pre>
</div>
<p>Within the caret (^) and dollar sign ($), which are used to specify the beginning and end of the pattern, there are three sequences of digits, optionally separated by a hyphen or a space. This pattern will be matched in all of following strings (and more).</p>
<ul>
<li>123-45-6789</li>
<li>123 45 6789</li>
<li>123456789</li>
<li>123-45 6789</li>
<li>123 45-6789</li>
<li>123-456789</li>
</ul>
<p>The last three strings are not ideal, but they do match the pattern. Backreferences can be used to make sure that the second delimiter matches the first delimiter. The regular expression would look like this.</p>
<div>
<pre>^\d{3}([\- ]?)\d{2}<em>\1</em>\d{4}$</pre>
</div>
<p>The \1 refers back to the first subpattern. Only the first three strings listed above match this regular expression.</p>
<h1 id="h1.4">Form Validation with Regular Expressions</h1>
<p>Regular expressions make it easy to create powerful form validation functions. Take a look at the following example.</p>
<div>
<h2>Code Sample: RegularExpressions/Demos/Login.html</h2>
<div>
<div>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Login&lt;/title&gt;
&lt;script type="text/javascript"&gt;
<strong> var RE_EMAIL = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/; var RE_PASSWORD = /^[A-Za-z\d]{6,8}$/;</strong>

function validate(form){
 var email = form.Email.value;
 var password = form.Password.value;
 var errors = [];<strong> if (!RE_EMAIL.test(email)) { errors[errors.length] = "You must enter a valid email address."; } if (!RE_PASSWORD.test(password)) { errors[errors.length] = "You must enter a valid password."; }</strong>

 if (errors.length &gt; 0) {
  reportErrors(errors);
  return false;
 }

 return true;
}

function reportErrors(errors){
 var msg = "There were some problems...\n";
 for (var i = 0; i&lt;errors.length; i++) {
  var numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
 }
 alert(msg);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Login Form&lt;/h1&gt;
&lt;form method="post" action="Process.html" onsubmit="return validate(this);"&gt;

 Email: &lt;input type="text" name="Email" size="25"&gt;&lt;br/&gt;
 Password: &lt;input type="password" name="Password" size="10"&gt;&lt;br/&gt;
 *Password must be between 6 and 10 characters and
 can only contain letters and digits.&lt;br/&gt;

 &lt;input type="submit" value="Submit"&gt;
 &lt;input type="reset" value="Reset Form"&gt;
 &lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
</div>
</div>
<div>Code Explanation</div>
<blockquote><p>This code starts by defining regular expressions for an email address and a password. Let&#8217;s break each one down.</p>
<div>
<pre>var RE_EMAIL = /^(\w+\.)*\w+@(\w+\.)+[A-Za-z]+$/;</pre>
</div>
<ol>
<li>The caret (^) says to start at the beginning. This prevents the user from entering invalid characters at the beginning of the email address.</li>
<li>(\w+[\-\.])* allows for a sequence of word characters followed by a dot or a dash. The * indicates that the pattern can be repeated zero or more times. Successful patterns include &#8220;ndunn.&#8221;, &#8220;ndunn-&#8221;, &#8220;nat.s.&#8221;, and &#8220;nat-s-&#8221;.</li>
<li>\w+ allows for one or more word characters.</li>
<li>@ allows for a single @ symbol.</li>
<li>(\w+\.)+ allows for a sequence of word characters followed by a dot. The + indicates that the pattern can be repeated one or more times. This is the domain name without the last portion (e.g, without the &#8220;com&#8221; or &#8220;gov&#8221;).</li>
<li>[A-Za-z]+ allows for one or more letters. This is the &#8220;com&#8221; or &#8220;gov&#8221; portion of the email address.</li>
<li>The dollar sign ($) says to end here. This prevents the user from entering invalid characters at the end of the email address.</li>
</ol>
<div>
<pre>var RE_PASSWORD = /^[A-Za-z\d]{6,8}$/;</pre>
</div>
<ol>
<li>The caret (^) says to start at the beginning. This prevents the user from entering invalid characters at the beginning of the password.</li>
<li>[A-Za-z\d]{6,8} allows for a six- to eight-character sequence of letters and digits.</li>
<li>The dollar sign ($) says to end here. This prevents the user from entering invalid characters at the end of the password.</li>
</ol>
</blockquote>
<br />Filed under: <a href='http://rsjeyakumar.wordpress.com/category/java-script/'>java script</a> Tagged: <a href='http://rsjeyakumar.wordpress.com/tag/form-validation-scripts/'>form validation scripts</a>, <a href='http://rsjeyakumar.wordpress.com/tag/literal-syntax/'>literal syntax</a>, <a href='http://rsjeyakumar.wordpress.com/tag/pattern-matching/'>pattern matching</a>, <a href='http://rsjeyakumar.wordpress.com/tag/regular-expression-pattern/'>regular expression pattern</a>, <a href='http://rsjeyakumar.wordpress.com/tag/social-security-number/'>social security number</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rsjeyakumar.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rsjeyakumar.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rsjeyakumar.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=345&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rsjeyakumar.wordpress.com/2011/12/21/regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f8f8425db9265a761a6efc327306688?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsjeyakumar</media:title>
		</media:content>
	</item>
		<item>
		<title>Ultimate client manager :New Plugin Procedure:</title>
		<link>http://rsjeyakumar.wordpress.com/2011/12/09/ultimate-client-manager-new-plugin-procedure/</link>
		<comments>http://rsjeyakumar.wordpress.com/2011/12/09/ultimate-client-manager-new-plugin-procedure/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 08:23:54 +0000</pubDate>
		<dc:creator>rsjeyakumar</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://rsjeyakumar.wordpress.com/?p=333</guid>
		<description><![CDATA[Ultimate client manager :New Plugin Procedure: 1.Folder name starts with plugin_yourpuginname 2.inside the folder mail php folder name should be yourplugin name 3.In the php file need to add this code 4.create database table as your requirements Filed under: PHP<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=333&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ultimate client manager :New Plugin Procedure:</p>
<p>1.Folder name starts with plugin_yourpuginname<br />
2.inside the folder mail php folder name should be yourplugin name<br />
3.In the php file need to add this code</p>
<p><pre class="brush: plain;">&lt;?php

class module_yourpluginname extends module_base{

var $links;
function init(){
$this-&gt;links = array();
$this-&gt;user_types = array();
$this-&gt;module_name = &quot;yourpluginname&quot;;
$this-&gt;module_position =This value should be integer(this is mention where your plugin should be display);
if(getcred()){
switch($_SESSION['_access_level']){
case &quot;administrator&quot;:
$this-&gt;links[] = array(&quot;name&quot;=&gt;&quot;yourplugindisplay_name&quot;,&quot;p&quot;=&gt;&quot;innerpage PHP file name&quot;,&quot;icon&quot;=&gt;&quot;icon.png&quot;);
break;
default:
//$this-&gt;links[] = array(&quot;name&quot;=&gt;&quot;yourplugindisplay_name&quot;,&quot;p&quot;=&gt;&quot;innerpage PHP file name&quot;,&quot;icon&quot;=&gt;&quot;icon.png&quot;);
break;
}
}

}?&gt;</pre></p>
<p>4.create database table as your requirements</p>
<br />Filed under: <a href='http://rsjeyakumar.wordpress.com/category/all/open-source-technology/php-open-source-technology/'>PHP</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rsjeyakumar.wordpress.com/333/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rsjeyakumar.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rsjeyakumar.wordpress.com/333/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rsjeyakumar.wordpress.com&amp;blog=9443514&amp;post=333&amp;subd=rsjeyakumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rsjeyakumar.wordpress.com/2011/12/09/ultimate-client-manager-new-plugin-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f8f8425db9265a761a6efc327306688?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsjeyakumar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
