<?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>code-diesel &#187; wordpress</title>
	<atom:link href="http://www.codediesel.com/tag/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>WordPress plugins to check for security threats</title>
		<link>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/</link>
		<comments>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:47:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2952</guid>
		<description><![CDATA[With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in [...]]]></description>
			<content:encoded><![CDATA[<p>With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in your site and help you monitor for any security breaches.<br />
<span id="more-2952"></span><br />
<strong>BulletProof Security</strong><br />
<a href="http://wordpress.org/extend/plugins/bulletproof-security/" title="BulletProof Security" target="_blank">BulletProof Security</a> protects your website from XSS, RFI, CSRF, Base64, Code Injection and SQL Injection hacking attempts. The plugin also Protects wp-config.php, bb-config.php, php.ini, php5.ini, install.php and readme.html with .htaccess security protection. BPS has built-in Backup and Restore, a File Editor, a File Uploader and a File Downloader to make managing the BPS master .htaccess files and your website security fast and simple.</p>
<p><strong>WordPress File Monitor</strong><br />
<a href="http://wordpress.org/extend/plugins/wordpress-file-monitor/" title="WordPress File Monitor" target="_blank">WFM</a> monitors your WordPress installation for any added, deleted or changed files. When a change is detected an email is sent to a specified address. Currently does not support multi-site installations.</p>
<p><strong>Ultimate Security Checker</strong><br />
<a href="http://wordpress.org/extend/plugins/ultimate-security-checker/" title="Ultimate Security Checker" target="_blank">Ultimate Security Checker</a> helps you identify security problems with your WordPress installation. It scans your WordPress blog for hundreds of known threats, then gives you a security &#8220;grade&#8221; based on how well you have protected yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmarking WordPress SQL using FirePHP</title>
		<link>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/</link>
		<comments>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 09:10:16 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/benchmarking-wordpress-sql-using-firephp/</guid>
		<description><![CDATA[Benchmarking your Wordpress SQL]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp_thumb.gif" alt="firephp_thumb" title="firephp_thumb" width="331" height="185" class="alignleft size-full wp-image-2478" />Building and running a WordPress site is a simple matter. WordPress is a considerably fast CMS system, until you start to add more and more plugins and one day you notice that Worpdress has started to slow down. It may be the case that SQL queries within some plugins are not optimized and are taking an increased amount of time executing them, this can considerably slow down your site. The first thing you can do to rectify the situation is to find out where exactly the bottleneck resides by analyzing the time each SQL query takes to executes. Some inquisitive people among you may also be interested in knowing in what sequence the WordPress SQL queries themselves are being run. Not that all bottlenecks occur due to unoptimized SQL, most are due to poor coding practices. Whatever the reason; the following post will show you how to look inside the SQL query execution of WordPress.<br />
<span id="more-2443"></span></p>
<h4>Logging WordPress SQL queries</h4>
<p>By default WordPress doesn&#8217;t log any queries. But you can instruct WordPress to do so  by adding the following configuration option to your WordPress &#8216;wpconfig.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p24431"><td class="code" id="p2443code1"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* wp-config.php */</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once that is done you can add the following lines at the end of your WordPress themes &#8216;footer.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p24432"><td class="code" id="p2443code2"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* footer.php */</span>
<span style="color: #339933;">..</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>This will display the SQL statements executed by your WordPress installation in a raw format as shown below. It displays a total of three items &#8211; the executed query, the execution time of the query and the function that called the query.</p>

<div class="wp_codebox"><table><tr id="p24433"><td class="code" id="p2443code3"><pre class="text" style="font-family:monospace;">Array
(
    [0] =&gt; Array
        (
            [0] =&gt; SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'
            [1] =&gt; 0.004619836807251
            [2] =&gt; require, require_once, require_once, require_once, is_blog_installed, wp_load_alloptions
        )
&nbsp;
    [1] =&gt; Array
        (
            [0] =&gt; SELECT option_value FROM wp_options WHERE option_name = 'sidebars_widgets' LIMIT 1
            [1] =&gt; 0.0015749931335449
            [2] =&gt; require, require_once, require_once, require_once, do_action, call_user_func_array, wp_widgets_init, register_widget, WP_Widget_Factory-&gt;register, WP_Widget_Recent_Comments-&gt;WP_Widget_Recent_Comments, is_active_widget, wp_get_sidebars_widgets, get_option
        )
.
.</pre></td></tr></table></div>

<p>As you can see the raw information is not interesting to look at and it also clutters the actual site. A better way is to use FireBug and FirePHP to log the SQL queries without interfering with the WordPress output.</p>
<h4>Installing FirePHP</h4>
<p>Before starting you need to have <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/6149/">FirePHP</a> and <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/1843">FireBug</a> installed. FirePHP uses FireBugs console window to output the debug messages.  Once you have installed FirePHP and FireBug, you need to install the FirePHP <a target="_blank" href="http://www.firephp.org/HQ/Install.htm">server libraries</a>. Copy the FirePHP server libraries to your WordPress themes directory. For e.g &#8216;wordpress\wp-content\themes\default\FirePHPCore&#8217;.</p>
<p>If you are unfamiliar with FirePHP a nice tutorial can be found at <a target="_blank" href="http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/">Six Revisions</a>.</p>
<h4>Displaying formatted information with FirePHP</h4>
<p>Once we are done with the above setup, you can add the following code at the end of your WordPress themes &#8216;footer.php&#8217; file, replacing the code we previously added.</p>

<div class="wp_codebox"><table><tr id="p24434"><td class="code" id="p2443code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* footer.php */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FirePHPCore/FirePHP.class.PHP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SQL&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Execution Time&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Calling Function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$expensive_query_time</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$expensive_query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$total_time</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009933; font-style: italic;">/* Get the most expensive query */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$expensive_query_time</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$expensive_query_time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$expensive_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$total_time</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$total_queries</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span> <span style="color: #339933;">=</span> FirePHP<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Display the queries in a formatted table */</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Query'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collapsed'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>  
                                    <span style="color: #0000ff;">'Color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#000'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_queries</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; queries took &quot;</span> <span style="color: #339933;">.</span> 
                    <span style="color: #000088;">$total_time</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; seconds.&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Display the query summary */</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Query Summary'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collapsed'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>  
                                            <span style="color: #0000ff;">'Color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#000'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_queries</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Total Queries'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expensive_query</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Expensive Query'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expensive_query_time</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Expensive Query Time'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Refresh your WordPress page and you should see the following output in the FireBug console. Click on the image below to view the complete debug screen. (Some queries have been removed for size.) As you can see FirePHP displays a nicely formatted output than the previous one.</p>
<p><a href="http://www.codediesel.com/data/images/firephp_large.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp.gif" alt="firephp" title="firephp" width="563" height="247" class="aligncenter size-full wp-image-2455" /></a></p>
<h4>Modifying the query data returned by WordPress</h4>
<p>WordPress by default saves and returns the following three parameters for each SQL query:<br />
1. SQL query<br />
2. Query Execution time<br />
3. The method chain that called the query.</p>
<p>The code section where this happens is located in the query() method of the &#8216;wp-db.php&#8217; file in the WordPress &#8216;wp-includes&#8217; directory. The following snippet shows that section of code:</p>

<div class="wp_codebox"><table><tr id="p24435"><td class="code" id="p2443code5"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* wp-db.php */</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #009933; font-style: italic;">// Log how the function was called</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">func_call</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>db-&gt;query(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$query</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Keep track of the last query for debug..</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Perform the query via std mysql_query function..</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> SAVEQUERIES <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_queries</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> SAVEQUERIES <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer_stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                              <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_caller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>You can modify the code to add some extra parameters as per your requirements, or modify the format in which it is returned. For example you could add code to save the data to a xml or csv file for latter retrieval and analysis.</p>
<p><strong>Note:</strong> Turning on the <em>define(&#8216;SAVEQUERIES&#8217;, true)</em> option can slow down your WordPress site, so make sure you disable it after your work is done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Filter your wordpress content by location</title>
		<link>http://www.codediesel.com/php/wordpress-country-filter-plugin/</link>
		<comments>http://www.codediesel.com/php/wordpress-country-filter-plugin/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 06:33:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1195</guid>
		<description><![CDATA[filter your wordpress content by country]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve released my first WordPress plugin, Country Filter. The <em>Country Filter</em> plugin allows you to control the visibility of WordPress elements depending on the users country of origin. You can hide or show certain elements of a page or some sections of a post from users browsing from a particular country. For example you could have a ‘donate’ button that displays only if the user is browsing from France, India or UK. </p>
<p>Plugin download and installation instructions can be found <a href="http://www.codediesel.com/plugins/country-filter/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/wordpress-country-filter-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing WordPress plugins remotely</title>
		<link>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/</link>
		<comments>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 04:23:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1087</guid>
		<description><![CDATA[remotely access your wordpress plugin data]]></description>
			<content:encoded><![CDATA[<p>With a plethora of wordpress plugins available for every conceivable purpose, there is a huge amount of data that is stored on a typical wordpress installation. We could easily use that data across domains or display them using widgets on the desktop. Take for example the <a target="_blank" href="http://lesterchan.net/portfolio/programming/php/">WP-UserOnline</a> plugin that displays how many users are currently online on your blog. We could easily write a proxy to grab that information from the plugin and use it in a Yahoo Widget or in a AIR application on the desktop, so you can see the number of users online without having to visit your site. Or you could use the information on some other site.<br />
<span id="more-1087"></span><br />
<strong>Example proxy</strong><br />
A sample proxy code to access the <em>WP-UserOnline</em> plugin data is shown below. The code basically uses the <em>WP-UserOnline</em> plugins &#8216;<em>get_users_browsing_site()</em>&#8216; function to read the data and echo it as a JSON string. Now all you have to do is request the below file (useronline_proxy.php) from a remote server and you get a JSON string containing the useronline information.</p>

<div class="wp_codebox"><table><tr id="p10876"><td class="code" id="p1087code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
  useronline_proxy.php
&nbsp;
  On my server this file is located in the
  www.codediesel.com/remote/ directory, hence the
  location of the $wp_root variable. You will need
  to change it depending on where you save this file.
*/</span>
<span style="color: #000088;">$wp_root</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'..'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp_root</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/wp-load.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp_root</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/wp-load.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp_root</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/wp-config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  The following file contains all the functions 
    we require to get to the wp-useronline data. 
*/</span> 
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp_root</span><span style="color: #339933;">.</span>
             <span style="color: #0000ff;">'/wp-content/plugins/wp-useronline/wp-useronline.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Get the number of users browsing the site right now */</span>
<span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> get_users_browsing_site<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;member&quot;</span>  <span style="color: #339933;">=&gt;</span>  <span style="color: #000088;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">&quot;guest&quot;</span>   <span style="color: #339933;">=&gt;</span>  <span style="color: #000088;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">&quot;bot&quot;</span>     <span style="color: #339933;">=&gt;</span>  <span style="color: #000088;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">&quot;total&quot;</span>   <span style="color: #339933;">=&gt;</span>  <span style="color: #990000;">array_sum</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Encodes the array as a JSON string and print it.
  (Only available since PHP 5.2)
*/</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>How the whole thing works is shown below:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/06/drawing22.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/06/drawing22.gif" alt="wordpress remote" title="wordpress remote" width="458" height="267" class="aligncenter size-full wp-image-1129" /></a></p>
<p><strong>Accessing the proxy remotely</strong><br />
Assuming allow-url-fopen is true, you can now access the <em>useronline_proxy.php</em> file from another server as below. Note that you will have to use cURL if remote url fopen is not allowed on your php installation.</p>

<div class="wp_codebox"><table><tr id="p10877"><td class="code" id="p1087code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.domain.com/remote/useronline_proxy.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$json_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$json_string</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$json_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Using Javascript to access the proxy</strong><br />
You can also use javascript to access the remote proxy. But since cross domain access is restricted by the browser, we will need to resort to jQuery&#8217;s  <em>getJSON</em> function to do the same. The javascript code is shown below.</p>

<div class="wp_codebox"><table><tr id="p10878"><td class="code" id="p1087code8"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE HTML <span style="color: #003366; font-weight: bold;">PUBLIC</span> <span style="color: #3366CC;">&quot;-//W3C//DTD HTML 4.01//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;en&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery-1.3.2.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
$proxy <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://www.domain.com/remote/useronline_proxy.php?callback=?&quot;</span><span style="color: #339933;">;</span>
    $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>$proxy<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        $info <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Members: '</span> <span style="color: #339933;">+</span> data.<span style="color: #660066;">member</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #339933;">+</span>
                <span style="color: #3366CC;">'Guests: '</span> <span style="color: #339933;">+</span> data.<span style="color: #660066;">guest</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #339933;">+</span>
                <span style="color: #3366CC;">'Bots: '</span> <span style="color: #339933;">+</span> data.<span style="color: #660066;">bot</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #339933;">+</span>
                <span style="color: #3366CC;">'Total: '</span> <span style="color: #339933;">+</span> data.<span style="color: #660066;">total</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$info<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>But to make the above javascript code work we will have to make a little change to the <em>useronline_proxy.php</em> file.</p>

<div class="wp_codebox"><table><tr id="p10879"><td class="code" id="p1087code9"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/* Replace the 'echo json_encode($temp);' line with the following */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'('</span> <span style="color: #339933;">.</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">');'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Security</strong><br />
To keep the proxy code simple, I&#8217;ve not added any authentication to the same. But you could pass a username/password as a GET parameter and check it in <em>useronline_proxy.php</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

