Create your own wordpress hook

Posted: November 26, 2012 in WordPress

Though we developed many themes and plugins we do not own
a single hook which fills our requirement this is for those
who wish to create their own hook for their requirement.

How it works?

Ideally our lovely WordPress provides a brilliant
method to create hook which is do_action(),
this generally refers to create hook,
this method serves as placeholder where add_action() needs
triger the specific event.

How to create action hook?

add_action('Ski_hook_after_title','after_title',10);

function after_title(){
echo “Sandeep Kumar”;
echo “Do your stuff here”;

}

function ski_hook_after_title(){
do_action(‘ski_hook_after_title’);
}

Generate SSH key pair

Posted: September 10, 2012 in Uncategorized

  1. Download SSH key generator from putty key generator utility click here.
  2. Select SSH-2 RSA
  3. Hit on generate key
  4. This will prompt you to move your cursor around blank area.
  5. As you move key will generate and indicator will show you the progress
  6. Save your public and private keys in desired location.

Many of us might have encountered this issue, seems small but creates major impact. Both Php and WordPress send mail
method will converts subject line’s html entities such as (single quotes), ( double quotes ), ( @ ), ( ! )…. into characters while sending mail.

I got solution for this issue, I used html_entity_decode() function to get rid off this issue.

    
       wp_mail( $to, stripslashes_deep( html_entity_decode( $subject, ENT_QUOTES, 'UTF-8' ) ), $message );
    

Parameters Description:
Returns a string with backslashes stripped off. (\’ becomes ‘ and so on.) Double backslashes (\\) are made into a single backslash (\).

UTF-8: Defines character set ENT_QUOTES will converts both double and single quotes.


 
function getCurrentBrowser() { 
     jQuery.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
     if(jQuery.browser.chrome)
     return "chrome";
     if(jQuery.browser.mozilla)
     return "mozilla"; 
     if(jQuery.browser.opera) 
     return "opera";
     if(jQuery.browser.safari)
     return "safari"; 
     if(jQuery.browser.msie)
      return "ie"; 
} 

Chrome Users: Try the WordPress.com Extension.

via Chrome Users: Try the WordPress.com Extension.


function to override bultin logout without disturbing core file.
This is for centralized user whith specifc role under multi site.

Export WordPress data into excel

Posted: January 28, 2012 in WordPress

DECLARE GLOBAL VARIABLE SINCE WE ARE USING WP CUSTOM QUERIES
global $wpdb,$post;
$file= "tablename";
SET YOUR BLOG ID NECESSARY ONLY WHEN FETCHING FROM CENTRALIZED DB
$wpdb->blogid = '257';
$wpdb->set_prefix( $wpdb->base_prefix );

ADD COLUMN HEADER WHICH HAS TO APPEAR IN EXCEL SHEET/CSV
$column_name = array(‘Business Name’, ‘City’, ‘Today Status’, ‘Tomorrow Status’, ‘Details’);
foreach( $column_name as $caname )
$output .= $caname.”\t”;

PRINT COLUMN HEADERS
echo $output;

CUSTOM QUERY TO FETCH POST AND POSTMETA VALUES ( YOU CAN CHANGE TABLE NAMES TO GET DESIRED OUTPUT )
$expid = “SELECT DISTINCT ID FROM “.$wpdb->prefix.”posts LEFT JOIN “.$wpdb->prefix.”postmeta ON ( “.$wpdb->prefix.”posts.ID=”.$wpdb->prefix.”postmeta.post_id )”;
$expids = $wpdb->get_results( $wpdb->prepare( $expid ));

ADDS NEXT LINE BASED ON WHICH BROWSER AGENT
$unewline = “\r\n”;
if (strstr(strtolower($_SERVER[“HTTP_USER_AGENT”]), ‘win’)) {
$unewline = “\r\n”;
} else if (strstr(strtolower($_SERVER[“HTTP_USER_AGENT”]), ‘mac’)) {
$unewline = “\r”;
} else {
$unewline = “\n”;
}

foreach( $expids as $t ){
ADD YOUR META KEY TO GET RESULT
$city = get_post_meta($t->ID, ‘METAKEY’, true);
$history = get_post_meta($t->ID, ‘METAKEY’, true);
if($history){
$output1 .= strip_tags($history[‘bname’]).”\t”.strip_tags($city).”\t”.strip_tags($history[‘todayStatusStr’]). “\t”. strip_tags($history[‘tomorrowStatusStr’]). “\t”. strip_tags($history[‘details’]) . $unewline;
}
}

CREATES YOUR FILE WITH DATE AND TIME
$filename = $file.”_”.date(“Y-m-d_H-i”,time());

IMPORTANT PART THIS CREATES EXCEL HEADERS
header(“Content-type: application/vnd.ms-excel”);
header(“Content-disposition: xls” . date(“Y-m-d”) . “.xls”);
header( “Content-disposition: filename=”.$filename.”.xls”);

PRINT YOUR COLUMNS ON EXCEL
print $output1;
exit;