Rename Publish button in WordPress admin

There are scenarios where you want the WordPress Admin to behave differently, specially for the custom post types. Imagine you have custom post type called ‘contacts’ and you want to change the button from PUBLISH to SAVE. So how to achieve this without changing the core files?

Add the following code to your functions.php:

Note: Make sure you make a copy of your function.php before placing the code. Just incase if you break something you can always bring it back.

Happy Coding 🙂

How To Fix Incorrect Post Comment Counts In WordPress

If your WordPress comment counts got messed up, whether because of a plugin or you messed with your database manually and did something wrong, then there is always a solution for this 🙂

Comment Counts In WordPress

Here’s how comment counts work in WP:

  • Posts live in a table called wp_posts and each has an ID.
  • Comments reside in a table called wp_comments, each referring to an ID in wp_posts.
  • However, to make queries faster, the comment count is also cached in the wp_posts table, rather than getting calculated on every page load.

If this count ever gets out of sync with the actual number of comments for some reason, WordPress, while still displaying all comments properly, will simply show the wrong count.

How To Find Out Which Posts Are Out Of Sync

Fire up a MySQL shell or your favorite MySQL software and run this query.

It assumes your database is called wordpress and the prefix is wp_, so adjust those accordingly.

The result of this query is a list of posts whose comment_counts differ from the actual number of comments associated with each of them.

The left count is the cached number, while the right one is the right one.

How To Fix The Counts Automatically

Please make a backup of your database before performing any altering queries such as the one below.

The following query will recalculate and fix the comment counts for all posts that are out of sync (ones we just queried for above):

This will fix all your issues regarding comment count for sure.


Happy Coding 🙂

Delete Orphaned Meta Data in WordPress

Delete Post Meta Data

Sometimes while developing a new plugin you have to do lots of database work including creating posts, editing posts, and deleting posts. All of these actions affect the post meta data table and if you aren’t careful with how to manage the post meta data you’ll have thousands and thousands of rows of data. By deleting orphaned post meta data (meta data belonging to posts which no longer exist) you immediately remove thousands of records that weren’t doing anything.

Delete User Meta Data

Source :

Happy Coding 🙂

How To Add Custom Rewrite Rules In WordPress

WordPress rewrite API used to convert URLs from something programmatically convenient to something user and search engine friendly. This article will give you some background information about WordPress URL rewriting principles and API.

What Is Rewriting?

URL rewriting is when you change what content is displayed by the server, normally when you type in a URL the web server will simply search for any files in that location. But with rewriting you can still show the same URL but in the back-end change where the server will look for the content. Therefore we can have a URL of /website-1 but tell the server to display content that is located in /website-1/content/index.php

How WordPress Uses Rewrite For Custom URLs?

When you go to Settings -> Permalinks, you will need to select how you want your URLs to appear on WordPress. You have many different options to choose from the default WordPress will use is ?p={post_id}, this is the value that is stored in the wp_posts table and what the rewrite API will write to, when it wants to display the post data.

When you select a new permalink structure WordPress will store these rules in the wp_options table so it will know how to search the post table for the correct post. This means that we can have URLs with just the post title in them and WordPress will know that it needs to search on the post_name column to find the correct post to display.

WordPress Rewrite API

Imagine you need to integrate custom page in which you can list your products. How can we do this?..First we need to create a new rewrite rule so wordpress can translate to We can use add_rewrite_rule() function which accepts 3 parameters. The first parameter is the URL pattern. The second parameter is the URL replacement and the third parameter is the priority. Let’s explore how to create this rule.

Catching the query variables

Using with plugin Activation and Deactivation

Copy Blog 🙂

Disable Update WordPress nags with Plugin’s and Theme’s Update Notifications

1. To Disable Update WordPress nag :

Insert the following code to the functions.php file of your active theme. It will remove the WordPress update nag e.g. WordPress 3.9.1 is available! Please update now, from the all users dashboard, admin dashboard & from Updates page as well.

2. To Disable Plugin Update Notifications :

Insert the following code to the functions.php file of your active theme. It will remove the update notifications of all the installed plugins.

