Showing posts with label wordpress. Show all posts
Showing posts with label wordpress. Show all posts

Nov 14, 2011

Wordpress Multiple Category Search

Since when I started wordpress, I had a question in my mind, why wordpress doesn’t give multiple search option? I googled a lot, but couldn’t find a plugin or code which exactly works.

So finally decided to go more into deep of wordpress and php, and came up with a code which works.

Functioning of this code:  Searches all posts using a custom search box where 2 drop-down box will be displayed with values of categories in it.

Have you ever played with search URL? Or tags URL or Categories URL?

Very few might have knowledge that wordpress supports multiple tags or multiple categories. Don’t believe me, in your URL, type this

http://yourblog.com/tag/tag1+tag2

Or

http://yourblog.com/tag/tag1,tag2

Now what does this means? Simple when you type tag1+tag2 it will show you posts with having both the tags. And when you type tag1, tag2 it will show you all the posts having either tag in it.

Same works for categories.

http://yourblog.com/category/cat1+cat2
http://yourblog.com/category/cat1,cat2

Now I tried this same functionality in search URL of Wordpress.

http://yourblog.com/?s=html&cat=114

And woila it worked!!!!

Here “s” is your search term and “cat=114” is your category ID. This will work and display all the posts with the search term only in category ID 114.

But the only problem in this is that it will just take one category at a time.

Wordpress doesn’t supports this URL

http://yourblog.com/?s=html&cat=114+115

Here comes a big problem. After a lot of search I found that “+” is considered as a space in URL. So we need to write a function which will make this space act as a + operator.



