Android & Wordpress Help center

Archive for the category “Wordpress”

WordPress page rewrite

Lately I was working on rewriting a WP page url so that the browser address that is loaded by a user doesn’t changes but internally the request is sent to some other page.

After spending hours on this and trying every possible .htaccess rules that I have knowledge of I finally came accross this super amazing WP class called WP_Rewrite.

Although the life didn’t become much easy when I found this class, but I had this feeling some where deep inside me that this class of WP  will prove to be a life saver for me.. and yes it did!

What I was trying:

1)  User loads a url:

and gets served a page which actually resides here:

without the url in address bar getting changed.

What did the trick: 

I added a WP filter called page_rewrite_rules, and coded a method which added my custom rewrite rules to the array of WP rewrite rules.

The code is something like this:

add_filter( ‘page_rewrite_rules’, ‘my_page_rewrite_rules’ );
function my_page_rewrite_rules( $rewrite_rules )
// The most generic page rewrite rule is at end of the array
// We place our rule one before that
end( $rewrite_rules );
$last_pattern = key( $rewrite_rules );
$last_replacement = array_pop( $rewrite_rules );
$rewrite_rules += array(
‘first-post/hello-world/?$’ => ‘index.php?pagename=hello-world’,
$last_pattern => $last_replacement,
return $rewrite_rules;

Now to make this rule apply to your WP installation, you need to flush out the existing rewrites so that new rewrites are generated. I avoided a little coding here and followed this procedure:

1) Go to your site’s permalink settings.

2) Change your settings to default and save.

3) Change your settings to YOUR OWN CUSTOM SETTINGS that you are using on your website.

The above 3 steps will flush and re-create the rewrite rules for your site and the new rewrite will come into effect.

Happy pressing!

Add new tab in wordpress media gallery

Adding this snippet to the functions.php of your wordpress theme will add a new tab to the featured image popup called mime types. Clicking on this tab will display a list of the allowed file types however this could be changed to display almost any information.

add tab to wordpress media gallery

Kevin Chard

The $post attribute of wordpress

The wordpress $post object has following attributes :


WordPress : Add administration/settings menu for plugins

To add an administration menu, you must do three things:

  1. Create a function that contains the menu-building code
  2. Register the above function using the “admin_menu” action hook (if you are adding an admin menu for the Network, use “network_admin_menu” instead)
  3. Create the HTML output for the page (screen) displayed when the menu item is clicked

It is that second step that is often overlooked by new developers. You cannot simply call the menu code described; you must put it inside a function, and then register the function.

Here is a very simple example of the three steps just described. This plugin will add a sub-level menu item under the Settings top-level menu, and when selected, that menu item will cause a very basic screen to display. Note: this code should be added to a main plugin PHP file or a separate PHP include file.

