<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>InTechgrity</title> <link>http://www.intechgrity.com</link> <description>Amalgamating Life &amp; Technology</description> <lastBuildDate>Fri, 25 May 2012 04:37:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/greentechspot" /><feedburner:info uri="greentechspot" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-nc-nd/2.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:emailServiceId>greentechspot</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><title>Automatically copy images (png, jpeg, gif) from remote server (http) to your local server using PHP</title><link>http://feedproxy.google.com/~r/greentechspot/~3/k2eKHdyKJUI/</link> <comments>http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/#comments</comments> <pubDate>Thu, 24 May 2012 17:12:03 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[php]]></category> <category><![CDATA[php-gd]]></category> <category><![CDATA[php-image]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=808</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/">Automatically copy images (png, jpeg, gif) from remote server (http) to your local server using PHP</a>">here</a></p><p>So, have you ever found the necessity to copy remote images to your local server using http URLs? This is pretty easy using PHP and PHP GD. If you have both of them installed (in most of the servers they come installed) then you can do that in no time. But then comes the security [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> No related posts.]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/S3XEfqajgiFnD4ZE0OEAlTm6pNg/0/da"><img src="http://feedads.g.doubleclick.net/~a/S3XEfqajgiFnD4ZE0OEAlTm6pNg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/S3XEfqajgiFnD4ZE0OEAlTm6pNg/1/da"><img src="http://feedads.g.doubleclick.net/~a/S3XEfqajgiFnD4ZE0OEAlTm6pNg/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/">Automatically copy images (png, jpeg, gif) from remote server (http) to your local server using PHP</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/copy-image-php-remote-local.png"><img
class="alignright  wp-image-1186" title="copy-image-php-remote-local" src="http://www.intechgrity.com/wp-content/uploads/2012/05/copy-image-php-remote-local.png" alt="" width="235" height="235" /></a>So, have you ever found the necessity to copy remote images to your local server using http URLs? This is pretty easy using PHP and PHP GD. If you have both of them installed (in most of the servers they come installed) then you can do that in no time. But then comes the security issue. Copying anything from remote server to your local server can expose your server to many vulnerabilities. Just like, recently our site got hacked due to TimThumb vulnerability. So, we decided to put together a working code which you can use on your projects to copy any valid images from remote to local server. The copying process is pretty much automatic given our code. We have wrapped them in a function and it is available for you to download as well. We shall now discuss the working of the method. But before that:</p><div
class="demo-download"><a
class="demo" href="http://demo.intechgrity.com/fetch-image/" target="_blank">Demo</a><a
class="download" title="Copy Remote Images using PHP Downloaded 1" href="http://www.intechgrity.com/downloads/14">Download Now</a></div><h2>#1: Understanding the concept:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/concept-function.png"><img
class="aligncenter size-full wp-image-1190" title="concept-function" src="http://www.intechgrity.com/wp-content/uploads/2012/05/concept-function.png" alt="" width="560" height="150" /></a>While creating such automated script, we should think of a few things. Or in other words, while creating the function, we should think of the parameters first.</p><ul><li><strong>$img_url</strong>: The URL of the image. We should accept URLs starting with http:// or https:// and ending with .png, .jpg, .jpeg or .gif. Others should be rejected.</li><li><strong>$store_dir</strong>: The directory where we should store the images.</li><li><strong>$store_dir_type</strong>: Should specify whether the directory path is relative to the script or is absolute.</li><li><strong>$overwrite</strong>: In case of duplicate file name, should it overwrite?</li><li><strong>$pref</strong>: Internally used for dynamic renaming of duplicate files using tail recursion  (a function, directly returns the same function call with some different arguments).</li><li><strong>$debug</strong>: A little bit of debug information is always fun, and well, informative.</li></ul><p>So, keeping these things, we write in the skeleton of our function.</p><pre class="brush: php; title: ; notranslate">
/**
 * Fetch JPEG or PNG or GIF Image
 *
 * A custom function in PHP which lets you fetch jpeg or png images from remote server to your local server
 * Can also prevent duplicate by appending an increasing _xxxx to the filename. You can also overwrite it.
 *
 * Also gives a debug mode to check where the problem is, if this is not working for you.
 *
 * @author Swashata &lt;swashata ~[at]~ intechgrity ~[dot]~ com&gt;
 * @copyright Do what ever you wish - I like GPL <img src='http://www.intechgrity.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (&amp; love tux <img src='http://www.intechgrity.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )
 * @link http://www.intechgrity.com/?p=808
 *
 * @param string $img_url The URL of the image. Should start with http or https followed by :// and end with .png or .jpeg or .jpg or .gif. Else it will not pass the validation
 * @param string $store_dir The directory where you would like to store the images.
 * @param string $store_dir_type The path type of the directory. 'relative' for the location in relation with the executing script location or 'absolute'
 * @param bool $overwrite Set to true to overwrite, false to create a new image with different name
 * @param bool|int $pref internally used to prefix the extension in case of duplicate file name. Used by the trailing recursion call
 * @param bool $debug Set to true for enable debugging and print some useful messages.
 * @return string the location of the image (either relative with the current script or abosule depending on $store_dir_type)
 */
function itg_fetch_image($img_url, $store_dir = 'image', $store_dir_type = 'relative', $overwrite = false, $pref = false, $debug = false) {
    //code
}
</pre><h2>#2: Verifying the image URL:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/validate-image-security.png"><img
class="aligncenter size-full wp-image-1189" title="validate-image-security" src="http://www.intechgrity.com/wp-content/uploads/2012/05/validate-image-security.png" alt="" width="560" height="150" /></a>This should be done before anything else. What we do is:</p><ul><li>Check if the URL begins with <span
class="spanlight">http://</span> or <span
class="spanlight">https://</span>. Reject otherwise.</li><li>Check if the URL ends with a valid extension (<span
class="spanlight">.gif</span>, <span
class="spanlight">.jpeg</span>, <span
class="spanlight">.jpg</span>, <span
class="spanlight">.png</span>). Reject otherwise.</li></ul><p>We shall use PHP&#8217;s built in <span
class="code">preg_match</span> to search using our regular expression. The code snippet for validating the image URL is this:</p><pre class="brush: php; title: ; notranslate">
    if(preg_match('/https?:\/\/.*\.png$/i', $img_url)) {
        $img_type = 'png';
    }
    else if(preg_match('/https?:\/\/.*\.(jpg|jpeg)$/i', $img_url)) {
        $img_type = 'jpg';
    }
    else if(preg_match('/https?:\/\/.*\.gif$/i', $img_url)) {
        $img_type = 'gif';
    }
    else {
        if(true == $debug)
            echo 'Invalid image URL';
        return ''; //possible error on the image URL
    }
</pre><h2>#3: Handling duplicate files:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/handle-duplicate-files.png"><img
class="aligncenter size-full wp-image-1191" title="handle-duplicate-files" src="http://www.intechgrity.com/wp-content/uploads/2012/05/handle-duplicate-files.png" alt="" width="560" height="150" /></a>As mentioned before, we should handle duplicate files properly. The strategy is:</p><ul><li>Somehow use smart renaming by appending _xxx (xxx is a counter) after the image name and before the extension. We shall increase the counter unless a valid non-existing filename is found.</li><li>Or, delete the old file altogether if overwriting is set to true.</li></ul><p>And here is the code snippet which does the trick:</p><pre class="brush: php; title: ; notranslate">
    //first get the base name of the image
    $i_name = explode('.', basename($img_url));
    $i_name = $i_name[0];

    $dir_name = (($store_dir_type == 'relative')? './' : '') . rtrim($store_dir, '/') . '/';

    //create the directory if not present
    if(!file_exists($dir_name))
        mkdir($dir_name, 0777, true);

    //calculate the destination image path
    $i_dest = $dir_name . $i_name . (($pref === false)? '' : '_' . $pref) . '.' . $img_type;

    //lets see if the path exists already
    if(file_exists($i_dest)) {
        $pref = (int) $pref;

        //modify the file name, do not overwrite
        if(false == $overwrite)
            return itg_fetch_image($img_url, $store_dir, $store_dir_type, $overwrite, ++$pref, $debug);
        //delete &amp; overwrite
        else
            unlink ($i_dest);
    }
</pre><p>Note that it intelligently uses tail recursion for smart renaming. It simply returns the same function by incrementing the value of <span
class="code">$pref</span> variable by 1.</p><h2>#4: Fetching the image from the server:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/fetch-from-remote-server.png"><img
class="aligncenter size-full wp-image-1187" title="fetch-from-remote-server" src="http://www.intechgrity.com/wp-content/uploads/2012/05/fetch-from-remote-server.png" alt="" width="560" height="150" /></a>Now comes the real thing. We will now see how we can fetch the image from the remove server using some built in PHP GD functions. Note that for all these to work, you will need fopen wrapper enabled, which is enabled in most of the servers. If not, then you can edit your <span
class="spanlight">php.ini</span> file and set it to true. <a
href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen" target="_blank">Check here</a> for more information. So, here is the code snippet which does the magic:</p><pre class="brush: php; title: ; notranslate">
    //first check if the image is fetchable
    $img_info = @getimagesize($img_url);

    //is it a valid image?
    if(false == $img_info || !isset($img_info[2]) || !($img_info[2] == IMAGETYPE_JPEG || $img_info[2] == IMAGETYPE_PNG || $img_info[2] == IMAGETYPE_JPEG2000 || $img_info[2] == IMAGETYPE_GIF)) {
        if(true == $debug)
            echo 'The image doesn\'t seem to exist in the remote server';
        return ''; //return empty string
    }

    //now try to create the image
    if($img_type == 'jpg') {
        $m_img = @imagecreatefromjpeg($img_url);
    } else if($img_type == 'png') {
        $m_img = @imagecreatefrompng($img_url);
        @imagealphablending($m_img, false);
        @imagesavealpha($m_img, true);
    } else if($img_type == 'gif') {
        $m_img = @imagecreatefromgif($img_url);
    } else {
        $m_img = FALSE;
    }

    //was the attempt successful?
    if(FALSE === $m_img) {
        if(true == $debug)
            echo 'Can not create image from the URL';
        return '';
    }
</pre><h2>#5: Saving the image locally on disk:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/save-image-local-disk.png"><img
class="aligncenter size-full wp-image-1188" title="save-image-local-disk" src="http://www.intechgrity.com/wp-content/uploads/2012/05/save-image-local-disk.png" alt="" width="560" height="150" /></a>Now finally we would save the image locally on our disk. The snippet is:</p><pre class="brush: php; title: ; notranslate">
    //now attempt to save the file on local server
    if($img_type == 'jpg') {
        if(imagejpeg($m_img, $i_dest, 100))
            return $i_dest;
        else
            return '';
    } else if($img_type == 'png') {
        if(imagepng($m_img, $i_dest, 0))
            return $i_dest;
        else
            return '';
    } else if($img_type == 'gif') {
        if(imagegif($m_img, $i_dest))
            return $i_dest;
        else
            return '';
    }
</pre><p>Note that you can edit the third variable of <span
class="code">imagejpeg</span> and <span
class="code">imagepng</span> to set the quality. More information can be found <a
href="http://in2.php.net/manual/en/function.imagejpeg.php" target="_blank">here</a> or <a
href="http://in2.php.net/manual/en/function.imagepng.php" target="_blank">here</a>.</p><h2>#6: The complete code:</h2><p>So putting together everything, here is the complete code:</p><pre class="brush: php; collapse: true; light: false; title: ; toolbar: true; notranslate">
/**
 * Fetch JPEG or PNG or GIF Image
 *
 * A custom function in PHP which lets you fetch jpeg or png images from remote server to your local server
 * Can also prevent duplicate by appending an increasing _xxxx to the filename. You can also overwrite it.
 *
 * Also gives a debug mode to check where the problem is, if this is not working for you.
 *
 * @author Swashata &lt;swashata ~[at]~ intechgrity ~[dot]~ com&gt;
 * @copyright Do what ever you wish - I like GPL <img src='http://www.intechgrity.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (&amp; love tux <img src='http://www.intechgrity.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )
 * @link http://www.intechgrity.com/?p=808
 *
 * @param string $img_url The URL of the image. Should start with http or https followed by :// and end with .png or .jpeg or .jpg or .gif. Else it will not pass the validation
 * @param string $store_dir The directory where you would like to store the images.
 * @param string $store_dir_type The path type of the directory. 'relative' for the location in relation with the executing script location or 'absolute'
 * @param bool $overwrite Set to true to overwrite, false to create a new image with different name
 * @param bool|int $pref internally used to prefix the extension in case of duplicate file name. Used by the trailing recursion call
 * @param bool $debug Set to true for enable debugging and print some useful messages.
 * @return string the location of the image (either relative with the current script or abosule depending on $store_dir_type)
 */
function itg_fetch_image($img_url, $store_dir = 'image', $store_dir_type = 'relative', $overwrite = false, $pref = false, $debug = false) {
    //first get the base name of the image
    $i_name = explode('.', basename($img_url));
    $i_name = $i_name[0];

    //now try to guess the image type from the given url
    //it should end with a valid extension...
    //good for security too
    if(preg_match('/https?:\/\/.*\.png$/i', $img_url)) {
        $img_type = 'png';
    }
    else if(preg_match('/https?:\/\/.*\.(jpg|jpeg)$/i', $img_url)) {
        $img_type = 'jpg';
    }
    else if(preg_match('/https?:\/\/.*\.gif$/i', $img_url)) {
        $img_type = 'gif';
    }
    else {
        if(true == $debug)
            echo 'Invalid image URL';
        return ''; //possible error on the image URL
    }

    $dir_name = (($store_dir_type == 'relative')? './' : '') . rtrim($store_dir, '/') . '/';

    //create the directory if not present
    if(!file_exists($dir_name))
        mkdir($dir_name, 0777, true);

    //calculate the destination image path
    $i_dest = $dir_name . $i_name . (($pref === false)? '' : '_' . $pref) . '.' . $img_type;

    //lets see if the path exists already
    if(file_exists($i_dest)) {
        $pref = (int) $pref;

        //modify the file name, do not overwrite
        if(false == $overwrite)
            return itg_fetch_image($img_url, $store_dir, $store_dir_type, $overwrite, ++$pref, $debug);
        //delete &amp; overwrite
        else
            unlink ($i_dest);
    }

    //first check if the image is fetchable
    $img_info = @getimagesize($img_url);

    //is it a valid image?
    if(false == $img_info || !isset($img_info[2]) || !($img_info[2] == IMAGETYPE_JPEG || $img_info[2] == IMAGETYPE_PNG || $img_info[2] == IMAGETYPE_JPEG2000 || $img_info[2] == IMAGETYPE_GIF)) {
        if(true == $debug)
            echo 'The image doesn\'t seem to exist in the remote server';
        return ''; //return empty string
    }

    //now try to create the image
    if($img_type == 'jpg') {
        $m_img = @imagecreatefromjpeg($img_url);
    } else if($img_type == 'png') {
        $m_img = @imagecreatefrompng($img_url);
        @imagealphablending($m_img, false);
        @imagesavealpha($m_img, true);
    } else if($img_type == 'gif') {
        $m_img = @imagecreatefromgif($img_url);
    } else {
        $m_img = FALSE;
    }

    //was the attempt successful?
    if(FALSE === $m_img) {
        if(true == $debug)
            echo 'Can not create image from the URL';
        return '';
    }

    //now attempt to save the file on local server
    if($img_type == 'jpg') {
        if(imagejpeg($m_img, $i_dest, 100))
            return $i_dest;
        else
            return '';
    } else if($img_type == 'png') {
        if(imagepng($m_img, $i_dest, 0))
            return $i_dest;
        else
            return '';
    } else if($img_type == 'gif') {
        if(imagegif($m_img, $i_dest))
            return $i_dest;
        else
            return '';
    }

    return '';
}

