Pages

Tuesday, January 29, 2013

Retrieve old Google cache pages

If you want to find your website old cached google page please see below website

http://web.archive.org


Monday, January 7, 2013

Javascript Detect iPhone device

<html>
    <head>
        <title>Mobile Detection</title>
        <script>
            function DetectIphone()
            {
               var uagent = navigator.userAgent.toLowerCase();
               if (uagent.search("iphone") > -1 || uagent.search("ipod") > -1 || uagent.search("android") > -1)
                  alert('true');
               else
                   alert('false');
            }
        </script>
    </head>
    <body>
        <input type="button" OnClick="DetectIphone()" value="Am I an Iphone?" />
    </body>
</html>

Friday, January 4, 2013

Add taxonomy column to custome post type list


Please write below code into functions.php 

<?php
add_filter( 'manage_edit-products_columns', 'my_edit_products_columns' ) ;   
    function my_edit_products_columns( $columns ) {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'title' => __( 'Products' ),
            'prod_cat' => __( 'Category' ),
            'date' => __( 'Date' )
        );
        return $columns;
    }

    add_action( 'manage_products_posts_custom_column', 'my_manage_products_columns', 10, 2 );
    function my_manage_products_columns( $column, $post_id ) {
        global $post;
        switch( $column ) {
            /* If displaying the 'genre' column. */
            case 'prod_cat' :
                /* Get the genres for the post. */
                $terms = get_the_terms( $post_id, 'prod_cat' );
                /* If terms were found. */
                if ( !empty( $terms ) ) {
                    $out = array();
                    /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                    foreach ( $terms as $term ) {
                        $out[] = sprintf( '<a href="%s">%s</a>',
                            esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'prod_cat' => $term->slug ), 'edit.php' ) ),
                            esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'prod_cat', 'display' ) )
                        );
                    }
                    /* Join the terms, separating them with a comma. */
                    echo join( ', ', $out );
                }
                /* If no terms were found, output a default message. */
                else {
                    _e( 'No Genres' );
                }
                break;
            /* Just break out of the switch statement for everything else. */
            default :
                break;
        }
    }
?>

Remove slug from custom post type - [resolved]

Add custom post type

In this example I’ll add “books” as a custom post type. What we want to achieve is that the urls are http://siteurl/%bookname%/

First we need to have a closer look at what rewrite options we have when we register a new post type with register_post_type.

For my hack to work you need to add a slug and set with_front to false:

Step 1: Register post type

First we will start with registering our new post type

add_action('init', 'demo_register_post_type');
    
    function demo_register_post_type() {
        register_post_type('products', array(
            'labels' => array(
            'name' => 'Products',
            'singular_name' => 'Product',
            'add_new' => 'Add new product',
            'edit_item' => 'Edit product',
            'new_item' => 'New product',
            'view_item' => 'View product',
            'search_items' => 'Search products',
            'not_found' => 'No products found',
            'not_found_in_trash' => 'No products found in Trash'),
            'public' => true,
            'rewrite' => array("slug" => "", "with_front" => true),
            'supports' => array('title','editor', 'thumbnail' )
        ));
    }

If you now add a book it will get the url http://siteurl/products/%post_title%/, so far so good.

Step 2: Add rewrite rule

We need to add a new rewrite rule to make the system recognise books without the slug /book/.
It’s important that you hook the rewrite rule after the theme setup is finished, otherwise it will be overwritten.
in this case, the regular expression to look for anything in the root will be “(.*?)$”, basically look for anything.
Our redirection is the query ‘index.php?products=$id’.

function products_rewrite_rule() {
    add_rewrite_rule('(.*?)$', 'index.php?products=$matches[1]', 'top');
}
add_action('after_theme_setup', 'products_rewrite_rule');

Step 3: .htaccess

Add a rule to redirect /products/%post_title%/ to /%post_title%/ in .htaccess
 
RewriteRule ^products/(.+)$ /$1 [R=301,L]

Step 4: Fix the permalink that is shown when editing

When adding a new book in this case or editing an old one, the permalink will still show “Permalink: http://siteurl/products/%post_title%/” and View will also go to the url with the slug. Here is the final piece to hack the slug away

function remove_slug($permalink, $post, $leavename) {
 $permalink = str_replace(get_bloginfo('url') . '/products' , get_bloginfo('url'), $permalink);
 return $permalink;
}
add_filter('post_type_link', 'remove_slug', 10, 3);

Thursday, January 3, 2013

N level Category Tree using php and mysql - [resolved]

First Method

function get_categories($parent = 0)
{
    $html = '<ul>';
    $query = mysql_query("SELECT * FROM `categories` WHERE `category_parent` = '$parent'");
    while($row = mysql_fetch_assoc($query))
    {
        $current_id = $row['category_id'];
        $html .= '<li>' . $row['category_name'];
        $has_sub = NULL;
        $has_sub = mysql_num_rows(mysql_query("SELECT COUNT(`category_parent`) FROM `categories` WHERE `category_parent` = '$current_id'"));
        if($has_sub)
        {
            $html .= get_categories($current_id);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}

print get_categories();