Sunday, 15 January 2017

Display product short description in woocommerce cart or checkout page

In order to add a short description to WooCommerce cart or checkout page, you could use a code, similar to the following:
<?php echo $_product->post->post_excerpt;?>

Tuesday, 10 January 2017

Get all custom posts by taxonomy


We get all the terms of a taxonomy, loop through them, and fire off a title link to each post that belongs to that term.

$custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        endwhile;
     }
}

Rewrites url with custom parameter in WordPress

A bit of research and a solution was at hand, and actually quite simple to do URL Parameter Rewrites in WordPress.For this exercise, let’s assume we want to use the following URL:
www.example.com/gifts/christmas/girl/50
 This entails variables for occasion, recipient and price. The non-frienly equivalent URL would look like this
www.example.com/gifts/?occasion=christmas&recipient=girl&price=50

Adding Rewrite Tags

First we need to declare the rewrite tags we’d like to use, to make WordPress aware of them. But careful, don’t choose tags that are already in use, otherwise they will get overwritten. These are declared using the add_rewrite_tag function and are best placed inside functions.php.
add_rewrite_tag('%occasion%','([^&]+)');
add_rewrite_tag('%recipient%','([^&]+)');
add_rewrite_tag('%price%','([^&]+)');
 Behind the scenes, WordPress will use the non-friendly version and therefore we need the proper regex to filter them out, as in the code above.

Adding Rewrite Rules

Now that tags are in place, we need to add the rewrite rule that rewrites the friendly URL to non-friendly, so that WordPress can actually parse it and extract the values. For this we use the add_rewrite_rule function.
add_rewrite_rule('^gifts/([^/]*)/([^/]*)/([^/]*)/?','index.php?
pagename=gifts&occasion=$matches[1]&recipient=$matches[2]&price=$matches[3]'
,'top');

In the above case, we use a page called gifts, and pass our values 
which we then extract using the $matches variable. We also specify to 
add this rule at the top of the rewrite stack, so it’s handled first 
before other rewrites. After adding this code to functions.php, you will
 have to visit the permalink settings page 
(/wp-admin/options-permalink.php), this will flush the existing rewrite 
rule cache.

Extract URL Query Values

On the target page (/gifts), we can now extract the URL values like such.
$occasion = isset($wp_query->query_vars['occasion']) ?
 $wp_query->query_vars['occasion'] : false;
$recipient = isset($wp_query->query_vars['recipient']) ?
 $wp_query->query_vars['recipient'] : false;
$price = isset($wp_query->query_vars['price']) ?
 $wp_query->query_vars['price'] : false;
And that completes the implementation of custom URL parameter in WordPress. Any validation and security additions are ignored for simplicity.

Monday, 9 January 2017

fetch youtube video information using api in php

Create a php script to fetch video information where you have to put your youtube video id and parts your going to fetch.
for example:-
https://www.youtube.com/watch?v=7UMNz5T-0VQ
In this URL your youtube video id is showing in red color.
<?php
$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=
YOUTUBE_VIDEO_ID&key=YOUR_YOUTUBE_APIKEY&part=snippet,contentDetails,statistics,status");
$result = json_decode($data, true);
echo "<pre>";
print_r($result);
?>

import excel file into mysql using php

With the help of php we can easily import our excel or csv file data in our Mysql database.
Method-1: Import excel data using php script
$handle = fopen("BooksList.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
$num = count($data);
$row;
echo "INSERT into importing(text,number)values('$data[0]','$data[1]')";
echo "<br>";
}
Method-2:
$handle = fopen("BooksList.csv", "r");
$fields=array('category','datatype','date','value');
$table='test';
$sql_query = "INSERT INTO $table(". implode(',',$fields) .") VALUES(";
while (($data = fgetcsv($handle)) !== FALSE) {
    foreach($data as $key=>$value) {
            $data[$key] = "'" . addslashes($value) . "'";
        }
           $rows[] = implode(",",$data);
  }
$sql_query .= implode("),(", $rows);
$sql_query .= ")";
echo $sql_query;

Monday, 2 January 2017

Set default opened JQuery tabs other than first

In any case, use the index of the tab you want to open, not the href value; i.e. if you want to open the 3rd tab use:
$("#tabs").tabs({ active: 2 });
Check this on Fiddle

Select every other row as male/female from mysql table

To Get the result table from table 'people' as below 

+-----+--------+-------+
| row | gender | name  |
+-----+--------+-------+
|   1 | female | Lisa  |
|   2 | male   | Greg  |
|   3 | female | Mary  |
|   4 | male   | John  |
|   5 | female | Jenny |
+-----+--------+-------+
Follow the below query
SELECT *
FROM (
    SELECT people.*,
        IF(gender=0, @mr:=@mr+1, @fr:=@fr+1) AS rank
    FROM people, (SELECT @mr:=0, @fr:=0) initvars
) tmp
ORDER BY rank ASC, gender ASC;