//a quick test? just uncomment the line below
//echo itg_fetch_image('http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png');
</pre><p>You can call it as mentioned there in the comments. Play with the parameters and see what you can do. You can integrate it with PHP POST and GET operations on your live project. Do check our tutorial on <a
title="Using PHP GET and POST simultaneously via single HTML form" href="http://www.intechgrity.com/using-php-get-and-post-simultaneously-via-single-html-form/">PHP POST and GET</a> for a better understanding. Also, the function returns the path of the stored image. You can change this behaviour if you like by simply altering the return statement. The usage is completely upto you.</p><div
class="demo-download"><a
class="demo" href="http://demo.intechgrity.com/fetch-image/" target="_blank">Demo</a><a
class="download" title="Copy Remote Images using PHP Downloaded 1" href="http://www.intechgrity.com/downloads/14">Download Now</a></div><p>So, that was all. If you have any question or need any help with it, then do drop in a comment. I will try my best to give you the support you want.</p><p>No related posts.</p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=k2eKHdyKJUI:opysFArVepc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=k2eKHdyKJUI:opysFArVepc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=k2eKHdyKJUI:opysFArVepc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=k2eKHdyKJUI:opysFArVepc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=k2eKHdyKJUI:opysFArVepc:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=k2eKHdyKJUI:opysFArVepc:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/k2eKHdyKJUI" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/</feedburner:origLink></item> <item><title>Add home &amp; other items directly in WP Nav Menu using wp_nav_menu_items filters</title><link>http://feedproxy.google.com/~r/greentechspot/~3/UTiZcJBml9c/</link> <comments>http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/#comments</comments> <pubDate>Sun, 20 May 2012 07:53:15 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Wordpress APIs]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress api]]></category> <category><![CDATA[wordpress hacks]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1162</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/">Add home &#038; other items directly in WP Nav Menu using wp_nav_menu_items filters</a>">here</a></p><p>So, this is going to be the last of the WordPress Nav Menu series. Previously, we have talked about the basics of WordPress custom navigation menu and dealing the empty nav menus with custom fallbacks. In this post, we shall see how we can directly add some links to the nav menu using filters. This [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/' rel='bookmark' title='Programmer&#8217;s guide for WordPress Nav menu'>Programmer&#8217;s guide for WordPress Nav menu</a></li><li><a
href='http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/' rel='bookmark' title='The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &amp; pages'>The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a></li><li><a
href='http://www.intechgrity.com/jquery-powered-sidebar-navigation-menu/' rel='bookmark' title='jQuery Vertical Sidebar Navigation menu for your Blog or Website – Free package by iTg'>jQuery Vertical Sidebar Navigation menu for your Blog or Website – Free package by iTg</a></li><li><a
href='http://www.intechgrity.com/download-web-videos-directly-from-your/' rel='bookmark' title='Download Web Videos directly from your Firefox using Video DownloadHelper'>Download Web Videos directly from your Firefox using Video DownloadHelper</a></li><li><a
href='http://www.intechgrity.com/add-dynamic-login-logout-n-site-admin-button-to-wordpress-using-wp-api/' rel='bookmark' title='Add dynamic Login Logout n Site Admin button to WordPress using WP API'>Add dynamic Login Logout n Site Admin button to WordPress using WP API</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/y8h7lUaEqi35FLha5X_DAtYbgHk/0/da"><img src="http://feedads.g.doubleclick.net/~a/y8h7lUaEqi35FLha5X_DAtYbgHk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/y8h7lUaEqi35FLha5X_DAtYbgHk/1/da"><img src="http://feedads.g.doubleclick.net/~a/y8h7lUaEqi35FLha5X_DAtYbgHk/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/">Add home &#038; other items directly in WP Nav Menu using wp_nav_menu_items filters</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/custom-items-to-wp_nav_menu.png"><img
class="alignright size-thumbnail wp-image-1175" title="custom-items-to-wp_nav_menu" src="http://www.intechgrity.com/wp-content/uploads/2012/05/custom-items-to-wp_nav_menu-150x150.png" alt="" width="150" height="150" /></a>So, this is going to be the last of the WordPress Nav Menu series. Previously, we have talked about the <a
title="Programmer’s guide for WordPress Nav menu" href="http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/">basics of WordPress custom navigation menu</a> and <a
title="The right way of dealing wp_nav_menu fallback function – List custom categories &amp; pages" href="http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/">dealing the empty nav menus with custom fallbacks</a>. In this post, we shall see how we can directly add some links to the nav menu using filters. This would become useful, if you want to customize the user&#8217;s own navigation menus. We shall see, how we can add the home button and also the dynamic login/logout button through user defined custom navigation menus using WordPress Filters.</p><h2>#1: Understanding the concept:</h2><p>The filter we will be using is <span
class="code">wp_nav_menu_items</span>. It lets the function get two arguments. One is <span
class="code">$items</span>, which is the HTML of all the items in the navigation. Other is the <span
class="code">$args</span>, which holds the information of the nav itself. We shall use the $args variable to identify our target navigation menu and then will add our links.</p><h2>#2: Adding the Navigation menu:</h2><p>First we hook our two navigation menus, <span
class="spanlight">primary</span> &amp; <span
class="spanlight">footer</span> as follows:</p><pre class="brush: php; title: ; notranslate">
/**
 * Register our nav menus
 * @see http://codex.wordpress.org/Function_Reference/register_nav_menus
 */
function my_theme_nav_bars() {
    register_nav_menus(array(
        'primary' =&gt; 'Primary Menu',
        'footer' =&gt; 'Footer Menu',
    ));
}
add_action('init', 'my_theme_nav_bars');
</pre><h2>#3: Add filters to customize:</h2><p>Now, we customize the navigation menus using filters like this:</p><pre class="brush: php; title: ; notranslate">
/**
 * Filter the navigation menus and add customized links
 *
 * We are usign wp_nav_menu_items filter to do this.
 * Also, $args is used to identify the location of the nav menu
 * and change it accordingly.
 *
 * @param string $items The HTML of the generated items
 * @param array $args The information of the navigation
 * @return string
 */