[php]
add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {
if ( ! isset( $query->query_vars[ 'cat' ] ) )
return $query;
// split cat query on a space to get IDs separated by '+' in URL
$cats = explode( ' ', $query->query_vars[ 'cat' ] );
if ( count( $cats ) > 1 ) {
unset( $query->query_vars[ 'cat' ] );
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}

function check()
{
$url = $_SERVER["REQUEST_URI"];
$query = str_replace('&cats=', '+', $url);
if(isset($query) && ($query != $url))
{
header('Location:'.$query);
}
}

add_action('parse_query', 'check');
[/php]

After getting this function in our functions.php file, the multiple categories URL with search will work.

So it’s time to design a simple search form.

Paste this anywhere in your theme.



[html]
<!-- Search Form -->
<div style="margin:10px 0 10px 0;">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<table cellspacing="5">
<tr>
<td>
<label for="s" style="margin:10px;">Search</label>
</td>

<td>
<input type="text" class="field" name="s" style="width: 100px;"/>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1&name=cats'); ?>
</td>
<td>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" style="display: block; position: relative; top: 7px;"/>
</td>
</tr>
</table>
</form>
</div>
<!-- Search Form -->


[/html]

That’s it, now just go and try your wordpress site will be having a multiple category search option. No plugin and no hundred lines of code, and best part, it’s fully flexible to use. If you want one more category drop down then just copy paste the category php code one more time.




Nov 8, 2011

Ten Wordpress Useful Functions And Snippets

Some useful Wordpress PHP funtions.

Just copy and paste these functions in your themes functions.php file

Change the WP Login Logo & URL Link

[php]
// CUSTOM ADMIN LOGIN HEADER LOGO
function my_custom_login_logo() {
echo '';
}
add_action('login_head', 'my_custom_login_logo');

// CUSTOM ADMIN LOGIN HEADER LINK & ALT TEXT
function change_wp_login_url() {
echo bloginfo('url'); // OR ECHO YOUR OWN URL
}
function change_wp_login_title() {
echo get_option('blogname'); // OR ECHO YOUR OWN ALT TEXT
}
add_filter('login_headerurl', 'change_wp_login_url');
add_filter('login_headertitle', 'change_wp_login_title');

[/php]

Load JQuery From Google CDN

[php]
// even more smart jquery inclusion :)
add_action( 'init', 'jquery_register' );

// register from google and for footer
function jquery_register() {

if ( !is_admin() ) {

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}
[/php]

How to remove the WordPress Version Information

[php]
// remove version info from head and feeds
function complete_version_removal() {
return '';
}
add_filter('the_generator', 'complete_version_removal');

[/php]

Remove Default Wordpress Meta Boxes

[php]
// REMOVE META BOXES FROM DEFAULT POSTS SCREEN
function remove_default_post_screen_metaboxes() {
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_post_screen_metaboxes');

// REMOVE META BOXES FROM DEFAULT PAGES SCREEN
function remove_default_page_screen_metaboxes() {
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_page_screen_metaboxes');

[/php]

Include custom post types in the search results of WP

[php]
// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );

[/php]

Add Excerpt to pages

[php]
if ( function_exists('add_post_type_support') )
{
add_action('init', 'add_page_excerpts');
function add_page_excerpts()
{
add_post_type_support( 'page', 'excerpt' );
}
}

[/php]

Change the order of Admin Menu

[php]
// CUSTOMIZE ADMIN MENU ORDER
function custom_menu_order($menu_ord) {
if (!$menu_ord) return true;
return array(
'index.php', // this represents the dashboard link
'edit.php?post_type=events', // this is a custom post type menu
'edit.php?post_type=news',
'edit.php?post_type=articles',
'edit.php?post_type=faqs',
'edit.php?post_type=mentors',
'edit.php?post_type=testimonials',
'edit.php?post_type=services',
'edit.php?post_type=page', // this is the default page menu
'edit.php', // this is the default POST admin menu
);
}
add_filter('custom_menu_order', 'custom_menu_order');
add_filter('menu_order', 'custom_menu_order');

[/php]

Change the length of excerpts

[php]
function new_excerpt_length($length) {
return 100;
}

add_filter('excerpt_length', 'new_excerpt_length');

[/php]

Enable GZIP output compression

[php]
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));

[/php]

Get the First Image from the Post Content

[php]
// AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, ' $start = 0;
for($i=1;$i $imgBeg = strpos($content, ' $postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],' $more = 0;
}

[/php]

Credit: Users Of wordpress.stackexchange.com

Sep 2, 2011

Wordpress Show Excerpt Of Child Page | How To Show Excerpt Of Child Pages On Parent Page Of Wordpress

Today in this tutorial I am going to share a wordpress hack, which will show your child pages excerpts with title on the parent page.

OK, it was confusing, let me explain you.
Suppose you have  5 pages, which has a single parent page. If you want to show all the 5 sub pages on the Parent page, then you have to use this code.

Open your page.php file and paste this code after the loop.

[php]
<?php
$child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div style="border:1px solid #e7e7e7;margin: 10px;padding: 10px;">
<h2><a href="<?php echo  get_permalink($pageChild->ID); ?>" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
?>
<?php the_excerpt();?>
<p style="text-align:right;"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>">Read More...</a></p>
</div>
<?php endforeach;
endif;
?>
[/php]

Aug 16, 2011

Wordpress JQuery Slide Show | How To Add A Wordpress Jquery Slide Show | Wordpress JQuery Nivo Slider

So finally you reached here in search of adding a slide show in your wordpress site. Why to use flash if jquery can help you.


In this tutorial I am using the NIVO SLIDER.


So lets get started, just follow these simple steps:-


First mainly decide where you want to display this slide show in your blog or site? For this example we will use this slide show in the header, as an image banner.


So first you need to add some files in your theme, just that they don't get mixed up with your other files, we will create a folder in your theme directory, name it as slider, in that folder we will place some css files and images.(Download)
So after completion, your folder structure should be like the below image.
Slider folder


After this you need to add some js files in your folder js, if you don't have any folder named as js, then you can create one and place files into it.(Download)


The two js, files are:




  • jquery.nivo.slider.pack.js

  • jquery-1.6.1.min.js


Then you need to open your theme's header.php file, and paste the following code before wp_head();



[html]
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/slider/default.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/slider/nivo-slider.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/slider/style5.css" type="text/css" media="screen" />

<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.nivo.slider.pack.js"></script>
<script type="text/javascript">
jQuery(window).load(function() {
jQuery('#slider').nivoSlider();
});
</script>
[/html]

In the above code we are just telling the wordpress theme to pick the necessary style sheets, js files and JQuery.


So here we complete major part of the tutorial, now finally we will just paste some html code to appropriate section in the site.



As in this tutorial I am making a header banner so I ll paste this code in header.php.

[html]
<div class="slider-wrapper theme-default">
<div class="ribbon"></div>
<div id="slider">
<img src="<?php bloginfo('template_directory'); ?>/images/header1.jpg" alt=""/>
<img src="<?php bloginfo('template_directory'); ?>/images/header2.jpg" alt=""/>
<img src="<?php bloginfo('template_directory'); ?>/images/header3.jpg" alt=""/>
<img src="<?php bloginfo('template_directory'); ?>/images/header4.jpg" alt=""/>
<img src="<?php bloginfo('template_directory'); ?>/images/header5.jpg" alt=""/>
</div>
</div>
[/html]
Just in img src tag give your appropriate images location, in this tutorial I have kept the images into images folder of my wordpress theme directory.

You can paste the JQuery code in footer.php file just to avoid the conflict of javascripts.


In This tutorial I am using the header banner size, that is 995X288, so if your slider goes off screen, then you can customize our css files.
This Slider is IE7 compliant.


download

Aug 8, 2011

Wordpress Custom Login Page | Wordpress Login Page

WordPress Custom Login Page




Wordpress login page is pretty good and simple, but after some time you will also find it boring. So modifying your wordpress login page is a good idea. This will also attract the users of the site and also helpful if you have multi admin users.

It looks difficult but actually its simple. You need to create two images with the following names

  • login-bkg-tile.gif 

  • login-bkg-bottom.gif


and copy and paste them in the following directory of your wordpress site wp-admin/images/ folder.
Previously if any one wanted to change the login page, it was simple with getting two hurdles in between.

  1. The Image

  2. And The Wordpress Upgrade


Wordpress Custom Login Page

How will you get the images?
This question can be answered by your current theme, your theme will be having many images which will be matching with your site, I ll recommend you to take the body background image and play with it little bit to make two images as show above, else start with Photoshop for totally made from scratch two new images.

And the next tweak the upgrade.
Generally people upgrade their wordpress as soon as new version is available, but I recommend you that don't change until and unless you find it necessary. Playing with core files unnecessarily will either damage your few plugins(majorly jquery) and make them functionless or can break your theme or both.


The Ultimate Solution

Why don't I offer you a free plugin for this, so you can start going with it without any headache.



download

Jul 17, 2011

Fully Customized Wordpress Twenty Ten Theme

Fully customized twenty ten theme, download and have fun, its fully free.

Credit:http://www.dynamicwp.net/free-themes/twentyeleven-theme/


download

Jul 15, 2011

Wordpress Twenty Eleven Theme sidebar on Pages and Posts

Recently I added a post on how to add a sidebar on single(posts-page) or single.php

So today I am with one more child theme which will add sidebar on page.php i.e. pages also

So just download and install this child theme. This new child theme will enable the sidebar on page.php and also on single.php page.


download

Jul 14, 2011

Wordpress Make A Featured Post Jquery Ticker | Wordpress Make featured Posts

Now a days blogging is very common, every human being wants to have their own blog or website where they can put some useful stuffs, but out of that blogs, there are some important or hot blogs, which we say as featured blogs, if you are wordpress user then why to adopt plugin, where you can create a simple one.
So lets get started.

First decide where you want to display the featured post section, usually it is on the home page. So considering it on home page, we will edit two files in this tutorial.
index.php
header.php

So lets get started with header.php, open that file, and in the head section put this small piece of code

[css]
<style>
#posts-container{ }
#posts-container ul li div{
border: 1px solid #aaaaaa;
background: #ffffff;}
</style>
[/css]

You can see that the style is not fully complete, cause I have left it upto you, so that you can have your own style, but if you leave this also it doesn't matter, cause this will also give the blog a simple and neat looks to featured posts

After styling we will add some javascript code to head section.
Be very careful while adding javascript code, as it may conflict with other js files.

[javascript]
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.vticker-min.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('#posts-container').vTicker({
speed: 600,
pause: 3000,
animation: 'fade',
mousePause: true,
showItems: 3
});
});
</script>
[/javascript]

As you can see the $ is been replaced by jQuery just to avoid the conflict.
So the final header code will look something like this:

[html]
<style type="text/css" media="all">
#posts-container{ }
#posts-container ul li div{
border: 1px solid #aaaaaa;
background: #ffffff;}
</style>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.vticker-min.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('#posts-container').vTicker({
speed: 600,
pause: 3000,
animation: 'fade',
mousePause: true,
showItems: 3
});
});
</script>
[/html]

