Hot link image cacher with keywords

This plugin caches your Hotlinked images from posts and saves it with provided keywords.
How does this plugin work:

  • After you activate the plugin, this plugin will automatically save all hotlinked image to your own server.
  • Curl is required to download images to your server.
  • Uses WordPress default upload functions to upload the downloaded images to wordpress upload directory.
  • You can process olders posts aswell in plugin’s page.
  • You can provide set of keywords to name the hotlinked image when saved to your server.

Download

Freshbooks class for codeigniter

This post shows you how to make freshbookrequest class from https://github.com/jboesch/FreshBooksRequest-PHP-API to work with codeigniter.
First download the files.
Copy two files from lib folder to your library folder in codeigniter.
open FreshBooksRequest.php file.
replace with following code:

<?php
require_once('XmlDomConstruct.php');
/**
 * A simple PHP API wrapper for the FreshBooks API.
 * All post vars can be found on the developer site: http://developers.freshbooks.com/
 * Stay up to date on Github: https://github.com/jboesch/FreshBooksRequest-PHP-API
 *
 * PHP version 5
 *
 * @author     Jordan Boesch <jordan@7shifts.com>
 * @license    Dual licensed under the MIT and GPL licenses.
 * @version    1.0
 */
class FreshBooksRequestException extends Exception {}
class FreshBooksRequest {

    /*
     * The domain you need when making a request
     */
    protected $_domain = '';

    /*
     * The API token you need when making a request
     */
    protected $_token = '';

    /*
     * The API url we're hitting. {{ DOMAIN }} will get replaced with $domain
     * when you set FreshBooksRequest::init($domain, $token)
     */
    protected $_api_url = 'https://{{ DOMAIN }}.freshbooks.com/api/2.1/xml-in';

    /*
     * Stores the current method we're using. Example:
     * new FreshBooksRequest('client.create'), 'client.create' would be the method
     */
    protected $_method = '';

    /*
     * Any arguments to pass to the request
     */
    protected $_args = array();

    /*
     * Determines whether or not the request was successful
     */
    protected $_success = false;

    /*
     * Holds the error returned from our request
     */
    protected $_error = '';

    /*
     * Holds the response after our request
     */
    protected $_response = array();

    public function __construct($params)
    {
        $this->_domain = $params['domain'];
        $this->_token = $params['token'];
    }

    /*
     * Set up the request object and assign a method name
     *
     * @param array $method The method name from the API, like 'client.update' etc
     * @return null
     */
    public function setMethod($method)
    {
        $this->_method = $method;
    }

    /*
     * Set the data/arguments we're about to request with
     *
     * @return null
     */
    public function post($data)
    {
        $this->_args = $data;
    }

    /*
     * Determine whether or not it was successful
     *
     * @return bool
     */
    public function success()
    {
        return $this->_success;
    }

    /*
     * Get the error (if there was one returned from the request)
     *
     * @return string
     */
    public function getError()
    {
        return $this->_error;
    }

    /*
     * Get the response from the request we made
     *
     * @return array
     */
    public function getResponse()
    {
        return $this->_response;
    }

    /*
     * Get the generated XML to view. This is useful for debugging
     * to see what you're actually sending over the wire. Call this
     * after $fb->post() but before your make your $fb->request()
     *
     * @return array
     */
    public function getGeneratedXML()
    {

        $dom = new XmlDomConstruct('1.0', 'utf-8');
        $dom->fromMixed(array(
            'request' => $this->_args
        ));
        $post_data = $dom->saveXML();
        $post_data = str_replace('<request/>', '<request method="' . $this->_method . '" />', $post_data);
        $post_data = str_replace('<request>', '<request method="' . $this->_method . '">', $post_data);

        return $post_data;

    }

    /*
     * Send the request over the wire
     *
     * @return array
     */
    public function request()
    {

        if(!$this->_domain || !$this->_token)
        {
            throw new FreshBooksRequestException('You need to call FreshBooksRequest with your domain and token.');
        }

        $post_data = $this->getGeneratedXML();
        $url = str_replace('{{ DOMAIN }}', $this->_domain, $this->_api_url);
        $ch = curl_init();    // initialize curl handle
        curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 40s
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // add POST fields
        curl_setopt($ch, CURLOPT_USERPWD, $this->_token . ':X');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $result = curl_exec($ch);

        if(curl_errno($ch))
        {
            $this->_error = 'A cURL error occured: ' . curl_error($ch);
            return;
        }
        else
        {
            curl_close($ch);
        }

        $response = json_decode(json_encode(simplexml_load_string($result)), true);

        $this->_response = $response;
        $this->_success = ($response['@attributes']['status'] == 'ok');
        if(isset($response['error']))
        {
            $this->_error = $response['error'];
        }

    }

}