function filter_my_theme_nav_bars($items, $args) {
    if($args-&gt;theme_location == 'primary') {
        $homelink = '&lt;li class=&quot;home' . ((is_home() || is_front_page())? ' current_page_item' : '') . '&quot;&gt;&lt;a href=&quot;' . get_bloginfo('url') . '&quot;&gt;' . __('Home') . '&lt;/a&gt;&lt;/li&gt;';
        $items = $homelink . $items;
    }

    if($args-&gt;theme_location == 'footer') {
        $dlink = '&lt;li class=&quot;login&quot;&gt;' . wp_loginout('', false) . '&lt;/li&gt;' . wp_register('&lt;li class=&quot;admin&quot;&gt;', '&lt;/li&gt;', false);
        $items .= $dlink;
    }

    return $items;
}
add_filter('wp_nav_menu_items', 'filter_my_theme_nav_bars', 10, 2);
</pre><p>The code is quite self explanatory. It adds a nice Home button to the primary nav and nice login/logout button to the footer navigation. We use the <span
class="code">wp_nav_menu_items</span> to hook to the filter. The argument 10 represents the priority, which is the default and 2 represent the number of arguments which should be two (<span
class="code">$items</span> &amp; <span
class="code">$args</span>).</p><p>Now, for the sake of completeness, let us see how we can do even more.</p><h3>#3.1: Add a search bar to the navigation:</h3><pre class="brush: php; title: ; notranslate">
function add_search_through_filter($items, $args) {
    if($args-&gt;theme_location == 'primary') {
        $searchlink = '&lt;li class=&quot;search&quot;&gt;' . get_search_form(false) . '&lt;/li&gt;';
        $items .= $searchlink;
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'add_search_through_filter', 10, 2);
</pre><h3>#3.2: Add feed link to the navigation:</h3><pre class="brush: php; title: ; notranslate">
function add_rss_link_through_filter($items, $args) {
    if($args-&gt;theme_location == 'primary') {
        $rsslink = '&lt;li class=&quot;rss&quot;&gt;&lt;a href=&quot;' . get_bloginfo('rss2_url') . '&quot;&gt;RSS&lt;/a&gt;&lt;/li&gt;';
        $items .= $rsslink;
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'add_rss_link_through_filter', 10, 2);
</pre><h3>#3.3: Show some text:</h3><pre class="brush: php; title: ; notranslate">
function add_custom_text_through_filter($items, $args) {
    if($args-&gt;theme_location == 'footer') {
        $clink = '&lt;li class=&quot;copyright&quot;&gt;&amp;copy; Copyright ' . get_bloginfo('name') . ' ~ 2010-' . date('Y') . '&lt;/li&gt;';
        $items .= $clink;
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'add_custom_text_through_filter', 10, 2);
</pre><p>So, that was all. I hope you found this interesting. If you have any doubt then feel free to ask.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/' rel='bookmark' title='Programmer&#8217;s guide for WordPress Nav menu'>Programmer&#8217;s guide for WordPress Nav menu</a></li><li><a
href='http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/' rel='bookmark' title='The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &amp; pages'>The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a></li><li><a
href='http://www.intechgrity.com/jquery-powered-sidebar-navigation-menu/' rel='bookmark' title='jQuery Vertical Sidebar Navigation menu for your Blog or Website – Free package by iTg'>jQuery Vertical Sidebar Navigation menu for your Blog or Website – Free package by iTg</a></li><li><a
href='http://www.intechgrity.com/download-web-videos-directly-from-your/' rel='bookmark' title='Download Web Videos directly from your Firefox using Video DownloadHelper'>Download Web Videos directly from your Firefox using Video DownloadHelper</a></li><li><a
href='http://www.intechgrity.com/add-dynamic-login-logout-n-site-admin-button-to-wordpress-using-wp-api/' rel='bookmark' title='Add dynamic Login Logout n Site Admin button to WordPress using WP API'>Add dynamic Login Logout n Site Admin button to WordPress using WP API</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=UTiZcJBml9c:paCFHHoQvoY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=UTiZcJBml9c:paCFHHoQvoY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=UTiZcJBml9c:paCFHHoQvoY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=UTiZcJBml9c:paCFHHoQvoY:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=UTiZcJBml9c:paCFHHoQvoY:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=UTiZcJBml9c:paCFHHoQvoY:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/UTiZcJBml9c" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/</feedburner:origLink></item> <item><title>The right way of dealing wp_nav_menu fallback function – List custom categories &amp; pages</title><link>http://feedproxy.google.com/~r/greentechspot/~3/Zw7Pk07pgaE/</link> <comments>http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/#comments</comments> <pubDate>Sat, 19 May 2012 11:11:09 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Wordpress APIs]]></category> <category><![CDATA[WP Programming]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress api]]></category> <category><![CDATA[wordpress functions]]></category> <category><![CDATA[wordpress hacks]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1164</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/">The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a>">here</a></p><p>So, this post succeeds our explanation on WordPress custom Nav menus. We&#8217;ve seen previously how we can use wp_nav_menu function to show user defined menus in the theme. But what if the user doesn&#8217;t provide any menu to the theme location? WordPress will then use the fallback function to write a default menu. wp_page_menu is [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> No related posts.]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/56GKWqVYDCelPNt9lT_CLcrbhpQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/56GKWqVYDCelPNt9lT_CLcrbhpQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/56GKWqVYDCelPNt9lT_CLcrbhpQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/56GKWqVYDCelPNt9lT_CLcrbhpQ/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/">The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/wp_nav_menu-fallback.png"><img
class="size-full wp-image-1170 alignright" title="wp_nav_menu-fallback" src="http://www.intechgrity.com/wp-content/uploads/2012/05/wp_nav_menu-fallback.png" alt="" width="300" height="300" /></a>So, this post succeeds our explanation on <a
title="Programmer’s guide for WordPress Nav menu" href="http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/">WordPress custom Nav menus</a>. We&#8217;ve seen previously how we can use <span
class="code">wp_nav_menu</span> function to show user defined menus in the theme. But what if the user doesn&#8217;t provide any menu to the theme location? WordPress will then use the fallback function to write a default menu. <span
class="code">wp_page_menu</span> is used by default which just prints all the pages of your blog. In many situations, especially while developing themes, you might not want this to happen. You might want to give the user more refined way to control the output of the default navigation menu. You might want to display specific pages along with specific categories. You might also want to include the Home button and our famous <a
title="Add dynamic Login Logout n Site Admin button to WordPress using WP API" href="http://www.intechgrity.com/add-dynamic-login-logout-n-site-admin-button-to-wordpress-using-wp-api/">dynamic login/logout button</a> to the navigation. In such cases, you will need to write your own fallback function from scratch. Today we shall see, the right way of writing the fallback with various customization option. We shall also add Home button to it, give it proper class, list the pages we want (hierarchically) along with hierarchical categories.</p><h2>#1: Registering the Nav menu:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/register-nav-menu-hook.png"><img
class="aligncenter size-full wp-image-1169" title="register-nav-menu-hook" src="http://www.intechgrity.com/wp-content/uploads/2012/05/register-nav-menu-hook.png" alt="" width="560" height="150" /></a>As usual, we register our <span
class="spanlight">primary</span> nav menu like this:</p><pre class="brush: php; title: ; notranslate">
/**
 * The function which registers the primary nav menu
 * @uses register_nav_menus
 * @see http://codex.wordpress.org/Function_Reference/register_nav_menus
 */
function reg_primary_nav() {
    register_nav_menus(array(
        'primary' =&gt; 'Primary Menu',
    ));
}
//hook it to init
add_action('init', 'reg_primary_nav');
</pre><h2>#2: Calling the API with custom fallback:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/print-the-nav-menu.png"><img
class="aligncenter size-full wp-image-1168" title="print-the-nav-menu" src="http://www.intechgrity.com/wp-content/uploads/2012/05/print-the-nav-menu.png" alt="" width="560" height="150" /></a>In order to do that, we need to discuss the arguments of <span
class="code">wp_nav_menu</span>. The one we will be using is <span
class="code">fallback_cb</span> which corresponds to the name of the fallback function. Keeping other arguments default we call it like this:</p><pre class="brush: php; title: ; notranslate">
/**
 * Call the wp_nav_menu API to print the navigation menu
 * We will use a custom fallback function primary_menu_fallback
 * passed in as an argument
 *
 * @see http://codex.wordpress.org/Function_Reference/wp_nav_menu
 */
wp_nav_menu(array(
    'theme_location' =&gt; 'primary', //corresponds to the location as on register_nav_menus
    'container_class' =&gt; 'primary-nav', //custom class for styling purposes
    'fallback_cb' =&gt; 'primary_menu_fallback',
));
</pre><p>What next? Yes, you have guessed it right. We will be writing our <span
class="code">primary_menu_fallback</span> function which we have passed to the wp_nav_menu as an argument.</p><h2>#3: Writing the fallback function:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/primary_menu_fallback-function.png"><img
class="aligncenter size-full wp-image-1167" title="primary_menu_fallback-function" src="http://www.intechgrity.com/wp-content/uploads/2012/05/primary_menu_fallback-function.png" alt="" width="560" height="150" /></a>We write in our fallback function keeping in mind 4 things:</p><ol><li>Add Home button with custom class (current on home page) for styling.</li><li>Add the pages we want in specific number of tiers.</li><li>Add the categories we want in specific number of tiers.</li><li>Add our dynamic login/logout button.</li></ol><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;?php
/**
 * The fallback function for the primary nav menu
 *
 * It is capable of doing 4 custom navigation
 * 1. Home button
 * 2. List the pages you want
 * 3. List the categories you want
 * 4. Add dynamic login/logout &amp; site admin button
 *
 * Check the inline comments for understanding how it works
 */
function primary_menu_fallback() {
    ?&gt;
&lt;div id=&quot;primary&quot; class=&quot;primary-nav&quot;&gt;
    &lt;ul class=&quot;menu&quot;&gt;
        &lt;?php /* add home button */ ?&gt;
        &lt;li class=&quot;home &lt;?php if(is_home() || is_front_page()) echo 'current_page_item'; ?&gt;&quot;&gt;&lt;a href=&quot;&lt;?php bloginfo('url'); ?&gt;&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
        &lt;?php
        /**
         * Show the pages we want
         * We shall be using wp_list_pages
         * For a list of arguments
         * @see http://codex.wordpress.org/Function_Reference/wp_list_pages
         */
        wp_list_pages(array(
            'depth' =&gt; 2, //number of tiers, 0 for unlimited
            'exclude' =&gt; '', //comma seperated IDs of pages you want to exclude
            'title_li' =&gt; '', //must override it to empty string so that it does not break our nav
            'sort_column' =&gt; 'post_title', //see documentation for other possibilites
            'sort_order' =&gt; 'ASC', //ASCending or DESCending
        ));
        ?&gt;
        &lt;?php
        /**
         * Show the categories we want
         * We shall be using wp_list_categories
         * For a list of arguments
         * @see http://codex.wordpress.org/Template_Tags/wp_list_categories
         */
        wp_list_categories(array(
            'orderby' =&gt; 'name', //see documentation for more sorting possibilites
            'order' =&gt; 'ASC', //ASCending or DESCending
            'exclude' =&gt; '', //comma seperated IDs of categories you want to exclude
            'title_li' =&gt; '', //must override it to empty string so that it does not break our nav
            'depth' =&gt; 2, //number of tiers, 0 for unlimited
            'hide_empty' =&gt; 1, //hides empty categories if 1, pass 0 to show
        ));
        ?&gt;
        &lt;?php
        /**
         * Add our dynamic login/logout &amp; Site Admin buttons
         * @see http://www.intechgrity.com/add-dynamic-login-logout-n-site-admin-button-to-wordpress-using-wp-api/
         */
        ?&gt;
        &lt;li class=&quot;login&quot;&gt;&lt;?php wp_loginout(); ?&gt;&lt;/li&gt;
        &lt;?php wp_register('&lt;li class=&quot;admin&quot;&gt;'); ?&gt;
    &lt;/ul&gt;
&lt;/div&gt;
    &lt;?php
}
</pre><p>So now the wp_nav_menu will first try to output user&#8217;s own menu if provided, else will fallback to your own function. This is the most sophisticated way of handling the fallback. In our next chapter, we shall see how we can directly hook into wp_nav_menu to even customize the user&#8217;s own menu dynamically. Till then, have fun with the fallback. Be creative and show us what fallback are you falling for?</p><p>No related posts.</p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Zw7Pk07pgaE:jcTRGixexUg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Zw7Pk07pgaE:jcTRGixexUg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Zw7Pk07pgaE:jcTRGixexUg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Zw7Pk07pgaE:jcTRGixexUg:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Zw7Pk07pgaE:jcTRGixexUg:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Zw7Pk07pgaE:jcTRGixexUg:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/Zw7Pk07pgaE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/</feedburner:origLink></item> <item><title>Programmer’s guide for WordPress Nav menu</title><link>http://feedproxy.google.com/~r/greentechspot/~3/4H1rQ8AIMpA/</link> <comments>http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/#comments</comments> <pubDate>Fri, 18 May 2012 21:46:41 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Wordpress APIs]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress api]]></category> <category><![CDATA[wordpress functions]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1153</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/">Programmer&#8217;s guide for WordPress Nav menu</a>">here</a></p><p>This post is basically a prelim to our upcoming post on programmatically customizing the behaviour and output of WordPress 3 Nav menus. Of course, before we actually customize it, we need to know what it is and how the Nav Menu API works. Consider it as a quick start guide on WordPress Nav menu. So, [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/' rel='bookmark' title='Add home &amp; other items directly in WP Nav Menu using wp_nav_menu_items filters'>Add home &#038; other items directly in WP Nav Menu using wp_nav_menu_items filters</a></li><li><a
href='http://www.intechgrity.com/parse-id-list-from-csv-using-new-wordpress-3-0-api-the-right-way-of-doing/' rel='bookmark' title='Parse ID list from CSV, using new WordPress 3.0 API &#8211; The right way of doing'>Parse ID list from CSV, using new WordPress 3.0 API &#8211; The right way of doing</a></li><li><a
href='http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/' rel='bookmark' title='The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &amp; pages'>The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a></li><li><a
href='http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/' rel='bookmark' title='Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach'>Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach</a></li><li><a
href='http://www.intechgrity.com/how-to-add-your-own-stylesheet-to-your-wordpress-plugin-settings-page-or-all-admin-page/' rel='bookmark' title='How to add your own stylesheet to your WordPress plugin settings page or All Admin page'>How to add your own stylesheet to your WordPress plugin settings page or All Admin page</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/mZuHYT9DGhunzYU0PXvYtvTwf28/0/da"><img src="http://feedads.g.doubleclick.net/~a/mZuHYT9DGhunzYU0PXvYtvTwf28/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/mZuHYT9DGhunzYU0PXvYtvTwf28/1/da"><img src="http://feedads.g.doubleclick.net/~a/mZuHYT9DGhunzYU0PXvYtvTwf28/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/">Programmer&#8217;s guide for WordPress Nav menu</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/working-with-wordpress-nav.png"><img
class="alignright size-thumbnail wp-image-1154" title="working-with-wordpress-nav" src="http://www.intechgrity.com/wp-content/uploads/2012/05/working-with-wordpress-nav-150x150.png" alt="" width="150" height="150" /></a>This post is basically a prelim to our upcoming post on programmatically customizing the behaviour and output of WordPress 3 Nav menus. Of course, before we actually customize it, we need to know what it is and how the Nav Menu API works. Consider it as a quick start guide on WordPress Nav menu. So, enough of introduction, let&#8217;s get to the business.</p><h2>#1: WP Nav menu from front end:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/frontend-wp-nav.png"><img
class="aligncenter size-full wp-image-1157" title="frontend-wp-nav" src="http://www.intechgrity.com/wp-content/uploads/2012/05/frontend-wp-nav.png" alt="" width="560" height="150" /></a></p><p>From frontend, WordPress Nav menu is a beautiful system for creating custom hierarchical horizontal and/or vertical <a
title="iTg Sexy Horizontal 1 tier Dropdown menu" href="http://www.intechgrity.com/itg-sexy-horizontal-1-tier-dropdown-menu/">nav menus</a>.  Now a days, all the themes have WP Nav menu enabled by default. So, simply go to the <strong>Appearance &gt; Menus</strong> and start creating your menus from there. It is a nice work of UI designing I must say. You simply add in items, drag and position them, move one item under another and bingo. After that, all you need to do is set the menu for some predefined nav area and you are done. If your theme supports it, it should style up automatically giving you the finest control of your navigation bar.</p><h2>#2: WP Nav API from the backend:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/code-behind-wp-nav-menu.png"><img
class="aligncenter size-full wp-image-1156" title="code-behind-wp-nav-menu" src="http://www.intechgrity.com/wp-content/uploads/2012/05/code-behind-wp-nav-menu.png" alt="" width="560" height="150" /></a></p><p>Now comes the real part. We have been saying, &#8220;if your theme supports&#8230;this&#8230;.&#8221;, &#8220;if your theme supports&#8230;.that&#8230;.&#8221;, let us see how do we support this from the backend (the code of the theme).</p><h3>#2.1: Understanding the API:</h3><p>The API for <a
href="http://codex.wordpress.org/Function_Reference/register_nav_menus" target="_blank">registering nav</a> menu is, indeed, <span
class="code">register_nav_menus</span>, and the format is:</p><pre class="brush: php; title: ; notranslate">
register_nav_menus(array(
    'nav_menu_key' =&gt; 'Nav Menu Nice Name',
    'nav_menu_key2' =&gt; 'Nav Menu Nice Name 2',
));
</pre><p>Of course, you can add as many instances of nav menu you wish through the array. Basically each of the items will provide a unique entry under the <strong>Theme Location</strong> of <strong>Appearance &gt; Menu</strong>.</p><p>The call of the function must be made during the <span
class="code">init</span>. So, in practice, we shall hook the call using the <span
class="code">add_action</span> API to the <span
class="code">init</span>.</p><pre class="brush: php; title: ; notranslate">
function my_theme_register_menu() {
    register_nav_menus(array(
        'nav_menu_key' =&gt; 'Nav Menu Nice Name',
        'nav_menu_key2' =&gt; 'Nav Menu Nice Name 2',
    ));
}
add_action('init', 'my_theme_register_menu');
</pre><p>So, the following code will add the following three sections to the admin area.</p><pre class="brush: php; title: ; notranslate">
function my_theme_three_navs() {
    register_nav_menus(array(
        'primary' =&gt; 'Primary Menu',
        'top' =&gt; 'Top Nav Menu',
        'bottom' =&gt; 'Footer Nav Menu',
    ));
}
add_action('init', 'my_theme_three_navs');
</pre><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/created-nav-theme-location.png"><img
class="aligncenter size-full wp-image-1155" title="created-nav-theme-location" src="http://www.intechgrity.com/wp-content/uploads/2012/05/created-nav-theme-location.png" alt="" width="299" height="320" /></a></p><h3>#2.2: Printing the Nav menu in the theme:</h3><p>So, you&#8217;ve added the navigation menu to the admin frontend, your theme user has made some nice navigation menu and added it to the theme section as you have provided. Now, it is time to get that nav menu and show it in the theme frontend. For that we will be using <span
class="code">wp_nav_menu</span> API. You can read more about it <a
href="http://codex.wordpress.org/Function_Reference/wp_nav_menu" target="_blank">here</a> or in our upcoming post on WP Nav Menu customization. Till then, we will just focus on printing the default nav_menu with default fallback. The parameter <span
class="code">theme_location</span> must be the key of your Nav menu you have set using <span
class="code">register_nav_menus</span>.</p><pre class="brush: php; title: ; notranslate">
wp_nav_menu(array(
    'theme_location' =&gt; 'nav_menu_key',
    'container_class' =&gt; 'my-nav'
));
</pre><p>Calling it in files like <span
class="spanlight">header.php</span> would produce HTML similar to this:</p><pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;nav_menu_key&quot; class=&quot;my-nav&quot;&gt;
    &lt;ul class=&quot;menu&quot;&gt;
        &lt;li class=&quot;home current_page_item&quot;&gt;&lt;a href=&quot;http://localhost/wp&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;page_item page-item-2&quot;&gt;&lt;a href=&quot;http://localhost/wp/sample-page/&quot;&gt;Sample Page&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
</pre><p>So, style up using your favorite CSS editor (I prefer NetBeans of course) and make a good nav menu for this HTML markup. You can check our <a
title="iTg Sexy Horizontal 1 tier Dropdown menu" href="http://www.intechgrity.com/itg-sexy-horizontal-1-tier-dropdown-menu/" target="_blank">vertical dropdown menu</a> and use it as a startup. So, that is all for today. Tomorrow, we shall see how we can extend the features of the WordPress Nav menu.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/add-home-other-items-directly-in-wp-nav-menu-using-wp_nav_menu_items-filters/' rel='bookmark' title='Add home &amp; other items directly in WP Nav Menu using wp_nav_menu_items filters'>Add home &#038; other items directly in WP Nav Menu using wp_nav_menu_items filters</a></li><li><a
href='http://www.intechgrity.com/parse-id-list-from-csv-using-new-wordpress-3-0-api-the-right-way-of-doing/' rel='bookmark' title='Parse ID list from CSV, using new WordPress 3.0 API &#8211; The right way of doing'>Parse ID list from CSV, using new WordPress 3.0 API &#8211; The right way of doing</a></li><li><a
href='http://www.intechgrity.com/the-right-way-of-dealing-wp_nav_menu-fallback-function-list-custom-categories-pages/' rel='bookmark' title='The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &amp; pages'>The right way of dealing wp_nav_menu fallback function &#8211; List custom categories &#038; pages</a></li><li><a
href='http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/' rel='bookmark' title='Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach'>Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach</a></li><li><a
href='http://www.intechgrity.com/how-to-add-your-own-stylesheet-to-your-wordpress-plugin-settings-page-or-all-admin-page/' rel='bookmark' title='How to add your own stylesheet to your WordPress plugin settings page or All Admin page'>How to add your own stylesheet to your WordPress plugin settings page or All Admin page</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=4H1rQ8AIMpA:gVe1CbG1VZc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=4H1rQ8AIMpA:gVe1CbG1VZc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=4H1rQ8AIMpA:gVe1CbG1VZc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=4H1rQ8AIMpA:gVe1CbG1VZc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=4H1rQ8AIMpA:gVe1CbG1VZc:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=4H1rQ8AIMpA:gVe1CbG1VZc:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/4H1rQ8AIMpA" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/programmers-guide-for-wordpress-nav-menu/</feedburner:origLink></item> <item><title>Catch the first image on WordPress post and use as thumbnail</title><link>http://feedproxy.google.com/~r/greentechspot/~3/Fx7YpyWcbaE/</link> <comments>http://www.intechgrity.com/catch-the-first-image-on-wordpress-post-and-use-as-thumbnail/#comments</comments> <pubDate>Fri, 18 May 2012 16:15:34 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[WP Programming]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress hacks]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1144</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/catch-the-first-image-on-wordpress-post-and-use-as-thumbnail/">Catch the first image on WordPress post and use as thumbnail</a>">here</a></p><p>So this the last (perhaps) continuation of the posts getting the source URL of the featured image and using TimThumb with WordPress MultiSite. As the title says, today we are going to discuss how to automatically fetch the URL of the first image for any WordPress posts or pages and then we shall also see [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/' rel='bookmark' title='How to get the souce URL of the featured image in WordPress'>How to get the souce URL of the featured image in WordPress</a></li><li><a
href='http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/' rel='bookmark' title='Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach'>Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach</a></li><li><a
href='http://www.intechgrity.com/getting-post-thumbnail-on-wordpress-3-w-wo-specific-post-id/' rel='bookmark' title='Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID'>Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID</a></li><li><a
href='http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/' rel='bookmark' title='WordPress Multisite compatibility for timthumb'>WordPress Multisite compatibility for timthumb</a></li><li><a
href='http://www.intechgrity.com/get-post-count-of-a-category-including-sub-categories-in-wordpress/' rel='bookmark' title='Get post count of a category including sub-categories in WordPress'>Get post count of a category including sub-categories in WordPress</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/lpHOYLuXoRsRktlZlH4ctN1Qcoo/0/da"><img src="http://feedads.g.doubleclick.net/~a/lpHOYLuXoRsRktlZlH4ctN1Qcoo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lpHOYLuXoRsRktlZlH4ctN1Qcoo/1/da"><img src="http://feedads.g.doubleclick.net/~a/lpHOYLuXoRsRktlZlH4ctN1Qcoo/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/catch-the-first-image-on-wordpress-post-and-use-as-thumbnail/">Catch the first image on WordPress post and use as thumbnail</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/grab-first-image-from-a-post.png"><img
class="alignright size-full wp-image-1149" title="grab-first-image-from-a-post" src="http://www.intechgrity.com/wp-content/uploads/2012/05/grab-first-image-from-a-post.png" alt="" width="300" height="300" /></a>So this the last (perhaps) continuation of the posts <a
title="How to get the souce URL of the featured image in WordPress" href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">getting the source URL of the featured image</a> and <a
title="WordPress Multisite compatibility for timthumb" href="http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/">using TimThumb with WordPress MultiSite</a>. As the title says, today we are going to discuss how to automatically fetch the URL of the first image for any WordPress posts or pages and then we shall also see how to pass it to timthumb etc for creating thumbnails. This becomes useful, when your theme/plugin users are not using the <a
title="Setting Post Thumbnail on WordPress with custom size and Cropping – A simpler approach" href="http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/">featured image</a> thingy of WordPress. In that scenario, what we can do is, search the post content for the any instance of image and return the first found image SRC. In case no images were found, we can also return some default image if necessary. So, let us see what we can do.</p><h2>#1: Understanding the concept:</h2><p>The concept remains the same as our previous post on getting the image SRC of featured image.</p><ul><li>We search the post content using preg_match.</li><li>If any instance is found we return the src.</li><li>Else we return false or optionally return the default image URL</li></ul><h2>#2: The code behind:</h2><pre class="brush: php; title: ; notranslate">
/**
 * Auto Generate Thumbnail URL from the post content
 *
 * First checks if the featured image is present on the post/page or not
 * If yes, then it returns the featured image URL (Source URL)
 * else, it manually searches the content and returns the URL of the first image found
 *
 * If no image is found then it returns the $default variable which is set to false by default
 *
 * @uses itg_get_the_post_thumbnail_url
 * @param int $id Use null if within the loop or give some ID
 * @param string $size The size of the thumbnail, 'post-thumbnail' by default
 * @param bool|string $default The default URL of not-found image or boolean false which will be returned
 * @param string $type The type of the entry. Can be 'post' or 'page'
 * @return mixed bool|string The URL of the featured image or first found image on success, false or default image (as passed by the $default parameter) URL on failure
 */
function itg_auto_gen_thumbnail($id = null, $size = 'post-thumbnail', $default = false, $type = 'post') {
    /**
     * try to get the featured image URL
     * @see http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/
     */
    $image_src = itg_get_the_post_thumbnail_url($id, $size);
    if($image_src != '') {
        //on success return the featured image
        return $image_src;
    } else {
        //try to get the first image
        if($id == null) {
            //called within the loop
            $content = get_the_content();
        } else {
            //get the post content
            $p_data = ($type == 'post')? get_post($id) : get_page($id);
            $content = $p_data-&gt;post_content;
            unset($p_data);
        }
        //the match pattern
        $src_pattern = '/&lt;img.+src=[\'&quot;]([^\'&quot;]+)[\'&quot;].*&gt;/i';
        //store $default to $src for returning
        $src = $default;
        $matches = array();

        //match it
        if(preg_match($src_pattern, $content, $matches)) {
            //found, so replace the $src value
            $src = trim($matches[1]);
        }

        return $src;
    }
}
</pre><p>So it is pretty much straight forward. It tries to get the featured image first. If not found then, it searches the content for an image. You call it like this:</p><pre class="brush: php; title: ; notranslate">
//within loop
echo itg_auto_gen_thumbnail();

//within loop for different size
$my_image = itg_auto_gen_thumbnail(null, 'full');

//outside loop for a page
$my_image = itg_auto_gen_thumbnail(10, 'full', false, 'page');

//set default image SRC to return
$my_image = itg_auto_gen_thumbnail(null, 'full', 'http://locati.on/to/image.png');
</pre><p>As you can see, it uses the <span
class="code">itg_get_the_post_thumbnail_url</span> function internally. Have a look at <a
title="How to get the souce URL of the featured image in WordPress" href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">here</a> for more information. You can even pass the return image to TimThumb using our <a
title="WordPress Multisite compatibility for timthumb" href="http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/">TimThumb compatibility</a> function for dynamic resizing.</p><p>That will be all. Hope it helps you out for your code. If you have any question, then do ask.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/' rel='bookmark' title='How to get the souce URL of the featured image in WordPress'>How to get the souce URL of the featured image in WordPress</a></li><li><a
href='http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/' rel='bookmark' title='Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach'>Setting Post Thumbnail on WordPress with custom size and Cropping &#8211; A simpler approach</a></li><li><a
href='http://www.intechgrity.com/getting-post-thumbnail-on-wordpress-3-w-wo-specific-post-id/' rel='bookmark' title='Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID'>Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID</a></li><li><a
href='http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/' rel='bookmark' title='WordPress Multisite compatibility for timthumb'>WordPress Multisite compatibility for timthumb</a></li><li><a
href='http://www.intechgrity.com/get-post-count-of-a-category-including-sub-categories-in-wordpress/' rel='bookmark' title='Get post count of a category including sub-categories in WordPress'>Get post count of a category including sub-categories in WordPress</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Fx7YpyWcbaE:3kLHhjd8tlg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Fx7YpyWcbaE:3kLHhjd8tlg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Fx7YpyWcbaE:3kLHhjd8tlg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Fx7YpyWcbaE:3kLHhjd8tlg:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Fx7YpyWcbaE:3kLHhjd8tlg:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Fx7YpyWcbaE:3kLHhjd8tlg:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/Fx7YpyWcbaE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/catch-the-first-image-on-wordpress-post-and-use-as-thumbnail/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/catch-the-first-image-on-wordpress-post-and-use-as-thumbnail/</feedburner:origLink></item> <item><title>WordPress Multisite compatibility for timthumb</title><link>http://feedproxy.google.com/~r/greentechspot/~3/noGLbLbo1Rc/</link> <comments>http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/#comments</comments> <pubDate>Thu, 17 May 2012 12:30:14 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[WP Programming]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress hacks]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1085</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/">WordPress Multisite compatibility for timthumb</a>">here</a></p><p>Ever found the necessity to use TimThumb for your WordPress theme and/or plugin? Then you should&#8217;ve faced the problem where you can not tell TimThumb about the correct location of image files in WordPress Multi-Site setup and in return TimThumb gave you internal image not found error. The tutorial over binarymoon is a bit backdated [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> No related posts.]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/AwjirAPbzT-lfDae6RrpmvvahRQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/AwjirAPbzT-lfDae6RrpmvvahRQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/AwjirAPbzT-lfDae6RrpmvvahRQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/AwjirAPbzT-lfDae6RrpmvvahRQ/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/">WordPress Multisite compatibility for timthumb</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/wordpress-ms-timthumb.png"><img
class="alignright size-full wp-image-1128" title="wordpress-ms-timthumb" src="http://www.intechgrity.com/wp-content/uploads/2012/05/wordpress-ms-timthumb.png" alt="" width="300" height="300" /></a>Ever found the necessity to use <a
href="http://www.binarymoon.co.uk/projects/timthumb/" target="_blank">TimThumb</a> for your WordPress theme and/or plugin? Then you should&#8217;ve faced the problem where you can not tell TimThumb about the correct location of image files in WordPress Multi-Site setup and in return TimThumb gave you internal image not found error. The tutorial over <a
href="http://www.binarymoon.co.uk/2009/10/timthumb-wordpress-mu/" target="_blank">binarymoon</a> is a bit backdated as it uses custom fields to get and set the image with TimThumb. The obvious question which arises is, how to use the <a
title="Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID" href="http://www.intechgrity.com/getting-post-thumbnail-on-wordpress-3-w-wo-specific-post-id/">WordPress&#8217; own</a> <a
title="Setting Post Thumbnail on WordPress with custom size and Cropping – A simpler approach" href="http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/">featured image</a> with TimThumb to give it maximum resizing capability. Previously, we have discussed about how to <a
title="How to get the souce URL of the featured image in WordPress" href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">get the image src from the featured image HTML</a> and use it for other purposes like CSS etc. Now, we shall see, how to use the original image source and modify it so that it works with TimThumb (for WP MS ofcourse).</p><h2>#1: Understanding the concept:</h2><p>What WordPress does is, it gives some nicer URL to the static files in case of MultiSite setup. In general setup (without networking), the file URL is like this</p><pre class="brush: plain; title: ; notranslate">

http://yoursite.com/wp-content/uploads/2012/05/image.jpg
</pre><p>Which is just the absolute URL of the file.</p><p>But, for WordPress it becomes like this</p><pre class="brush: plain; title: ; notranslate">

http://sub.yoursite.com/files/2012/05/image.jpg
</pre><p>Which is obviously not the absolute static URL of the file. WordPress internally modifies the path via some htaccess rewrite to push the original content at this URL.</p><p>The main reason why TimThumb doesn&#8217;t work is, because, there is no <span
class="spanlight">real</span> directory files anywhere on the root of your site. So, we have to either rewrite the URL before passing it to TimThumb or we have to somehow tell TimThumb about this issue. The second solution is not feasible as it involves editing the core of the script, which means, you have to do it again after any update. So, we shall use the first technique within our themes or plugins to make it WP MS compatible. Also, we shall explicitly check the image URL for the presence of <span
class="spanlight">/files/</span> (uploaded media) and <span
class="spanlight">/wp-content/</span> (theme or plugin files) and will treat them properly.</p><h2>#2: The code behind the compatibility:</h2><pre class="brush: php; title: ; notranslate">
/**
 * TimThumb Image
 *
 * **Properly** appends the source image URL to timthumb for WP MS compatibility
 * Explicitly check the presence of /files/ and /wp-content/ in the URI and treats accordingly.
 * Has compatibility for both uploaded media as well as theme/plugin media files.
 *
 * You can also set height and width using the parameters
 *
 * @author Swashata
 * @param String $src The URL of the original image 'http://path/to/image.jpg
 * @param Int $h The height of the image
 * @param Int $w The width of the image
 * @return String The modified timthumb URL of the image
 */
function itg_timthumb_image($src, $h = 50, $w = 50) {
    //set your timthumb url
    $timthumb_url = get_bloginfo('wpurl') . '/path/to/timthumb.php';
    //check for multisite
    if(is_multisite()) {
        //get rid of the www part from the HTTP_HOST
        $host = preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST']);
        if(preg_match('/https?:\/\/(?:www\.)?' . $host . '/i', $src)) { //check if internal
            //is it a request for directory wp-content?
            if(preg_match('/\/wp-content\//', $src)) {
                $src_parts = explode('/wp-content/', $src);
                $src = '/wp-content/' . $src_parts[1];
            }
            //is it a request for /files/ virtual directory
            else if(preg_match('/\/files\//', $src)) {
                $src_parts = explode('/files/', $src);
                $src = '/blogs.dir/' . get_current_blog_id() . '/files/' . $src_parts[1];
            }
        }
    }
    return $timthumb_url . '?src=' . urlencode($src) . '&amp;amp;h=' . $h . '&amp;amp;w=' . $w . '&amp;amp;zc=1';
}
</pre><p>And finally you call the function to do what you want. Note that you do need to pass the raw image URL as the <span
class="code">$src</span> variable. If you wish to TimThumb your featured images, then use the function used <a
title="How to get the souce URL of the featured image in WordPress" href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">here</a> and call the above function like this:</p><pre class="brush: php; title: ; notranslate">
//get 200 X 200 thumbnail for the current post featured image
itg_timthumb_image(itg_get_the_post_thumbnail_url(), 200, 200);
</pre><p>So that was the most complete and simple solution I could think of while making my WordPress things compatible with MultiSite and TimThumb. If you know any better technique or have any problem using this, then do let us know.</p><p>No related posts.</p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=noGLbLbo1Rc:_VLjMr78GRI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=noGLbLbo1Rc:_VLjMr78GRI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=noGLbLbo1Rc:_VLjMr78GRI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=noGLbLbo1Rc:_VLjMr78GRI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=noGLbLbo1Rc:_VLjMr78GRI:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=noGLbLbo1Rc:_VLjMr78GRI:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/noGLbLbo1Rc" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/wordpress-multisite-compatibility-for-timthumb/</feedburner:origLink></item> <item><title>How to get the souce URL of the featured image in WordPress</title><link>http://feedproxy.google.com/~r/greentechspot/~3/NRBOPDwT6As/</link> <comments>http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/#comments</comments> <pubDate>Wed, 16 May 2012 13:01:37 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[WP Programming]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress api]]></category> <category><![CDATA[wordpress hacks]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1117</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">How to get the souce URL of the featured image in WordPress</a>">here</a></p><p>Previously we have talked about setting and getting featured images in WordPress using built in APIs. But, one of the drawback of this method is, it returns or prints the image with proper HTML. In other words, it does not give you the URL of the image. It becomes necessary if you want to Pass [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> No related posts.]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/PiC7xGhTj01ahO7xearlsI_cU-o/0/da"><img src="http://feedads.g.doubleclick.net/~a/PiC7xGhTj01ahO7xearlsI_cU-o/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/PiC7xGhTj01ahO7xearlsI_cU-o/1/da"><img src="http://feedads.g.doubleclick.net/~a/PiC7xGhTj01ahO7xearlsI_cU-o/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/">How to get the souce URL of the featured image in WordPress</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/image-url-featured-post-wp.png"><img
class="alignright size-thumbnail wp-image-1124" title="image-url-featured-post-wp" src="http://www.intechgrity.com/wp-content/uploads/2012/05/image-url-featured-post-wp-150x150.png" alt="" width="150" height="150" /></a>Previously we have talked about <a
title="Setting Post Thumbnail on WordPress with custom size and Cropping – A simpler approach" href="http://www.intechgrity.com/setting-post-thumbnail-on-wordpress-with-custom-size-and-cropping-a-simpler-approach/">setting</a> and <a
title="Getting Post thumbnail on WordPress 3 w/ w/o specific Post ID" href="http://www.intechgrity.com/getting-post-thumbnail-on-wordpress-3-w-wo-specific-post-id/">getting</a> featured images in WordPress using built in APIs. But, one of the drawback of this method is, it returns or prints the image with proper HTML. In other words, it does not give you the URL of the image. It becomes necessary if you want to</p><ul><li>Pass the URL to scripts like say TimThumb for dynamic resizing.</li><li>Use the URL for inline css styling</li><li>and others&#8230;</li></ul><p>So, in this tutorial we will learn how to do that by passing the fetched image HTML to our custom function.</p><h2>#1: Understanding the concept:</h2><h3>#1.1: The output HTML of the API:</h3><p>When we use <span
class="code">get_the_post_thumbnail</span>, then it returns a nice HTML code which looks like this&#8230;</p><pre class="brush: xml; title: ; notranslate">
    &lt;img width=&quot;50&quot; height=&quot;50&quot; title=&quot;3d_pie_chart_icon_psd_by_psdblast-d4jqqn4&quot; alt=&quot;3d_pie_chart_icon_psd_by_psdblast-d4jqqn4&quot; src=&quot;http://localhost/wp/wp-content/uploads/2012/05/img.jpg&quot; /&gt;
</pre><h3>#1.2: What we need to do:</h3><p>So, somehow we need to extract the src attribute from the HTML img tag. Now, there are a number of ways for doing this. I will discuss the fastest and simplest <span
class="code">preg_match</span> method, where you have to pass the output string and will get the src attribute.</p><h2>#2: Using preg_match to get the source URL</h2><p>From the output HTML we see that</p><ol><li>The image tag starts with <em>&lt;img</em>.</li><li>There can be almost anything or nothing before the <em>src</em> keyword.</li><li>Inside the src the URL of the image is written in between two double quote. <em>It can contain anything except double quotes</em>.</li><li>After src there can be again anything or nothing.</li></ol><p>So, keeping in these strategies in mind, we make our regular expression to work with preg_match.</p><pre class="brush: php; title: ; notranslate">
$src_pattern = '/&lt;img.+src=[\'&quot;]([^\'&quot;]+)[\'&quot;].*&gt;/i';
</pre><p>Now we use the preg_match like this:</p><pre class="brush: php; title: ; notranslate">
$src = '';
$matches = array();
if(preg_match($src_pattern, $text, $matches)) {
    $src = $matches[1];
}
</pre><p>Where the <span
class="code">$text</span> variable should hold the HTML returned by the get_the_post_thumbnail function. So, we put together all the code and come up with something like this</p><pre class="brush: php; title: ; notranslate">
/**
 * Get the post thumbnail URL
 *
 * Automatically gets and returns the source URL of the full-sized featured image
 * of the current post in the loop.
 *
 * You can also change the parameters below for more flexibility
 *
 * @param int $id The id of the post. If null, then it will fetch the current post thumbnail.
 * @param String $size The registered image size name. Default is full, which gives the full-size image url. You set it to post-thumbnail or anything else.
 * @return String The URL of the featured image of the queried size.
 */
function itg_get_the_post_thumbnail_url($id = null, $size = 'full') {
    //if no post thumbnail is set, return empty string
    if(!has_post_thumbnail($id))
        return '';

    //get the post thumbnail
    $text = get_the_post_thumbnail($id, $size);

    //initialize the variables
    $src = '';
    $matches = array();

    //set the match string
    $src_pattern = '/&lt;img.+src=[\'&quot;]([^\'&quot;]+)[\'&quot;].*&gt;/i';

    //match it
    if(preg_match($src_pattern, $text, $matches)) {
        $src = $matches[1];
    }

    return trim($src);
}
</pre><p>What the function does is it automatically gets the featured image of the current post in the loop and returns its full sized image URL. But it has some flexibility as well. As the comments say, the available parameters are:</p><ul><li><strong>$id</strong> &#8211; <em>Integer</em> &#8211; The id of the post. If null, then it will fetch the current post thumbnail.</li><li><strong>$size</strong> &#8211; <em>String </em>- The registered image size name. Default is full, which gives the full-size image url. You set it to post-thumbnail or anything else.</li><li><strong>return </strong>- <em>String</em> &#8211; The URL of the featured image of the queried size.</li></ul><p>So when ever you need to get the image url, you call it like this:</p><pre class="brush: php; title: ; notranslate">
//get full sized, current post
$img_src = itg_get_the_post_thumbnail_url();

//get thumbnail sized, custom post
//replace with your post ID
$img_src = itg_get_the_post_thumbnail_url(10, 'post-thumbnail');

//get thumbnail sized, current post
$img_src = itg_get_the_post_thumbnail_url(null, 'post-thumbnail');

//get full sized, custom post
$img_src = itg_get_the_post_thumbnail_url(10);
</pre><p>You are free to use this code anywhere you like. It should do what you want. If there is anything else you want to know, then just comment.</p><p>No related posts.</p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=NRBOPDwT6As:Kk-kNCrslm8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=NRBOPDwT6As:Kk-kNCrslm8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=NRBOPDwT6As:Kk-kNCrslm8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=NRBOPDwT6As:Kk-kNCrslm8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=NRBOPDwT6As:Kk-kNCrslm8:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=NRBOPDwT6As:Kk-kNCrslm8:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/NRBOPDwT6As" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/how-to-get-the-souce-url-of-the-featured-image-in-wordpress/</feedburner:origLink></item> <item><title>10 free minimalist grey scale icon sets for your web design</title><link>http://feedproxy.google.com/~r/greentechspot/~3/0cdSI8PUwIE/</link> <comments>http://www.intechgrity.com/10-free-minimalist-grey-scale-icon-sets-for-your-web-design/#comments</comments> <pubDate>Tue, 15 May 2012 17:44:00 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Web Designing]]></category> <category><![CDATA[Icons]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1094</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/10-free-minimalist-grey-scale-icon-sets-for-your-web-design/">10 free minimalist grey scale icon sets for your web design</a>">here</a></p><p>Previously, we had discussed about websites providing FREE icons and background patterns. Today we are going to give you some modern minimalist icon sets made on grey scale. The awesome thing about grey scale icons are, they blend with almost any design. So, we searched over the Internet and finally ended up with 10 most [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/15-free-web-icons-background/' rel='bookmark' title='15+ Free Web-Icons &amp; Background Patterns/Texture download sites to boost your Blog &amp; Website designing'>15+ Free Web-Icons &amp; Background Patterns/Texture download sites to boost your Blog &amp; Website designing</a></li><li><a
href='http://www.intechgrity.com/huge-collection-of-css-js-jquery/' rel='bookmark' title='Huge collection of CSS, JS &amp; jQuery Dropdown Navigation menus for FREE &#124; Use on your Web site or Blog'>Huge collection of CSS, JS &amp; jQuery Dropdown Navigation menus for FREE &#124; Use on your Web site or Blog</a></li><li><a
href='http://www.intechgrity.com/create-ajax-style-loading-animated-gif/' rel='bookmark' title='Create Ajax style Loading Animated Gif images easily with 3 Free Online Generators'>Create Ajax style Loading Animated Gif images easily with 3 Free Online Generators</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/c9-1Da3FTk9c28kdy9Uv4ro5jaE/0/da"><img src="http://feedads.g.doubleclick.net/~a/c9-1Da3FTk9c28kdy9Uv4ro5jaE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/c9-1Da3FTk9c28kdy9Uv4ro5jaE/1/da"><img src="http://feedads.g.doubleclick.net/~a/c9-1Da3FTk9c28kdy9Uv4ro5jaE/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/10-free-minimalist-grey-scale-icon-sets-for-your-web-design/">10 free minimalist grey scale icon sets for your web design</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/10-minimalist-grey-scale-icon-set.png"><img
class="alignright size-thumbnail wp-image-1098" title="10-minimalist-grey-scale-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/10-minimalist-grey-scale-icon-set-150x150.png" alt="" width="150" height="150" /></a>Previously, we had discussed <a
title="15+ Free Web-Icons &amp; Background Patterns/Texture download sites to boost your Blog &amp; Website designing" href="http://www.intechgrity.com/15-free-web-icons-background/">about websites providing FREE icons and background patterns</a>. Today we are going to give you some modern minimalist icon sets made on grey scale. The awesome thing about grey scale icons are, they blend with almost any design. So, we searched over the Internet and finally ended up with 10 most beautiful icon packs. Most of them are free for both commercial as well as personal, and only requires an attribution. But do check the licenses yourself before using it on any commercial projects. So, here we go with the list:</p><h2>Big Sized Icons with Included PSD</h2><h3>#1: Retina Display Icon Set</h3><p
style="text-align: center;"><a
href="http://blog.twg.ca/2010/11/retina-display-icon-set/" target="_blank"><img
class="aligncenter size-full wp-image-1106" title="retina-display-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/retina-display-icon-set.png" alt="" width="560" height="150" /></a><strong>Sizes:</strong> 64&#215;64, 48&#215;48, 24&#215;24 (<em>PSD included</em>) <strong>Total Icons: </strong>147 <strong>Download: </strong><a
href="http://blog.twg.ca/2010/11/retina-display-icon-set/" target="_blank">Click here</a></p><h3>#2: WPZOOM Developer Icon Set</h3><p
style="text-align: center;"><a
href="http://www.wpzoom.com/wpzoom/new-freebie-wpzoom-developer-icon-set-154-free-icons/" target="_blank"><img
class="aligncenter size-full wp-image-1108" title="wpzoom-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/wpzoom-icon-set.png" alt="" width="560" height="150" /></a><strong>Sizes:</strong> 48&#215;48 (<em>PSD included</em>) <strong>Total Icons:</strong> 154 <strong>Download: </strong><a
href="http://www.wpzoom.com/wpzoom/new-freebie-wpzoom-developer-icon-set-154-free-icons/" target="_blank">Click here</a></p><h3 style="text-align: left;">#3: Mnml Icon Set</h3><p
style="text-align: center;"><a
href="http://brsev.deviantart.com/art/Mnml-Icon-Set-106367676" target="_blank"><img
class="aligncenter size-full wp-image-1105" title="mnml-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/mnml-icon-set.png" alt="" width="560" height="150" /></a><strong>Sizes:</strong> 128&#215;128 <strong>Total Icons:</strong> 32 <strong>Download: </strong><a
href="http://brsev.deviantart.com/art/Mnml-Icon-Set-106367676" target="_blank">Click here</a></p><h3 style="text-align: left;">#4: Free Vector Icons</h3><p
style="text-align: center;"><a
href="http://www.dezinerfolio.com/freebie/30-free-vector-icons" target="_blank"><img
class="aligncenter size-full wp-image-1100" title="free-vector-icons" src="http://www.intechgrity.com/wp-content/uploads/2012/05/free-vector-icons.png" alt="" width="560" height="150" /></a><strong>Sizes:</strong> Vectors (<em>PSD included</em>) <strong>Total Icons:</strong> 30 <strong>Download: </strong><a
href="http://www.dezinerfolio.com/freebie/30-free-vector-icons" target="_blank">Click here</a></p><h3 style="text-align: left;">#5: Iconic Icon Set</h3><p
style="text-align: center;"><a
href="http://somerandomdude.com/work/iconic/" target="_blank"><img
class="aligncenter size-full wp-image-1103" title="iconic-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/iconic-icon-set.png" alt="" width="560" height="150" /></a><strong>Sizes:</strong> Vectors (<em>PSD, SVG included</em>) <strong>Total Icons:</strong> 171 <strong>Download: </strong><a
href="http://somerandomdude.com/work/iconic/" target="_blank">Click here</a></p><h2 style="text-align: left;">Small Sized UI type icon:</h2><h3 style="text-align: left;">#6: Minicons Icon Set</h3><p
style="text-align: center;"><a
href="http://jospinoj.deviantart.com/art/minicons-292943928" target="_blank"><img
class="aligncenter size-full wp-image-1104" title="minicons-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/minicons-icon-set.png" alt="" width="550" height="150" /></a><strong>Sizes:</strong> Vectors (<em>PSD included</em>) <strong>Total Icons:</strong> 100+ <strong>Download: </strong><a
href="http://jospinoj.deviantart.com/art/minicons-292943928" target="_blank">Click here</a></p><h3 style="text-align: left;">#7: Wireframe Toolbar Icons</h3><p
style="text-align: center;"><a
href="http://gentleface.com/free_icon_set.html" target="_blank"><img
class="aligncenter size-full wp-image-1107" title="wireframe-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/wireframe-icon-set.png" alt="" width="550" height="150" /></a><strong>Sizes:</strong> 16&#215;16, 24&#215;24, 32&#215;32, 48&#215;48 <strong>Total Icons: </strong>147 <strong>Download: </strong><a
href="http://gentleface.com/free_icon_set.html" target="_blank">Click here</a></p><h3 style="text-align: left;">#8: Dusseldorf Icon Set</h3><p
style="text-align: center;"><a
href="http://pc.de/icons/#Dusseldorf" target="_blank"><img
class="aligncenter size-full wp-image-1099" title="dusseldorf-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/dusseldorf-icon-set.png" alt="" width="550" height="150" /></a><strong>Sizes:</strong> 16&#215;16, 32&#215;32, 48&#215;48 <strong>Total Icons: </strong>100+ <strong>Download: </strong><a
href="http://pc.de/icons/#Dusseldorf" target="_blank">Click here</a></p><h3 style="text-align: left;">#9: Glyphish Icon Set</h3><p
style="text-align: center;"><a
href="http://glyphish.com/" target="_blank"><img
class="aligncenter size-full wp-image-1102" title="glyphish-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/glyphish-icon-set.png" alt="" width="550" height="150" /></a><strong>Sizes:</strong> 28&#215;28 (<em>PSD included</em>) <strong>Total Icons: </strong>100+ <strong>Download: </strong><a
href="http://glyphish.com/" target="_blank">Click here</a></p><h3 style="text-align: left;">#10: Glyphicons Icon Set</h3><p
style="text-align: center;"><a
href="http://glyphicons.com/" target="_blank"><img
class="aligncenter size-full wp-image-1101" title="glyphicons-icon-set" src="http://www.intechgrity.com/wp-content/uploads/2012/05/glyphicons-icon-set.png" alt="" width="550" height="150" /></a><strong>Sizes:</strong> 24&#215;24 <strong
style="text-align: center;">Total Icons: </strong>400 <strong>Download: </strong><a
href="http://glyphicons.com/" target="_blank">Click here</a></p><p
style="text-align: left;">So that was all. If you are aware of something better, or wish to see your own icon sets in this list, do drop a comment.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/15-free-web-icons-background/' rel='bookmark' title='15+ Free Web-Icons &amp; Background Patterns/Texture download sites to boost your Blog &amp; Website designing'>15+ Free Web-Icons &amp; Background Patterns/Texture download sites to boost your Blog &amp; Website designing</a></li><li><a
href='http://www.intechgrity.com/huge-collection-of-css-js-jquery/' rel='bookmark' title='Huge collection of CSS, JS &amp; jQuery Dropdown Navigation menus for FREE &#124; Use on your Web site or Blog'>Huge collection of CSS, JS &amp; jQuery Dropdown Navigation menus for FREE &#124; Use on your Web site or Blog</a></li><li><a
href='http://www.intechgrity.com/create-ajax-style-loading-animated-gif/' rel='bookmark' title='Create Ajax style Loading Animated Gif images easily with 3 Free Online Generators'>Create Ajax style Loading Animated Gif images easily with 3 Free Online Generators</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=0cdSI8PUwIE:5Ez12y9RKGI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=0cdSI8PUwIE:5Ez12y9RKGI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=0cdSI8PUwIE:5Ez12y9RKGI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=0cdSI8PUwIE:5Ez12y9RKGI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=0cdSI8PUwIE:5Ez12y9RKGI:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=0cdSI8PUwIE:5Ez12y9RKGI:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/0cdSI8PUwIE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/10-free-minimalist-grey-scale-icon-sets-for-your-web-design/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.intechgrity.com/10-free-minimalist-grey-scale-icon-sets-for-your-web-design/</feedburner:origLink></item> <item><title>Howto: Choose the right web hosting company for your business</title><link>http://feedproxy.google.com/~r/greentechspot/~3/Hpnqz_tqpXQ/</link> <comments>http://www.intechgrity.com/howto-choose-the-right-web-hosting-company-for-your-business/#comments</comments> <pubDate>Tue, 15 May 2012 10:43:41 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Webhosts n Domains]]></category> <category><![CDATA[Web Hosting]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1072</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/howto-choose-the-right-web-hosting-company-for-your-business/">Howto: Choose the right web hosting company for your business</a>">here</a></p><p>We have discussed a lot about web hosting and domains previously. One of the most important thing to consider while buying any web host is, undoubtedly, the value for money. Everyone wants to choose the right web hosting company for his/her business. Now, this is easy to say than to actually compare. With so many [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/use-pickaweb-domain-hosting-to-buy/' rel='bookmark' title='Use PickaWeb Domain Hosting to buy Domains and Web Host at reasonable price'>Use PickaWeb Domain Hosting to buy Domains and Web Host at reasonable price</a></li><li><a
href='http://www.intechgrity.com/choose-best-web-host-for-you-using/' rel='bookmark' title='Choose the Best Web Host for you using WebHostingGeeks.com'>Choose the Best Web Host for you using WebHostingGeeks.com</a></li><li><a
href='http://www.intechgrity.com/choose-your-best-webhost-using/' rel='bookmark' title='Choose your Best Webhost using AlreadyHosting.com ~ One of the best webhosting review provider'>Choose your Best Webhost using AlreadyHosting.com ~ One of the best webhosting review provider</a></li><li><a
href='http://www.intechgrity.com/best-and-affordable-alternatives-of/' rel='bookmark' title='The best and affordable alternatives of Google Page Hosting'>The best and affordable alternatives of Google Page Hosting</a></li><li><a
href='http://www.intechgrity.com/host-your-website-using-one-of-uks-top/' rel='bookmark' title='Host your Website using one of UK’s top WebHosting sites-JustHost [review]'>Host your Website using one of UK’s top WebHosting sites-JustHost [review]</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/pD-xAnu9fIV-6A2vcBe6oSMQ98k/0/da"><img src="http://feedads.g.doubleclick.net/~a/pD-xAnu9fIV-6A2vcBe6oSMQ98k/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pD-xAnu9fIV-6A2vcBe6oSMQ98k/1/da"><img src="http://feedads.g.doubleclick.net/~a/pD-xAnu9fIV-6A2vcBe6oSMQ98k/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/howto-choose-the-right-web-hosting-company-for-your-business/">Howto: Choose the right web hosting company for your business</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/webhosting.png"><img
class="alignright  wp-image-1081" title="webhosting" src="http://www.intechgrity.com/wp-content/uploads/2012/05/webhosting.png" alt="" width="250" height="250" /></a>We have discussed a lot about <a
href="http://www.intechgrity.com/category/internet/webhosts-n-domains/">web hosting and domains</a> previously. One of the most important thing to consider while buying any web host is, undoubtedly, the <em>value for money</em>. Everyone wants to choose the right web hosting company for his/her business. Now, this is easy to say than to actually compare. With so many parameters of a reliable web host, so many features of a good web server, it just becomes difficult to choose among the &#8220;best&#8221;s. Luckily, there are services in the Internet which makes our job a lot easier. By looking into comparison tables, reading expert reviews and statistical analysis, going through highlighted features, we can get a better idea before putting money behind a web host. But still, you should have the basic (and well a little advanced won&#8217;t hurt) idea of today&#8217;s server technologies and cost for a profitable long term investment. So, we are going to discuss, what you need to consider before buying any web host. We shall also see some of the reviewing websites from where we can get aggregated information.</p><h2>Things to consider before buying:</h2><p>Out of my own experiences, I think, one should consider the following constrains while buying any web host:</p><h3><strong>#1: Offer Price &amp; Renewal Price:</strong></h3><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/offer-price-renewal-price.png"><img
class="aligncenter size-full wp-image-1077" title="offer-price-renewal-price" src="http://www.intechgrity.com/wp-content/uploads/2012/05/offer-price-renewal-price.png" alt="" width="550" height="150" /></a></p><p>The first thing that should come into mind is, indeed the money. After all you are investing something for your company, so you want to be sure that you get the maximum out of it! Most web hosting services out there offers very less price while buying, and then the renewal price becomes higher. Mostly people overlook this thing. We buy using some promo code and then after a year or two, when the renewal time comes, we have to spend rather a lot to continue the service.<strong></strong></p><h3><strong>#2: Specifications &amp; Features:</strong></h3><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/specifications-features.png"><img
class="aligncenter size-full wp-image-1079" title="specifications-features" src="http://www.intechgrity.com/wp-content/uploads/2012/05/specifications-features.png" alt="" width="550" height="150" /></a></p><p>The most concerned features and specs are, perhaps, diskspace, bandwidth and number of addon domains. The more number of addon domains you have, more number of sites you can host. So, you know that you not just put money behind a single website hosting, rather a multi website hosting. Now a days, most good web hosts give unlimited addon domains.</p><ul><li><strong>Technical Limitations:</strong> Apart from the unlimited stuffs, you should look into the technical limitations. Some host might give you unlimited bandwidth at the cost of limited domains and vice versa. Just keep in mind, what do you really need from your host.</li><li><strong>Domains, Addon Domains, Parked Domains &amp; Sub domains:</strong> The main domain is the domain which is registered primarily with your web host. The addon domains are other domains which you can add and host using the same web host. The parked domains are &#8220;parked&#8221; on some directory of your existing sites. If someone enters the URL of the parked domain, then it will show that directory (and the index file if present). Now sub domains are the children of your main domain. Say, you have<em> mydomain.com</em> as your main domain or addon domain. Then, <em>mysubdomain.mydomain.com</em> would be a sub domain. Now a days, no limits are imposed on all these, which means, with a good web host, you can host unlimited domains with unlimited sub-domains.</li><li><strong>Domain portability: </strong>Another thing you look into is, domain portability. Some hosts out there force you to register your domain with them. Personally, I don&#8217;t prefer to do business with such web hosting companies. What if I want to move my sites to another host? Web hosts which gives the flexibility to add your already registered domain via the name servers, are always my first preferences.</li><li><strong>Bandwidth:</strong> The bigger the value, more visitors your webhost can support. With unlimited bandwidth, this is not an issue in shared hosting. But if you are going for VPS or VPN or Cloud, then you should look into the bandwidth and consider it seriously with respect to the number of visitors in your site. An average site, with around 1500-2000 page visits per day, might consume upto 100GB per month, depending on the data provided by the site. (This figure is mainly for image heavy sites, a text heavy site will actually consume much less bandwidth). You can use tools like <a
href="http://getfirebug.com/" target="_blank">Firebug</a> to check out the size of your pages and estimate average bandwidth by multiplying it with your daily average page visits.</li><li><strong>Disk Usage: </strong>Larger value indicates bigger possibilities for storing audio, video and image files. But also keep in mind that a nice disk usage limitation is useless without a nicer Bandwidth cap. So choose wisely. You won&#8217;t want to have a web host with 100 GB diskspace yet only 10 GB bandwidth per month.</li><li><strong>Server Technology: </strong>The technology depends explicitly on your requirement. If you want to publish a .net or aspx site, then you would prefer <em>Windows Hosting</em>. Where as, if you are familiar with Open source technologies like PHP, MySQL etc, then <em>Linux Hosting</em> should be your first choice. Some of you might want to have several other technologies like <em>Ruby on Rails, Perl, CGI</em> etc.</li><li><strong>Website builder and Auto Installer script:</strong> Now, not everyone of us buy a webhost to create their sites from scratch or install CMS at their own. There are services like <a
href="http://www.softaculous.com/" target="_blank">softaculous</a>, <a
href="https://netenberg.com/fantastico.php" target="_blank">fantastico</a> etc which makes our job even easier. They provide a one click installer for popular CMS like WordPress, Joomla etc. All of the good web hosts should provide atleast one of these auto-installer scripts.</li><li><strong>Emails &amp; Accounts:</strong> If you want to access your email directly within your host, this should be your concern. Personally, I prefer using <a
title="Register your Domain with Google Apps – Beginners Tutorial on setting up the DNS part and URL forwarding" href="http://www.intechgrity.com/register-your-domain-with-google-apps/">google apps to setup my email accounts</a> for the domain. But this feature should not be overlooked.</li><li><strong>SSH Access: </strong>If you know what this feature is, then you probably want your web host to support your feature. For those, who doesn&#8217;t know, <a
href="http://support.hostgator.com/articles/hosting-guide/lets-get-started/how-do-i-get-and-use-ssh-access" target="_blank">here</a> <a
href="http://support.suso.com/supki/SSH_Tutorial_for_Linux" target="_blank">are</a> <a
href="http://wtfh4x.wordpress.com/2010/11/27/25-useful-ssh-commands/" target="_blank">some</a> articles I found from the google explaining the importance and advantages of this feature. Note that for average usage, you won&#8217;t need this.</li></ul><h3><strong>#3: Server Performance &amp; Uptime:</strong></h3><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/serveruptime.png"><img
class="aligncenter size-full wp-image-1078" title="serveruptime" src="http://www.intechgrity.com/wp-content/uploads/2012/05/serveruptime.png" alt="" width="550" height="150" /></a></p><p>You don&#8217;t want your website to be down. There are already services which does the job for us. But in most of the times the information provided is bloated with just too many details. The thing which one should look while choosing a good web host, is, the average uptime for the target region. Which means, if your target visitors are from US, then obviously you should not come into any conclusion from the UK&#8217;s uptime.</p><h3>#4: Technical Support:</h3><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/technical-support.png"><img
class="aligncenter size-full wp-image-1080" title="technical-support" src="http://www.intechgrity.com/wp-content/uploads/2012/05/technical-support.png" alt="" width="550" height="150" /></a></p><p>The next most important thing, is perhaps, how much support and technical help your web host can give. Mostly, now a days, big web hosting companies like Hostgator, Fatcow, BlueHost etc, have their dedicated support forum for their customers. Also, they give 24X7 toll free numbers where you can call and easily get help, regardless of your location. So, you should consider this aspect as well. If your site goes down, then there should be someone to recover.</p><h3>#5: Compliance:</h3><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/compliance.png"><img
class="aligncenter size-full wp-image-1076" title="compliance" src="http://www.intechgrity.com/wp-content/uploads/2012/05/compliance.png" alt="" width="550" height="150" /></a></p><p>When you are using any external web host to host your site, then you should be aware of a few things. Because after all, it is your web host who is finally showing your site to the world. I would recommend to go through it at least once, before buying any web host. It is generally found at their Policy.</p><h2>Doing your own researches:</h2><p>Recently I was looking into fatcow and bluehost reviews in the internet. As an experienced blogger and developer, I am already aware of these two web hosting services and I used to think bluehost is any day better than its competitors because, it has spend more time in the business. But, surprisingly, my concepts changed as soon as I looked into those reviews. Here are the few of the points I noticed:</p><ul><li><a
href="http://www.webhostingsearch.com/review/fatcow" target="_blank">Fatcow will give a 24 months plan at only $3.67/month</a> (at the time of the review). Now that&#8217;s quite a lot of saving. Whereas, for BlueHost it is $19.95/month (although it is for reseller plan, but I prefer to stick to normal plans).</li><li>For <a
href="http://www.webhostingsearch.com/review/bluehost" target="_blank">bluehost, it is $19.94/month for reseller plan</a>. I could not found any offer on normal plans. They have 100GB diskspace, 15Mbps bandwidth, unlimited accounts, but they did not provide any easy to get information on bandwidth cap.</li><li>Fatcow gives free domain &amp; setup along with 75$ marketing credits. Also, just like bluehost it has unlimited addon domains along with unlimited diskspace and bandwidth.</li><li>User recommendation of Fatcows beats BlueHost by 35%. For big web hosting companies like this, 35% is really something to consider.</li></ul><p>So, from all these points, I could figure out that, had I wanted a reseller web host, I would have chosen BlueHost, but, as my needs are limited, so FatCow gives the best solution for me at a very reasonable, rather cheap price.</p><p>I hope you have found this review useful, and it will help you find the best web hosting for you. If you have any doubt, feel free to comment. I will be more than happy to help you.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/use-pickaweb-domain-hosting-to-buy/' rel='bookmark' title='Use PickaWeb Domain Hosting to buy Domains and Web Host at reasonable price'>Use PickaWeb Domain Hosting to buy Domains and Web Host at reasonable price</a></li><li><a
href='http://www.intechgrity.com/choose-best-web-host-for-you-using/' rel='bookmark' title='Choose the Best Web Host for you using WebHostingGeeks.com'>Choose the Best Web Host for you using WebHostingGeeks.com</a></li><li><a
href='http://www.intechgrity.com/choose-your-best-webhost-using/' rel='bookmark' title='Choose your Best Webhost using AlreadyHosting.com ~ One of the best webhosting review provider'>Choose your Best Webhost using AlreadyHosting.com ~ One of the best webhosting review provider</a></li><li><a
href='http://www.intechgrity.com/best-and-affordable-alternatives-of/' rel='bookmark' title='The best and affordable alternatives of Google Page Hosting'>The best and affordable alternatives of Google Page Hosting</a></li><li><a
href='http://www.intechgrity.com/host-your-website-using-one-of-uks-top/' rel='bookmark' title='Host your Website using one of UK’s top WebHosting sites-JustHost [review]'>Host your Website using one of UK’s top WebHosting sites-JustHost [review]</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Hpnqz_tqpXQ:yZVW8I9kUig:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Hpnqz_tqpXQ:yZVW8I9kUig:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Hpnqz_tqpXQ:yZVW8I9kUig:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Hpnqz_tqpXQ:yZVW8I9kUig:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=Hpnqz_tqpXQ:yZVW8I9kUig:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=Hpnqz_tqpXQ:yZVW8I9kUig:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/Hpnqz_tqpXQ" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/howto-choose-the-right-web-hosting-company-for-your-business/feed/</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://www.intechgrity.com/howto-choose-the-right-web-hosting-company-for-your-business/</feedburner:origLink></item> <item><title>Login, Logout and Administrate using PHP SESSION, COOKIE &amp; MySQL – Version 2 with remember me option</title><link>http://feedproxy.google.com/~r/greentechspot/~3/W4WCMKlEgIg/</link> <comments>http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/#comments</comments> <pubDate>Fri, 11 May 2012 18:49:05 +0000</pubDate> <dc:creator>Swashata</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <category><![CDATA[php-cookie]]></category> <category><![CDATA[php-post]]></category> <category><![CDATA[php-session]]></category><guid isPermaLink="false">http://www.intechgrity.com/?p=1052</guid> <description><![CDATA[<p><p>Original post published <a
href="<a href="http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/">Login, Logout and Administrate using PHP SESSION, COOKIE &#038; MySQL &#8211; Version 2 with remember me option</a>">here</a></p><p>So, our last post on creating login admin and logout seems to be pretty backdated. We decided to come up with a new post under the same topic. So, to start with, let us see what you will be able to do after following this tutorial: Create a MySQL database for managing admin users. Use [...]</p></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p> Related posts:<ol><li><a
href='http://www.intechgrity.com/create-login-admin-logout-page-in-php-w/' rel='bookmark' title='Create Login Admin &amp; Logout Page in PHP w/ SESSION n MySQL'>Create Login Admin &amp; Logout Page in PHP w/ SESSION n MySQL</a></li><li><a
href='http://www.intechgrity.com/get-parent-directory-of-current-url/' rel='bookmark' title='Get Parent directory of Current URL using PHP dirname function'>Get Parent directory of Current URL using PHP dirname function</a></li><li><a
href='http://www.intechgrity.com/how-to-connect-to-xamppmysql-using/' rel='bookmark' title='How to Connect to XAMPP/MySQL using Windows CMD [Command Prompt]'>How to Connect to XAMPP/MySQL using Windows CMD [Command Prompt]</a></li><li><a
href='http://www.intechgrity.com/jquery-recent-post-widget-for/' rel='bookmark' title='jQuery Recent Post Widget for BlogSpot/Blogger with Filter by Tag Option'>jQuery Recent Post Widget for BlogSpot/Blogger with Filter by Tag Option</a></li><li><a
href='http://www.intechgrity.com/understanding-the-basic-of-html-form-and-php-get-and-post-method/' rel='bookmark' title='Understanding the basic of HTML form and PHP GET and POST method'>Understanding the basic of HTML form and PHP GET and POST method</a></li></ol>]]></description> <content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/LQKmq-7ly8GBBAzW_MfvGYCSzgw/0/da"><img src="http://feedads.g.doubleclick.net/~a/LQKmq-7ly8GBBAzW_MfvGYCSzgw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/LQKmq-7ly8GBBAzW_MfvGYCSzgw/1/da"><img src="http://feedads.g.doubleclick.net/~a/LQKmq-7ly8GBBAzW_MfvGYCSzgw/1/di" border="0" ismap="true"></img></a></p><p>Original post published <a
href="<a href="http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/">Login, Logout and Administrate using PHP SESSION, COOKIE &#038; MySQL &#8211; Version 2 with remember me option</a>">here</a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/secured-login3.png"><img
class="alignright size-medium wp-image-1057" title="secured login[3]" src="http://www.intechgrity.com/wp-content/uploads/2012/05/secured-login3-300x168.png" alt="" width="300" height="168" /></a>So, our last post on <a
title="Create Login Admin &amp; Logout Page in PHP w/ SESSION n MySQL" href="http://www.intechgrity.com/create-login-admin-logout-page-in-php-w/" target="_blank">creating login admin and logout</a> seems to be pretty backdated. We decided to come up with a new post under the same topic. So, to start with, let us see what you will be able to do after following this tutorial:</p><ul><li>Create a MySQL database for managing admin users. Use SHA1 encryption method to store the password in database. Also insert data into it.</li><li>Create your own login/logout system in PHP using Cookie and Session along with remember me option. We shall md5 the SHA1 password in cookie for better protection.</li><li>Understand the PHP code behind the scene. How we have stored the encrypted password in the cookie.</li><li>Implementing the whole thing in OOP (Object Oriented Programming).</li></ul><p>In this tutorial we will do only the basic stuffs, there will be no CSS/HTML styling. In our next release, we will provide you a package with HTML5 admin, from where you can download and use it directly in your projects. This tutorial is intended to explain the basic mechanism used in Login Systems and how cookies and sessions work to provide the administrator security. We will discuss the file and directory structure, the database structure, we will see how we&#8217;ve used MySQL in PHP to connect to database and how we actually authenticate the user for a single session or for multiple session (in case the remember me option is selected). Check out below to get the codes straight away if you know how to do this already and just want to get the code.</p><div
class="demo-download lonely"><a
class="download" title="Login Logout System Downloaded 348" href="http://www.intechgrity.com/downloads/13">Download Now</a></div><p>As for the so called licensing stuffs, this whole thing is just <strong>FREE</strong>. You download, fork, modify, redistribute, use in your project, or do whatever you want. Although a credit will be appreciated, but in this open world of internet, it is completely upto you.</p><p>So, let us start with the directory structure and understand which file does what.</p><h2>#0: File and directory structure:</h2><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/file-structure-login-logout.png"><img
class="aligncenter size-full wp-image-1059" title="file-structure-login-logout" src="http://www.intechgrity.com/wp-content/uploads/2012/05/file-structure-login-logout.png" alt="" width="209" height="199" /></a></p><ul><li><strong>directory &#8211; admin: </strong>The root directory of all your admin files. It contains all the classes, login/logout files etc that we need to get the system running.<ul><li><strong>admin-class.php</strong> &#8211; The main library class of the admin. It contains the several library functions that we need for user authentication, password checking etc.</li><li><strong>index.php</strong> &#8211; The main admin file which should be restricted to the logged in administrator only. If not logged in, then this should redirect to the login page.</li><li><strong>login-action.php</strong> &#8211; The action file which processes the user submitted username and password and authenticates on success. On failure, it redirects to the login page.</li><li><strong>login.php</strong> &#8211; The login form where users are asked to enter their username, password etc along with the a nice remember me option.</li><li><strong>logout.php</strong> &#8211; The logout script which should clear the authentication data and send back the user to the login page.</li></ul></li><li><strong>directory &#8211; db:</strong> The root directory for the database connection files. We have used <a
href="http://justinvincent.com/ezsql" target="_blank">ezSQL</a>to code our application. It gives several query abstractions for faster access.<ul><li><strong>db.php </strong>- It includes the library files <strong>ez_sql_core.php </strong>&amp; <strong>ez_sql_mysql.php</strong> and initializes a global variable <strong>$db</strong> for all the database connections. We shall look more about this later.</li><li><strong>ez_sql_core.php</strong> &#8211; The core ez_sql file as downloaded from the link above.</li><li><strong>ez_sql_mysql.php </strong>- The MySQL extension for the ezSQL.</li></ul></li></ul><h2>#1.1: Creating the database:</h2><p>We shall start by creating a new database for our application. If you are on linux and have MySQL installed, just open up terminal and hit <span
class="code">mysql</span> (you need to access with super user privilege if you have not changed the mysql root, <span
class="code">su mysql</span> or <span
class="code">sudo mysql</span>) and enter the following sql queries.</p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/database-login-logout.png"><img
class="aligncenter size-medium wp-image-1060" title="database-login-logout" src="http://www.intechgrity.com/wp-content/uploads/2012/05/database-login-logout-300x190.png" alt="" width="300" height="190" /></a></p><pre class="brush: sql; title: ; notranslate">
CREATE DATABASE login_test;
USE login_test;

CREATE TABLE `user` (
`id` INT NOT NULL auto_increment,
`username` VARCHAR(50) NOT NULL default '',
`nicename` VARCHAR(255) NOT NULL default '',
`email` VARCHAR(255) NOT NULL default '',
`password` VARCHAR(255) NOT NULL default '',
UNIQUE KEY `user_n` (`username`),
UNIQUE KEY `user_e` (`email`),
    PRIMARY KEY (`id`)
);

INSERT INTO `user` (`username`, `nicename`, `email`, `password`)
VALUES (
'swashata', 'Swashata Ghosh', 'abc@domain.com', SHA1('pass')
);
</pre><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/database-table-login-logout.png"><img
class="aligncenter size-medium wp-image-1061" title="database-table-login-logout" src="http://www.intechgrity.com/wp-content/uploads/2012/05/database-table-login-logout-300x257.png" alt="" width="300" height="257" /></a></p><p>Let us see what it does:</p><ol><li>Creates a database named <span
class="spanlight">login_test</span>.</li><li>Use that database.</li><li>Create a table <span
class="spanlight">user</span> with id, username, nicename, email and password as fields. Set usename and email as unique keys and id as primary key.</li><li> Insert a user named swashata with nicename Swashata Ghosh, email as above and password as pass.</li><li>Note that we have encrypted the password &#8216;<span
class="spanlight">pass</span>&#8216; using the SHA1 encryption method provided my MySQL. In this way our password is stored in encrypted form. But that also means, you will never be able to see it in original form again. So, how do you reset it? Simple, by querying the database directly. Just</li></ol><p>Now, if you run a SELECT * query, then you will see something like this.</p><p>All done, now let us code the PHP backend.</p><h2>#1.2: Connecting the database with PHP: &#8211; db.php</h2><p>Now, we need to connect this database to our script. To do this, we will be using ezSQL as mentioned before. So, inside our db.php file, we place the following code:</p><pre class="brush: php; title: ; notranslate">
&lt;?php
/**
* The db.php file which initiates a connection to the database
* and gives a global $db variable for access
* @author Swashata &lt;swashata@intechgrity.com&gt;
* @uses ezSQL MySQL
*/
/** edit your configuration */
$dbuser = 'db_user';
$dbname = 'db_name';
$dbpassword = 'db_password';
$dbhost = 'localhost';

/** Stop editing from here, else you know what you are doing <img src='http://www.intechgrity.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  */

/** defined the root for the db */
if(!defined('ADMIN_DB_DIR'))
define('ADMIN_DB_DIR', dirname(__FILE__));

require_once ADMIN_DB_DIR . '/ez_sql_core.php';
require_once ADMIN_DB_DIR . '/ez_sql_mysql.php';
global $db;
$db = new ezSQL_mysql($dbuser, $dbpassword, $dbname, $dbhost);
</pre><p>As you can see, it creates a global $db object which connects to the database from the credential above. To use it, we just need to include this file and declare $db as global.</p><h2>#2: Creating the Login form: &#8211; login.php</h2><p>So, we have used a plain and simple login form with valid xhtml markup. You can add your own css to it to make it look better. The things to notice are</p><ul><li>The name and id of the fields should not change.</li><li>The form method should be post and action should be login-action.php.</li></ul><pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Login to admin&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action=&quot;login-action.php&quot; method=&quot;post&quot;&gt;
            &lt;fieldset&gt;
                &lt;legend&gt;Enter Credential&lt;/legend&gt;
                    &lt;p&gt;
                        &lt;label for=&quot;username&quot;&gt;Username: &lt;/label&gt;
                        &lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; value=&quot;&quot; /&gt;
                    &lt;/p&gt;
                    &lt;p&gt;
                        &lt;label for=&quot;password&quot;&gt;Password: &lt;/label&gt;
                        &lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; value=&quot;&quot; /&gt;
                    &lt;/p&gt;
                    &lt;p&gt;
                        &lt;label for=&quot;remember&quot;&gt;
                            &lt;input type=&quot;checkbox&quot; name=&quot;remember&quot; id=&quot;remember&quot; value=&quot;1&quot; /&gt; Remember me
                        &lt;/label&gt;
                    &lt;/p&gt;
            &lt;/fieldset&gt;
            &lt;p&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt; &lt;input type=&quot;reset&quot; value=&quot;Reset&quot; /&gt;
            &lt;/p&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre><p>Simple and effective.</p><h2>#3: Creating the library classes &#8211; admin-class.php</h2><p>We first need to analyze what features we need for our library. The basic features are (and also the code behind it):</p><p>#3.0: Initialize the session for login system:</p><p>Login systems work mainly either on session or on cookie. As we are using remember me option, so it is better to use both. Here is how we initialize the session and do some other stuffs.</p><pre class="brush: php; title: ; notranslate">
    /**
     * The constructor function of admin class
     * We do just the session start
     * It is necessary to start the session before actually storing any value
     * to the super global $_SESSION variable
     */
    public function __construct() {
        session_start();

        //store the absolute script directory
        //note that this is not the admin directory
        self::$abs_path = dirname(dirname(__FILE__));

        //initialize the post variable
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this-&gt;post = $_POST;
            if(get_magic_quotes_gpc ()) {
                //get rid of magic quotes and slashes if present
                array_walk_recursive($this-&gt;post, array($this, 'stripslash_gpc'));
            }
        }

        //initialize the get variable
        $this-&gt;get = $_GET;
        //decode the url
        array_walk_recursive($this-&gt;get, array($this, 'urldecode'));
    }