In the above javascript, you can change the attribut as per your need, like speed, animation, on-mouse-over-pause etc etc. You can change it anytime. I have kept the js file into the js folder of the theme, if you are keeping it in any other folder then be sure to edit the code.

This part completes the main jquery.
But the question is how will you select the posts as featured?
For this we can use custom fields.
Add a new custom field in your Add-new or Edit posts page, and replicate the same as in below image
How TO Add Custom Field

Tip: Instead Of Manually Adding one-one custom field, I recommend that you use More Fields Plugin. Which Will help you to have a custom field for all post.

Do the above step for only those posts which you want to display as featured posts.

Now open index.php file and just paste this code just above the if ( have_posts() ) code

[php]
<?php
$key = 'featured';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta == 'no') {
echo 'No Featured Posts';
}
elseif($themeta == 'yes'){
?>
<?php query_posts('meta_key=featured&meta_value=Yes');  ?>
<div style="border:4px solid #666666; -moz-border-radius:5px; -webkit-border-radius:5px; padding:12px; color:#333333;font-weight:bold; background-color:#FFFFE0;">
<h1 style="color:#880000; font-weight:bold; font-size:18px; text-decoration:underline;">Featured Posts:</h1>
<div id="posts-container">
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" style="color:#333333;"><?php echo the_title() ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>

<?php wp_reset_query();

}?>
[/php]