add_action( 'admin_menu', 'my_plugin_menu' ); 
function my_plugin_menu() { 

// My Plugin Options => Page title
// My Plugin => Label in sub-menu
// manage_options => capability
// my-unique-identifier => page identifier 
// my_plugin_options => call back function name

add_options_page( 'My Plugin Options', 
'My Plugin', 
'my_plugin_options' ); 

function my_plugin_options() { 
if ( !current_user_can( 'manage_options' ) ) { 
 wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 

echo '<div>'; echo '<p>Here is where the form would go if I actually had options.</p>'; 
echo '</div>'; 


In this example, the function, my_plugin_menu(), adds a new item to the Administration menu via the add_options_page function. Note: more complicated multiple menu items can be added, but that will be described later. Notice the add_action line–that invokes the hook which “registers” the function, my_plugin_menu(). Without that add_action, a PHP error for “undefined function” will be thrown when attempting to activate the plugin. Finally, the add_options_page code refers to the my_plugin_options() function which contains the actual page to be displayed (and PHP code to be processed) when someone clicks the menu item.

The actual detail of these processes is described in more detail in the sections below. Remember to enclose creation of the menu and the html page in functions, and invoke the admin_menu hook to get the whole process started!

The above code will create a sub-menu under the Settings tab with the name “My Plugin”

References :

WordPress : get customised widget area

At times we want our own custom widget area, which we can customise according to our needs (rather than relying on the stock WP widget areas).

This tutorial helps you to create your own custom widget area.

Here is how it is done :

1. Register The Widget

To register the new widget you need to open the functions.php file in your WordPress themes editor.

Add the following code to the functions file, making sure you place it in between php tags:

if ( function_exists(‘register_sidebar’) ){
‘name’ => ‘my_mega_menu’,
‘before_widget’ => ‘<div id=”my-mega-menu-widget”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ”,
‘after_title’ => ”,

Give the widget area a name – e.g. my_mega_menu and in the before/after options you can place code which you wish to appear wrapped around either the widget itself or the title.

Since we are wanting to use one of the menu widget themes, which come with plugins we dont want to wrap it in the standard widget tags, which may interfere with the CSS.

For this example we have just wrapped it in a div tag with the id – my-mega-menu-widget.

2. Add The Widget Code To Your Theme

Now we can add the code that will call the widget to the correct location in our theme files. Since we are creating a horizontal mega menu we obviously want to display this in the header.

In your WordPress themes editor open the header.php file.

Find the location where you want to place the menu and add the following code to the header.php file:

<?php /* Widgetized sidebar */
    if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('my_mega_menu') ) : ?><?php endif; ?>

Make sure that you change the “my_mega_menu” text to be the same as the name you gave to your widget area in the functions file.

Save the changes to the header.php file

If you now go to the widget area in WordPress admin you should see a new widget area in the right hand column with the name of your widget.

You can now add your mega menu, accordion menu or any other widget to this area.

How to get proper HTML formatting with get_post method.

I was coding a custom excerpt function in functions.php and for that i had to use the get_post() method which fetches every details associated with a particular post. I used it to get content of the post id being passed as a param to my custom post excerpt function.

I noticed a very subtle behaviour of get_post() method while using the object that get_post() method returned.  When i used the object to get the post_content, the html formatting that it was providing me was not similar to the one i was getting (for the same post) when i used the_content() method. I was really astonished as to why this was happening !!!

Googling a bit i found that it is some editor filters that the two methods follow to throw the html, which was causing a mismatch in the HTML (post_content) that they were throwing.


Now to get around this situation & to get the same html post_content as the one provided by the_content() method you can do something like this.

In your code instead of doing this :

// ID of your post 

$post_id = 100;

// Get every information about the post

$post_details = get_post($post_id);

// Get the post_content to use it somewhere 

$post_content = $post_details->post_content;

Change the last line where you are fetching the post_content to something like this :

// Get the post_content to use it somewhere 

$post_content = apply_filters(‘the_content’, $post_details->post_content);

Applying the_content() filter will help you get post_content’s HTML formatted the way you would have got when using the_content() method.


Hope it helps some one!


How to show all the posts of a category.

Hi Folks!!!

Its been quite some time since i have posted on this blog.

Was working on wordpress and just came across a scenario where i had to show all the posts belonging to a particular category. 

So here it goes..

Background ::

WordPress uses category.php as a template file  to show category pages. category.php can be modified to show all the posts belonging to that particular category. Now the catch here is that THE WHILE loop used to show all the posts belonging to that category limits the number of posts for a single page to be 10 (by default) & if the category has more than 10 pages, then it adds pagination links at the top and bottom of your page.

How to get rid of this :: 

Now if you wan to show all the posts for that category on the single page itself, without any pagination links just do this :

Before the while loop (the one used to loop through all the posts), somehting like this <?php while (have_posts()) : the_post(); ?> , add the following lines –


global $query_string;
query_posts( $query_string . ‘&posts_per_page=-1’ );


and after the while loop ends, add –




What we are doing here is, we are modifying the $query_string  which the WP was using and changing its $post_per_page param to  “-1” (-1 for all the posts), additionally we can specify the value of $post_per_page to whatever number of posts we want & add support for pagination.

To know more about WP pagination stay tuned for my next post.

WordPress: get category list (with lots of customization options)


Displays a list of Categories as links. When a Category link is clicked, all the posts in that Category will display on a Category Page using the appropriate Category Template dictated by the Template Hierarchy rules.

NOTE: wp_list_categories() works in much the same way as the two template tags replaced in WordPress 2.1, list_cats() and wp_list_cats() (both deprecated).

If you need a function that does not format the results, try get_categories()


 <?php wp_list_categories( $args ); ?> 

Default Usage

<?php $args = array(
'show_option_all'    => ,
'orderby'            => 'name',
'order'              => 'ASC',
'show_last_update'   => 0,
'style'              => 'list',
'show_count'         => 0,
'hide_empty'         => 1,
'use_desc_for_title' => 1,
'child_of'           => 0,
'feed'               => 
'feed_type'          => ,
'feed_image'         => 
'exclude'            => ,
'exclude_tree'       => 
'include'            => ,
'hierarchical'       => true,
'title_li'           => __( 'Categories' ),
'show_option_none'   => __('No categories'),
'number'             => NULL,
'echo'               => 1,
'depth'              => 0,
'current_category'   => 0,
'pad_counts'         => 0,
'taxonomy'           => 'category',
'walker'             => 'Walker_Category' ); ?>

By default, the usage shows:

  • No link to all categories
  • Sorts the list of Categories by the Category name in ascending order
  • Does not show the last update (last updated post in each Category)
  • Displayed in an unordered list style
  • Does not show the post count
  • Displays only Categories with posts
  • Sets the title attribute to the Category Description
  • Is not restricted to the child_of any Category
  • No feed or feed image used
  • Does not exclude any Category and includes all Categories
  • Displays the active Category with the CSS Class-Suffix ‘ current-cat’
  • Shows the Categories in hierarchical indented fashion
  • Display Category as the heading over the list
  • No SQL LIMIT is imposed (‘number’ => 0 is not shown above)
  • Displays (echos) the categories
  • No limit to depth
  • All categories.
  • The list is rendered using the Walker_Category class


(string) A non-blank value causes the display of a link to all categories if the style is set to list. The default value is not to display a link to all.
(string) Sort categories alphabetically, by unique Category ID, or by the count of posts in that Category. The default is sort by category name. Valid values:

  • ID – Default
  • name
  • slug
  • count
  • term_group
(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:

  • ASC – Default
  • DESC
(boolean) Should the last updated timestamp for posts be displayed (TRUE) or not (FALSE). Defaults to FALSE.

  • 1 (True)
  • 0 (False) – Default
(string) Style to display the categories list in. A value of list displays the categories as list items while none generates no special display method (the list items are separated by <br> tags). The default setting is list (creates list items for an unordered list). See the markup sectionfor more. Valid values:

  • list – Default
  • none
(boolean) Toggles the display of the current count of posts in each category. The default is false(do not show post counts). Valid values:

  • 1 (True)
  • 0 (False) – Default
(boolean) Toggles the display of categories with no posts. The default is true(hide empty categories). Valid values:

  • 1 (True) – Default
  • 0 (False)
(boolean) Sets whether a category’s description is inserted into the title attribute of the links created (i.e. <a title=”<em>Category Description</em>” href=”…). The default is true(category descriptions will be inserted). Valid values:

  • 1 (True) – Default
  • 0 (False)
(integer) Only display categories that are children of the category identified by this parameter. There is no default for this parameter.
(string) Display a link to each category’s rss-2 feed and set the link text to display. The default is no text and no feed displayed.
(string) Set a URI for an image (usually an rss feed icon) to act as a link to each categories’ rss-2 feed. This parameter overrides the feed parameter. There is no default for this parameter.
(string) Exclude one or more categories from the results. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example. The child_of parameter is automatically set to false.
(string) Exclude category-tree from the results. This parameter added at Version 2.7.1
(string) Only include the categories detailed in a comma-separated list by unique ID, in ascending order. See the example.
(boolean) Display sub-categories as inner list items (below the parent list item) or inline. The default is true(display sub-categories below the parent list item). Valid values:

  • 1 (True) – Default
  • 0 (False)
(string) Set the title and style of the outer list item. Defaults to “Categories”. If present but empty, the outer list item will not be displayed. See below for examples.
(integer) Sets the number of Categories to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT.
(boolean) Show the result or keep it in a variable. The default is true (display the categories organized). This parameter added at Version 2.3Valid values:

  • 1 (True) – Default
  • 0 (False)
(integer) This parameter controls how many levels in the hierarchy of Categories are to be included in the list of Categories. The default value is 0 (display all Categories and their children). This parameter added at Version 2.5

  • 0 – All Categories and child Categories (Default).
  • -1 – All Categories displayed in flat (no indent) form (overrides hierarchical).
  • 1 – Show only top level Categories
  • n – Value of n (some number) specifies the depth (or level) to descend in displaying Categories
(integer) Allows you to force the “current-cat” to appear on uses of wp_list_categories that are not on category archive pages. Normally, the current-cat is set only on category archive pages. If you have another use for it, or want to force it to highlight a different category, this overrides what the function thinks the “current” category is. This parameter added at Version 2.6
(boolean) Calculates link or post counts by including items from child categories. If show_counts and hierarchical are true this is automatically set to true. This parameter added at Version 2.9Valid values:

  • 1 (true)
  • 0 (false) – default
(string) Taxonomy to return.

This parameter added at Version 3.0 Valid values:

  • category – default
  • taxonomy – or any registered taxonomy
(object) Walker class to render the list with. Valid values:

  • object – an instance of a class that extends either Walker_Category or Walker


Include or Exclude Categories

To sort categories alphabetically and include only the categories with IDs of 16, 3, 9 and 5, you could write the following code:

<?php wp_list_categories('orderby=name&include=3,5,9,16'); ?> 

References :

Post Navigation