</pre><h3>#3.1: Checking the database for valid username and password combination:</h3><pre class="brush: php; title: ; notranslate">
    /**
     * Check the database for login user
     * Get the password for the user
     * compare md5 hash over sha1
     * @param string $username Raw username
     * @param string $password expected to be md5 over sha1
     * @return bool TRUE on success FALSE otherwise
     */
    private function _check_db($username, $password) {
        global $db;
        $user_row = $db-&gt;get_row(&quot;SELECT * FROM `user` WHERE `username`='&quot; . $db-&gt;escape($username) . &quot;'&quot;);

        //general return
        if(is_object($user_row) &amp;&amp; md5($user_row-&gt;password) == $password)
            return true;
        else
            return false;
    }
</pre><p>So, what we do is, we compare the <span
class="code">md5</span> and <span
class="code">sha1</span> encrypted password with the one stored in database. As the database has only sha1 encrypted password, so we calculate the md5 hash by passing it through the function md5. If it matches, then we return true else, false. You might think, from where we get the username and password. Just keep on reading.</p><h2>#3.2: Add action for the login-action.php file:</h2><pre class="brush: php; title: ; notranslate">
    /**
     * Check for login in the action file
     */
    public function _login_action() {

        //insufficient data provided
        if(!isset($this-&gt;post['username']) || $this-&gt;post['username'] == '' || !isset($this-&gt;post['password']) || $this-&gt;post['password'] == '') {
            header (&quot;location: login.php&quot;);
        }

        //get the username and password
        $username = $this-&gt;post['username'];
        $password = md5(sha1($this-&gt;post['password']));

        //check the database for username
        if($this-&gt;_check_db($username, $password)) {
            //ready to login
            $_SESSION['admin_login'] = $username;

            //check to see if remember, ie if cookie
            if(isset($this-&gt;post['remember'])) {
                //set the cookies for 1 day, ie, 1*24*60*60 secs
                //change it to something like 30*24*60*60 to remember user for 30 days
                setcookie('username', $username, time() + 1*24*60*60);
                setcookie('password', $password, time() + 1*24*60*60);
            } else {
                //destroy any previously set cookie
                setcookie('username', '', time() - 1*24*60*60);
                setcookie('password', '', time() - 1*24*60*60);
            }

            header(&quot;location: index.php&quot;);
        }
        else {
            header (&quot;location: login.php&quot;);
        }

        die();
    }