In this code we are taking the posts which has custom field value set to yes for featured, and displaying it in a list format, here I have given the css into the style tag of a div, you can change it any time as per your theme design.  I am also using wp_reset_query just to be sure that it doesn't conflicts with other posts.

This above tutorial is just going to display posts title, but if you also want to display posts content, then just make a small change to the above code, just add the 3 line to the code.

[php]
<li>
<a href="<?php the_permalink() ?>" style="color:#333333;"><?php echo the_title() ?></a>
<br><em><php echo the_excerpt(); ?></em>
</li>
[/php]

You can see the demo on my websites homepage
Demo | Download [download id="3"]

Jul 12, 2011

Wordpress Remove Parent Category From Permalink

The easiest way to remove parent category from permalink only if it has a child category is to include the below code in your theme's functions.php file

[php]

// Remove category base
add_filter('category_link', 'no_category_parents',1000,2);
function no_category_parents($catlink, $category_id) {
$category = &get_category( $category_id );
if ( is_wp_error( $category ) )
return $category;
$category_nicename = $category->slug;

$catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $category_nicename, 'category' );
return $catlink;
}

// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'no_category_parents_rewrite_rules');
function no_category_parents_rewrite_rules($category_rewrite) {
//print_r($category_rewrite); // For Debugging

$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
}
// Redirect support from Old Category Base
global $wp_rewrite;
$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( '%category%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';

//print_r($category_rewrite); // For Debugging
return $category_rewrite;
}

// Add 'category_redirect' query variable
add_filter('query_vars', 'no_category_parents_query_vars');
function no_category_parents_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
// Redirect if 'category_redirect' is set
add_filter('request', 'no_category_parents_request');
function no_category_parents_request($query_vars) {
//print_r($query_vars); // For Debugging
if(isset($query_vars['category_redirect'])) {
$catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
status_header(301);
header("Location: $catlink");
exit();
}
return $query_vars;
}

[/php]

Wordpress Remove Pagination

Ok to remove pagination only from archives page, just past the below snippet to functions.php file

[php]

function no_nopaging($query) {
if ( !is_home() && !is_feed() && '' === $query->get('nopaging') )
$query->set('nopaging', 1);
}

add_action('parse_query', 'yay_nopaging');

[/php]

If you want to remove from other pages, then just change the if condition.

Jul 10, 2011

Wordpress Make HTML5 theme

HTML 5 is the latest and the hottest topic in the web development.
Now a days everybody wants there blogs theme to be in HTML5, even the latest WP-3.2 default theme twenty eleven is also in HTML-5.

So why to wait, convert our current blog old html theme to brand new HTML-5

In this tutorial, you are going to learn the conversion of old HTML to HTML-5

1. Basic Document’s structure

[php]
<!doctype html>
<head>
</head>
<body>
<header>
</header>
<section id="content">
<article>
<header>
</header>
</article>
</section>
<aside>
<nav>
</nav>
</aside>
<footer>
</footer>
</body>
</html>
[/php]