Now to use it in your controller. do this:

 //freshbook api
                                        $params = array('domain' => $this->config->item('freshbook_domain'), 'token' => $this->config->item('freshbook_token'));
                                        $this->load->library('FreshBooksRequest', $params);
                                        $this->freshbooksrequest->setMethod('client.list');
                                        //search client with email
                                        $this->freshbooksrequest->post(array(
                                            'email' => $agency_row->agency_email
                                        ));
                                        $this->freshbooksrequest->request();
                                        if ($this->freshbooksrequest->success()) {
                                            $response = $this->freshbooksrequest->getResponse();}
else{
 $this->freshbooksrequest->getError();
                                            $this->freshbooksrequest->getResponse();
                                        }

FOr oauth library : https://github.com/cloudmanic/php-freshbooks-codeigniter

paged query in wordpress

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
              query_posts('paged='.$paged);

contact form 7 controlling br tags

Have you encountered br tags messing your contact form 7 forms. you can remove it using below code in wp-config.php:

define( 'WPCF7_AUTOP', false );

more info at http://contactform7.com/controlling-behavior-by-setting-constants/

Posting to a wordpress page from a form

Ever tried to post a form to wordpress page? sometimes you might have encountered 404 error. this is due to reserved name for worpress like “name”. You should not have input with name “name” or else you will get 404 error after submission.
for more details go to : http://www.cabeeb.com/2009/09/posting-a-form-to-a-wordpress-page/

wp nav menu undocumented paramater

‘items_wrap’ => ‘<ul id="%1$s">%3$s</ul>

This is executed by: $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );

you can play around with the sprintf arguments

ex: ‘items_wrap’ => ‘%3$s’ would remove the wrapping <ul> tag

svn issues unable to rename

Sometimes while working with svn you might encounter errors like unable to rename .svn/tmp/entries to .svn/entries. I encountered this error when I changed my computer. I used to use pc then i got myself mac book pro. I had to transfer all my files from old pc to new mac. then after i set it up i did svn update and the error was shown. after searching a while on google. I found out the solution. Solution is to run following command on the root folder of the svn directory.

chflags -R nouchg .

why to run above command? explanation as I found on stack overflow:

If you’re changing workspaces on OS X and you import an SVN-based project into your new workspace, some of your files may have the uchg flag set. SubClipse/SVN will not be able to update this project. You will get an error:

svn: Cannot rename file

every time you try invoke svn. If you issue:

chflags -R nouchg .

at the top-level of the project directory this will clear these flags and restore SVN function.

Get top parent page id in wordpress.

<?php
function getTopParentPageId($page_id){
 $mypage = get_page($page_id);
 if ($mypage->post_parent == 0){ 
   return $mypage->ID; 
 }
 else{ 
  return getTopParentPageId($mypage->post_parent); 
 }
}
?>

SEO friendly timthumb on wordpress

Many of you love using timthumb in your wordpress theme but don’t use it because it does not generates seo friendly urls for images. I was asked by a client to make timthumb urls seo friendly and here is how I did that.

First open your wordpress site .htaccess file. Generally it is located in root of your wordpress installation.

It should look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Now create new folder in your root call it let’s say images and place cache folder and timthumb in there then edit your .htaccess file as below

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(images) [NC]
RewriteRule . /index.php [L]
RewriteRule ^images/(.*)x(.*)/r/(.*) images/timthumb.php?src=http://example.com/$3&amp;w=$1&amp;h=$2&amp;zc=1&amp;q=100&amp;a=tl
</IfModule>

# END WordPress

Replace example.com with you site name:
Now upload htaccess file and where you need to use timthumb do as below

<?php
if (has_post_thumbnail()):
  $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
  echo '&lt;a href="'.get_permalink().'"&gt;&lt;img src="'.get_bloginfo('url').'/images/715x440/r/'.str_replace(array('http://example.com/', 'http://www.example.com/'), array('',''), $thumbnail[0]).'" width="715" height="440" alt="'.get_the_title().'" /&gt;&lt;/a&gt;';
endif;
?>

That should do the trick for making timthumb url seo friendly.

Get menu name from menu location

<?php
function wpexpo_get_theme_menu_name( $theme_location ) {
	if( ! $theme_location ) return false;

	$theme_locations = get_nav_menu_locations();
	if( ! isset( $theme_locations[$theme_location] ) ) return false;

	$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
	if( ! $menu_obj ) $menu_obj = false;
	if( ! isset( $menu_obj-&gt;name ) ) return false;

	return $menu_obj-&gt;name;
}
?>

Tags