</pre><p>What it does is, calls the <span
class="code">_check_db</span> function with the username and encrypted password as parameters. If true is returned, then it sets the session for the admin with the username as <span
class="code">$_SESSION['admin_login']</span> variable. Then it further checks for remember me option, and if yes, then it sets cookie as well. We shall discuss more about cookie in some other post. For now, you can just check the comments. As it says, it stores the username and password in the corresponding variables for 1 day. To fetch the cookie we shall use <span
class="code">$_COOKIE</span> super global.</p><h3>#3.3: Creating the authentication method:</h3><pre class="brush: php; title: ; notranslate">
    /**
     * Checks whether the user is authenticated
     * to access the admin page or not.
     *
     * Redirects to the login.php page, if not authenticates
     * otherwise continues to the page
     *
     * @access public
     * @return void
     */
    public function _authenticate() {
        //first check whether session is set or not
        if(!isset($_SESSION['admin_login'])) {
            //check the cookie
            if(isset($_COOKIE['username']) &amp;&amp; isset($_COOKIE['password'])) {
                //cookie found, is it really someone from the
                if($this-&gt;_check_db($_COOKIE['username'], $_COOKIE['password'])) {
                    $_SESSION['admin_login'] = $_COOKIE['username'];
                    header(&quot;location: index.php&quot;);
                    die();
                }
                else {
                    header(&quot;location: login.php&quot;);
                    die();
                }
            }
            else {
                header(&quot;location: login.php&quot;);
                die();
            }
        }
    }