As we can see, that the tags are very new to us. If we add following CSS:
article, aside, figure, footer, header, hgroup, menu, nav, section { display:block; }
we are free to format them like other block elements, e.g.


  • Tag Header, as the name itself indicates that all the head contents like, logo, navigation, search etc will come into it. The usage of this tag inside
    will be presented below.



  • tag used to keep the order in document. We can divide the website into sections, e.g. content, comments, gallery etc.



  • can be used to store our entries – each entry in separate tag! The title and meta of entry should be inside
    – it will have positive impact on our SEO.



  • Our sidebar should be inside






  • If we have footer it is a good practice to put it inside
    . We can have more than one
    , but each should be inside separate
    and one can be directly inside


Tags  like article, header, footer, section can be used more than one time, but the structure has to be maintained.

[php]
<section id="content">
<article>
<header>
</header>
<p>
</p>
<footer>
</footer>
</article>
</section>
[/php]

In header, we can put h3 tag, which is commonly used in wordpress themes content.
If you want to use more then one time hx tags, then it should be wrapped inside hgroup tag.

[php]
<hgroup>
<h3>Main Title</h3>
<h4>Subtitle</h4>
</hgroup>
[/php]

The meta of the post can be wrapped by
, what is a convenient solution.


When it comes to the comments we can treat them like posts and put inside
with >header>,


,
etc. inside.


When we want to refresh our comment form we should definitely use HTML5. Some new input features have been added. Let’s take a look on this:

[php]
<input type="text" name="author" id="comment-form-author" placeholder="Your name" value="" size="22" tabindex="1" required="required">

<input type="email" name="email" id="comment-form-email" placeholder="Your email" pattern="[^ @]*@[^ @]*" value="" size="22" tabindex="2" required="required">

<input type="text" name="url" id="comment-form-url" placeholder="Your website" size="22" tabindex="3" />
[/php]

As a value of placeholder we can specify a hint for user, it will appear as a text inside the input and after clicking on it will disappear.
pattern attribute allows us to specify a patter than must be followed. Here we have an example of pattern for email address.

HTML5 is powerful tool, but it’s not completely supported by all browsers.
More cool features we can find on http://html5demos.com/

 

Wordpress Dynamic Favicon

It’s simple to add your own link within the header.php file to create a favicon. However if you’d like to be a little more dynamic we can write a simple function inside our functions.php which adds our favicon into every page without fail. Below is the few lines of code required.

[php]
<code>function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');</code>

[/php]

Wordpress Post Autosave Limit

Wordpress auto save post help's the user to save post automatically, but you can play with it, you can set the limit of time to save the post.
Just copy the following code to your wp-config.php file

[php]
# Autosave interval set to 5 Minutes #
define('AUTOSAVE_INTERVAL', 300);
[/php]

Best way to include JQuery | Correct way to include JQuery

JQuery is the javascript library, but just copy pasting the JQuery snippet will not do, it will start conflicting with other JQeries, so the best practice is to use the following method

[php]<?php wp_enqueue_script("jquery"); ?>[/php]

Jul 7, 2011

How To Add Simple Pagination To WordPress

Many sites uses pagination to navigate to the older posts. If you install twenty ten or twenty eleven default wp theme, then you will notice that instead of pagination, it gives you old fashion links.
So to have pagination write the below function to your functions.php file

 

[php]function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' =>true,
'type' =>'list',
'next_text' => '»',
'prev_text' =>'«'
);

if( $wp_rewrite->sing_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
}[/php]

After some PHP code, we will need to add some CSS styling, so it gets the looks of actual pagination.
So add the following CSS code to your style.css file

 

[php]/* Pagination */

ul.page-numbers {
margin: 20px 0 10px;
width: 100%;
padding: 0;
font-size: 12px;
line-height: normal;
clear: both;
float: left;
}

ul.page-numbers li {
float: left;
}