3. To Disable all the Nags & Notifications :

Insert the following code the functions.php file of your active theme. This code disables all the updates notifications regarding plugins, themes & WordPress completely.

Remove WordPress SEO columns from post table

The WordPress SEO plugin by Yoast is very popular, and for a good reason. It uses solid methodology, and is easy enough for folks to use who may not have a solid background in SEO.

That being said, there is a *lot* of data to be displayed. On the post editor, it’s not a big deal, as it’s contained in a metabox and can be minimized easily. But on the post table, it can crowd out other information, especially if you have other data being displayed in the post column. Below is a function that will remove the columns from the post table.

This same function can be used on any post type, including pages and other custom post types. Simply change the manage_edit-post_columns to manage_edit-YOUR_POST_TYPE_columns

That’s it. The great thing about this function is that you can use it to remove other columns as well, for example the author column (if you’re the only writer on the site).

*UPDATE*  You can also uncheck the boxes on the post table under the ‘screen options’ tab. This will only apply to the current user, however.

Split WordPress Post to Multiple Pages

All bloggers usually come across long blog posts to be published in their blogs. It’s good that you are giving detailed information on topic with your huge blog post. But have you ever thought what impression it makes on your visitors? Don’t make your visitors scared by showing them a long post in a single page, they may end up reading your post just once they scroll through out the length of your post. I have got a better solution for you, just think of split wordpress post into multiple pages.

How to achieve ?

To split wordpress post simply go to new post, put your WHOLE content in editor now ‘SWITCH TO HTML EDITOR’ and paste  <!–nextpage–>  from where you want your content to be shown in next page.


Compatibility Issue with themes

Sometimes pagination is successful but the navigation section does not appear. In other words, your page gets split but you don’t find any link to access them. This is because your present theme is unable to handle the tag for splitting the page. Its a small problem and can be fixed easily. Insert the following line to the theme’s template file.

Often the template file is called single.php. However some themes might not have a separate file for individual posts, and might utilize index.php itself. In this case you will need to edit this file.



WordPress Nested meta_query Using Multiple Relation OR & AND

WP_Meta_Query is a class defined in wp-includes/meta.php that generates the necessary SQL for meta-related queries. It greatly improved the possibility to query posts by custom fields. In the WP core, it’s used in the WP_Query and WP_User_Query classes.

meta_query is used by WP_Query while we have a situation like we have to search for something that is not native by wordpress and was added by custom code known as custom field.

So, when we have a simple search that contains only one meta key to search through the thing is quite easy like below,

After that we got a condition that we have to search through different meta keys having one single value, there comes the role of “meta_query”. For that we have a code as follows,

Finally we got a situation where we have multiple meta_key with more than one conditions. Suppose I am searching a lawyer with his Area of Law and location, and also location is divided in to three parts i.e. city, state and zip. Here I have to select one of the three meta keys like “city”, “state” & “zip” and also I have to compare it with “Area of Law”. Here is the situation where native functionality of WordPress fails to integrate the functionality. So by googling so much I found the following solution that uses both relation keys i.e. OR & AND in single meta_query. Here is the example,

While some say it works and some denies, in my case this query failed completely. In my case I used an array of keys for “key” and compared it with a sting value inserted for the “value”. But whatever I did, totally violets the rule for meta_query as type of key should be a string while I am putting an array. It only gave me a warning that invalid argument for “trim()” as I was providing an array instead of string. Otherwise it created the exact result that was expected. Here is the query,

More exact solution is much appreciated.

A Guide To WordPress Custom Post Types: Create, Display & Meta Boxes

Custom Post Types are a new set of administrative options appearing along with the default post types such as Posts, Pages, Attachments etc. A Custom Post Type can store any type of information. It has a dedicated editor, media uploader and uses the existing WordPress table structure for ease in data management. The main advantage of creating custom post types using the WordPress API is that it equips itself well with existing themes and templates. Custom Post Types are also SEO friendly because of their nifty permalinks.

Why Use Custom Post Types?

Custom Post Types help us to keep different types of posts in different buckets. It separates our regular posts from others. Simple enough!