</pre><p>When ever we need to authenticate in a file access, we just have to call this function. We shall see it in the index.php file. It automatically fetches the data from cookie, revalidates it and also redirects to the login.php in case of failure. This is just pretty much what you need.</p><p>#3.4: Putting together the class file &#8211; admin-class.php</p><pre class="brush: php; collapse: true; light: false; title: ; toolbar: true; notranslate">
&lt;?php
/** Include the database file */
include_once '../db/db.php';
/**
 * The main class of login
 * All the necesary system functions are prefixed with _
 * examples, _login_action - to be used in the login-action.php file
 * _authenticate - to be used in every file where admin restriction is to be inherited etc...
 * @author Swashata &lt;swashata@intechgrity.com&gt;
 */
class itg_admin {

    /**
     * Holds the script directory absolute path
     * @staticvar
     */
    static $abs_path;

    /**
     * Store the sanitized and slash escaped value of post variables
     * @var array
     */
    var $post = array();

    /**
     * Stores the sanitized and decoded value of get variables
     * @var array
     */
    var $get = array();

    /**
     * The constructor function of admin class
     * We do just the session start
     * It is necessary to start the session before actually storing any value
     * to the super global $_SESSION variable
     */
    public function __construct() {
        session_start();

        //store the absolute script directory
        //note that this is not the admin directory
        self::$abs_path = dirname(dirname(__FILE__));

        //initialize the post variable
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this-&gt;post = $_POST;
            if(get_magic_quotes_gpc ()) {
                //get rid of magic quotes and slashes if present
                array_walk_recursive($this-&gt;post, array($this, 'stripslash_gpc'));
            }
        }