ul.page-numbers a,
ul.page-numbers span {
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
background: -webkit-gradient(linear, left top, left bottom, from(#E4E3E3), to(#FFFFFF));
background: -moz-linear-gradient(top, #E4E3E3, #FFFFFF);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#E4E3E3', endColorstr='#FFFFFF');
padding: 3px 4px 2px 4px;
margin: 2px;
text-decoration: none;
border: 1px solid #ccc;
color: #666;
}

ul.page-numbers a:hover,
ul.page-numbers span.current {
border: 1px solid #666;
color: #444;
}[/php]

The above CSS is really simple, if you want to make your pagination to match your theme, then simply edit the CSS. Its damn Simple.
To include the pagination, we need to call the function.
So if you are going to display the pagination on the home dynamic page, then goto index.php file, and replace the below function with your old boring pagination.

[php]
<?php paginate(); ?>
[/php]

To know more about paginate_links()
Refer the Codex: http://codex.wordpress.org/Function_Reference/paginate_links

Wordpress Search URL Rewrite

Have you ever considered your wp-site search URL?

http://yoursite.com/?s=searchterm

Also after setting the custom permalink structure, the only URL which is not effected is the search URL, it still remains the same.
OK, now goto your site, and after your root URL, just type this search/term

http://yoursite.com/search/term

Woila!!!! you'll find that the search worked, just that there is no URL rewrite rule written for this.
So to make it done, just add the following function to your themes functions.php file

[php]
function search_url_rewrite_rule() {
if ( is_search() && !empty($_GET['s'])) {
wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
exit();
}
}
add_action('template_redirect', 'search_url_rewrite_rule');=
[/php]


This function will now rewrite every search results URL. Which will something look like this:-

http://yoursite.com/search/term

I have just implemented this technique on Webs Tutorial.

Wordpress plugin directory problem

Some of you may not be aware of this, but there was a small security breach in the WordPress.org plugin directory this week. Someone managed to commit malicious changes to some popular plugins by hacking into the SVN repository. The WP crew discovered the changes and managed to revert the affected plugins back to the last legitimate release. They have also shut down access to the repo to make sure no other plugins were affected.

You can read more about in the WP blog: http://wordpress.org/news/2011/06/passwords-reset/

Strange enough, the plugin we were going to review this week, BackWPup has been removed completely from the repo. After doing a bit of research it seems like there was a little issue with the plugin causing excessively long processes while backing up WP and the author is aware and working on a fix.

You can read more about that in the support forum: http://wordpress.org/support/topic/plugin-backwpup-run-away-backup-process

If the plugin is updated and works well enough, we will do another review and post about it, but for now, this weeks plugin review will be replaced with this article about the few plugins that were affected by the repo hack.

So if you are using WP Touch, W3 Total Cache or Add This make sure to update again to the most recent release which is the clean one. Also, if you are a member of WP.org, you will have to reset your password as a “prophylactic measure” to help heighten security.

How to Remove Images from a WordPress Post

This is a simple way to remove images from wordpress post. just add this piece of code.
[php]<?php
function remove_images( $content ) {
$postOutput = preg_replace('/<img[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_images', 100 );
?>[/php]
If you include this in your functions.php it will remove every image from the content everywhere the_content() function has been used. That isn’t probably what you want, but what you can do is include this function in a specific page template, such as your index.php file, and then at the end of that file include the following to remove the filter:

[php]
<?php remove_filter( 'the_content', 'remove_images' ); ?>

[/php]

Jul 6, 2011

Add Sidebar to TwentyEleven Theme

This Monday WordPress released its new version, WP 3.2, with a brand new HTML5 default theme, Twenty Eleven.
But after activating this theme, you will find that the sidebar from pages and posts pages .i.e. single.php is missing,

This theme by default doesn't has sidebar in pages and posts.

To add them install Twenty Eleven Child Theme.
With this you will be able to add widgets on sinle.php or Posts page.

Download Twenty Ten Child Theme

[download id="2"]

 

Please Download the new Child Theme, which Activates Sidebar on Posts and also on Pages

Jun 29, 2011

Wordpress-Publish new post redirecting to homepage | While saving edited code redirecting to home-page

Wordpress-Publish new post redirecting to homepage | While saving edited code redirecting to home-page

This is an hectic bug for us, whenever a user tries to publish the post or save any code changes, it redirects to the homepage without publishing and saving the code,

This problem I also faced, many people recommends of redirection plugin fixes, but in many cases people are not using that, and then also they facing this kinds of problems.

The only solution to this is writing a simple code in .htaccess file

[php]<IfModule mod_security.c> SecFilterEngine Off SecFilterScanPOST Off </IfModule>[/php]

 

Just copy paste the above code and then done, you are free to publish post and save the codes without any home-page redirection.
This usually happens when on server, some changes are made on mod_security rule.So next time if you face this problem, no worries, just follow this post