What Actually Are Custom Post Types?

Suppose you want your blog to have a separate section for Portfolio. By using Custom Post Types you can create a new type of item like Posts and Pages, which will contain a different set of data. It will have a new administration menu, dedicated editing pages, custom taxonomies and many more utilities required for full fledged publishing.

Let’s Create Custom Post Type “Portfolio” For Your New Portfolio Page

Register Custom Function :-

Add the below function to your theme’s function.php. The following line of code is used to execute the custom function named site_portfolio during the initialization phase every time a page is generated.

add_action( ‘init’, ‘site_portfolio’ );

Implement The Custom Function :-

The register_post_type function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including the different sections in the admin. This function takes two arguments: the first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type. Here it’s another array containing the different labels, which indicates the text strings to be displayed in the different sections of the custom post type e.g. name displays the custom post type name in the dashboard, edit and view are displayed in Edit and View buttons respectively. I think the rest are pretty self explanatory.

In the next arguments:

  • 'public' => true determines the visibility of the custom post type both in the admin panel and front end.
  • 'menu_position' => 15 determines the menu position of the custom post type.
  • 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ) determines the features of the custom post type which is to be displayed.
  • 'taxonomies' => array( '' ) creates custom taxonomies. Here it’s not defined.
  • 'menu_icon' => get_bloginfo( 'template_url').'/images/portfolio.png'  displays the admin menu icon.
  • 'has_archive' => true enables archiving of the custom post type.

Icon for Custom Post Type :-

Save a 16×16 pixel icon image in your current plugin folder. This is required for the custom post type icon in the dashboard.

Creating Meta Box Fields for Custom Post Types

The meta box mechanism uses the help of the built in WordPress meta box system and helps to add fields required specifically for the custom post types, without requiring the default custom fields in the editor.

Registering the Custom Function :-

Open your theme’s function.php and add the following function. This registers a function to be called when the WordPress admin interface is visited.

add_action( ‘admin_init’, ‘my_admin_portfolio’ );

Implement The Custom Function For Meta Box :-

Here add_meta_box is the function used to add meta boxes to custom post types. Explanation of the given attributes:

  • portfolio_meta_box is the required HTML id attribute
  • Portfolio Details is the text visible in the heading of the meta box section
  • display_portfolio_meta_box is the callback which renders the contents of the meta box
  • portfolio is the name of the custom post type where the meta box will be displayed
  • normal defines the part of the page where the edit screen section should be shown
  • high defines the priority within the context where the boxes should show

Implementation of the display_portfolio_meta_box Function :-

This code renders the contents of the meta box. Here we have used an object variable that contains the information of each of the portfolio displayed in the editor. Using this object we have retrieved the post ID and used that to query the database to get the associated ‘Description’, ‘Type’ and ‘URL’ which in turn render the fields on the screen. When a new entry is added then the get_post_meta returns an empty string which results in displaying empty fields in the meta box.

Registering A Save Post Function :-

add_action( ‘save_post’, ‘add_portfolio_detail_fields’, 10, 2 );

This function is called when posts get saved in the database.

Implementation Of The add_portfolio_detail_fields Function :-

This function is executed when posts are saved or deleted from the admin panel. Here after checking for the type of received post data, if it is a Custom Post Type then it checks again to see if the meta box elements have been assigned values and then finally stores the values in those fields.

Creating a Custom Template Dedicated to Custom Post Types

  • archive-{post_type}.php
  • single-{post_type}.php
If your custom post type were ‘product’, WordPress would look for archive-product.php to display the archive of posts.
If your custom post type were ‘product’, WordPress would look for single-product.php to display the single or permalink of the post.

If these files are not available in your Theme’s directory WordPress will look for archive.php and single.php, respectively. If even these files are not present it will default to index.php.

Create PHP file named archive-portfolio.php and put the following code in it. It will display all the post having post type “portfolio”.

Now for displaying the detail of particular post create single-portfolio.php

Now create a page in admin named as “Portfolio” (same name as post_type) and save it. Now access your page by your domain name/portfolio.