        //initialize the get variable
        $this-&gt;get = $_GET;
        //decode the url
        array_walk_recursive($this-&gt;get, array($this, 'urldecode'));
    }

    /**
     * Sample function to return the nicename of currently logged in admin
     * @global ezSQL_mysql $db
     * @return string The nice name of the user
     */
    public function get_nicename() {
        $username = $_SESSION['admin_login'];
        global $db;
        $info = $db-&gt;get_row(&quot;SELECT `nicename` FROM `user` WHERE `username` = '&quot; . $db-&gt;escape($username) . &quot;'&quot;);
        if(is_object($info))
            return $info-&gt;nicename;
        else
            return '';
    }

    /**
     * Sample function to return the email of currently logged in admin user
     * @global ezSQL_mysql $db
     * @return string The email of the user
     */
    public function get_email() {
        $username = $_SESSION['admin_login'];
        global $db;
        $info = $db-&gt;get_row(&quot;SELECT `email` FROM `user` WHERE `username` = '&quot; . $db-&gt;escape($username) . &quot;'&quot;);
        if(is_object($info))
            return $info-&gt;email;
        else
            return '';
    }

    /**
     * Checks whether the user is authenticated
     * to access the admin page or not.
     *
     * Redirects to the login.php page, if not authenticates
     * otherwise continues to the page
     *
     * @access public
     * @return void
     */
    public function _authenticate() {
        //first check whether session is set or not
        if(!isset($_SESSION['admin_login'])) {
            //check the cookie
            if(isset($_COOKIE['username']) &amp;&amp; isset($_COOKIE['password'])) {
                //cookie found, is it really someone from the
                if($this-&gt;_check_db($_COOKIE['username'], $_COOKIE['password'])) {
                    $_SESSION['admin_login'] = $_COOKIE['username'];
                    header(&quot;location: index.php&quot;);
                    die();
                }
                else {
                    header(&quot;location: login.php&quot;);
                    die();
                }
            }
            else {
                header(&quot;location: login.php&quot;);
                die();
            }
        }
    }

    /**
     * Check for login in the action file
     */
    public function _login_action() {

        //insufficient data provided
        if(!isset($this-&gt;post['username']) || $this-&gt;post['username'] == '' || !isset($this-&gt;post['password']) || $this-&gt;post['password'] == '') {
            header (&quot;location: login.php&quot;);
        }

        //get the username and password
        $username = $this-&gt;post['username'];
        $password = md5(sha1($this-&gt;post['password']));

        //check the database for username
        if($this-&gt;_check_db($username, $password)) {
            //ready to login
            $_SESSION['admin_login'] = $username;

            //check to see if remember, ie if cookie
            if(isset($this-&gt;post['remember'])) {
                //set the cookies for 1 day, ie, 1*24*60*60 secs
                //change it to something like 30*24*60*60 to remember user for 30 days
                setcookie('username', $username, time() + 1*24*60*60);
                setcookie('password', $password, time() + 1*24*60*60);
            } else {
                //destroy any previously set cookie
                setcookie('username', '', time() - 1*24*60*60);
                setcookie('password', '', time() - 1*24*60*60);
            }

            header(&quot;location: index.php&quot;);
        }
        else {
            header (&quot;location: login.php&quot;);
        }

        die();
    }

    /**
     * Check the database for login user
     * Get the password for the user
     * compare md5 hash over sha1
     * @param string $username Raw username
     * @param string $password expected to be md5 over sha1
     * @return bool TRUE on success FALSE otherwise
     */
    private function _check_db($username, $password) {
        global $db;
        $user_row = $db-&gt;get_row(&quot;SELECT * FROM `user` WHERE `username`='&quot; . $db-&gt;escape($username) . &quot;'&quot;);

        //general return
        if(is_object($user_row) &amp;&amp; md5($user_row-&gt;password) == $password)
            return true;
        else
            return false;
    }

    /**
     * stripslash gpc
     * Strip the slashes from a string added by the magic quote gpc thingy
     * @access protected
     * @param string $value
     */
    private function stripslash_gpc(&amp;$value) {
        $value = stripslashes($value);
    }

    /**
     * htmlspecialcarfy
     * Encodes string's special html characters
     * @access protected
     * @param string $value
     */
    private function htmlspecialcarfy(&amp;$value) {
        $value = htmlspecialchars($value);
    }

    /**
     * URL Decode
     * Decodes a URL Encoded string
     * @access protected
     * @param string $value
     */
    protected function urldecode(&amp;$value) {
        $value = urldecode($value);
    }
}
</pre><h2>#4: Creating the login-action.php file:</h2><p>As we have everything in our class, so creating the login-action.php file will be a breeze. All what we do is</p><ul><li>Include the admin-class.php file.</li><li>Initiate a new instance of the itg_admin class.</li><li>Call the <span
class="code">_login_action</span> function.</li></ul><pre class="brush: php; title: ; notranslate">
&lt;?php
include_once 'admin-class.php';
$admin = new itg_admin();
$admin-&gt;_login_action();
</pre><p>and we are done.</p><h2>#5: Creating the index.php file &#8211; restricted access:</h2><p>So, it is time to put all the codes to work. In index.php file we simply do what we have done in login-action.php file, except of calling the _login_action, we call <span
class="code">_authenticate</span> function. It will automatically check whether user is authenticated to access the further script or not and will redirect to login form if necessary.</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;?php
include_once 'admin-class.php';
$admin = new itg_admin();
$admin-&gt;_authenticate();
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Administrator page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;fieldset&gt;
            &lt;legend&gt;Welcome &lt;?php echo $admin-&gt;get_nicename(); ?&gt;&lt;/legend&gt;
                &lt;p&gt;
                    Here are some of the basic informations
                &lt;/p&gt;
                &lt;p&gt;
                    Username: &lt;?php echo $_SESSION['admin_login']; ?&gt;
                &lt;/p&gt;
                &lt;p&gt;
                    Email: &lt;?php echo $admin-&gt;get_email(); ?&gt;
                &lt;/p&gt;
        &lt;/fieldset&gt;
        &lt;p&gt;
            &lt;input type=&quot;button&quot; onclick=&quot;javascript:window.location.href='logout.php'&quot; value=&quot;logout&quot; /&gt;
        &lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre><p>and finally&#8230;</p><h2>#6: The logout.php file:</h2><p>Everything seems to be incomplete without it! What we do is</p><ul><li>Destroy the session.</li><li>Expire the cookie.</li><li>Redirect to login form.</li></ul><pre class="brush: php; title: ; notranslate">
