Create your own wordpress hook


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


  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.

Send mail subject line converts html entities to charecters


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.

Find your browser to make browser specifc updates from jQuery


 
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"; 
} 

Export WordPress data into excel


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;

Get Custom posts from specified term.


$querystr = ”
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = ‘message’
AND $wpdb->posts.post_status = ‘publish’
AND $wpdb->term_taxonomy.taxonomy = ‘taxonomy_name’
AND $wpdb->terms.slug = ‘term_name’
ORDER BY $wpdb->posts.post_date DESC
LIMIT 10
“;

$pageposts = $wpdb->get_results($querystr, OBJECT);

Custom login with desired styling, authentication


$creds = array();

$creds[‘user_login’] = $_POST[‘log’];
$creds[‘user_password’] = $_POST[‘pwd’];
$creds[‘remember’] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
if( $user->get_error_message() ){
$content .=’ERROR: Invalid username. Lost your password?‘;
}

Solution for another daemon running


If you are encountered following error while starting your localhost. I came up with a soluton without any hassle

sandeep@rtcamp:~$ sudo /opt/lampp/lampp startStarting XAMPP for Linux 1.7.4…
XAMPP: Another web server daemon is already running.
XAMPP: Another MySQL daemon is already running.
XAMPP: Another FTP daemon is already running.
XAMPP for Linux started.

Solution: Stop your other services
TO DO SO
TO Stop another web server daemon
sudo /etc/init.d/apache2 stop

To Stop another MySQL daemon
sudo /etc/init.d/mysql stop

To Stop another FTP daemon
sudo /etc/init.d/ftp stop

Restart your localhost
sudo /etc/opt/lampp/lampp restart