&lt;?php
/**
* The logout file
* destroys the session
* expires the cookie
* redirects to login.php
*/
session_start();
session_destroy();
setcookie('username', '', time() - 1*24*60*60);
setcookie('password', '', time() - 1*24*60*60);
header(&quot;location: login.php&quot;);
?&gt;
</pre><p>and we are done. It looks like this</p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/login-page.png"><img
class="aligncenter size-medium wp-image-1063" title="login-page" src="http://www.intechgrity.com/wp-content/uploads/2012/05/login-page-300x176.png" alt="" width="300" height="176" /></a></p><p><a
href="http://www.intechgrity.com/wp-content/uploads/2012/05/admin-page.png"><img
class="aligncenter size-medium wp-image-1062" title="admin-page" src="http://www.intechgrity.com/wp-content/uploads/2012/05/admin-page-300x173.png" alt="" width="300" height="173" /></a></p><p>So that was all. Feel free to ask any questions you have. I will try best to answer them.</p><p>Related posts:<ol><li><a
href='http://www.intechgrity.com/create-login-admin-logout-page-in-php-w/' rel='bookmark' title='Create Login Admin &amp; Logout Page in PHP w/ SESSION n MySQL'>Create Login Admin &amp; Logout Page in PHP w/ SESSION n MySQL</a></li><li><a
href='http://www.intechgrity.com/get-parent-directory-of-current-url/' rel='bookmark' title='Get Parent directory of Current URL using PHP dirname function'>Get Parent directory of Current URL using PHP dirname function</a></li><li><a
href='http://www.intechgrity.com/how-to-connect-to-xamppmysql-using/' rel='bookmark' title='How to Connect to XAMPP/MySQL using Windows CMD [Command Prompt]'>How to Connect to XAMPP/MySQL using Windows CMD [Command Prompt]</a></li><li><a
href='http://www.intechgrity.com/jquery-recent-post-widget-for/' rel='bookmark' title='jQuery Recent Post Widget for BlogSpot/Blogger with Filter by Tag Option'>jQuery Recent Post Widget for BlogSpot/Blogger with Filter by Tag Option</a></li><li><a
href='http://www.intechgrity.com/understanding-the-basic-of-html-form-and-php-get-and-post-method/' rel='bookmark' title='Understanding the basic of HTML form and PHP GET and POST method'>Understanding the basic of HTML form and PHP GET and POST method</a></li></ol></p><p>- By <a
href="http://www.intechgrity.com">InTechgrity - Amalgamating Life &amp; Technology</a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:nQ_hWtDbxek"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=nQ_hWtDbxek" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=W4WCMKlEgIg:YucjlmcGfoc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=W4WCMKlEgIg:YucjlmcGfoc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=W4WCMKlEgIg:YucjlmcGfoc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/greentechspot?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=W4WCMKlEgIg:YucjlmcGfoc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/greentechspot?a=W4WCMKlEgIg:YucjlmcGfoc:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/greentechspot?i=W4WCMKlEgIg:YucjlmcGfoc:-BTjWOF_DHI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/greentechspot/~4/W4WCMKlEgIg" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/feed/</wfw:commentRss> <slash:comments>6</slash:comments> <feedburner:origLink>http://www.intechgrity.com/login-logout-and-administrate-using-php-session-cookie-mysql-version-2-with-remember-me-option/</feedburner:origLink></item> </channel> </rss><!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              c15c7
Template:           index
--><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 22/97 queries in 0.092 seconds using disk: basic
Object Caching 4683/4754 objects using disk: basic

Served from: www.intechgrity.com @ 2012-05-25 10:07:36 -->

