Essential WordPress Tips and Tricks

Written by Kevin Liew on 31 Jan 2014
27,083 Views • WordPress

After I have gone through a few projects with WordPress as the CMS backend, it has became my official CMS platform that I would shamelessly recommend it to my peers. There are a few reasons behind it - good documentation, large pool of plugin resources and wide solution & supports online.

There are tons of documented WordPress solutions out there and they are free technical support to me. Also there are websites dedicated to WordPress, for example - a WordPress resource site including WordPress beginner's guide, SEO, optimization, themes, plugins, web hosting, etc. Previously, I have also shared about WordPress functions and scripts. Again, another round of WordPress coding tips and tricks that I have been using in my projects recently.

Standalone PHP file with WordPress functions

Sometimes, I found myself creating standalone PHP file to achieve certain task. For example, I did a PDF generator with data from the database and I want to use WordPress' database query - $wpdb. What we have to do is, include wp-load.php at the start, like this:

include_once $path . 'wp-load.php';	

You will able to use everything in WordPress. Credit: Using $wpdb in standalone script


AJAX with WordPress

I'm pretty sure you will need this one. It's easy to create AJAX call with jQuery, but what about WordPress' backend coding? Okay, I assume you're using jQuery to make AJAX call and this is how it would look like.

$(function () {

	$('#button').click(function() {
		var name = 'queness';
		   type: "post",
		   dataType: "json",		   
		   url: "/wp-admin/admin-ajax.php", // always use this if you're using WordPress built in AJAX
		   data: {action: "yourAction", name: name},
		   success: function (data) {
			 	// do whatever thing you want
		return false;

Observe Javascript above, in data there is an attribute called action, we need to define a name for it, this is how you tell WordPress which function to load after established an AJAX call. Open up functions.php, you can put the backend code in there.

We're using two hooks wp_ajax_yourAction and wp_ajax_nopriv_yourAction. Depend on your scripts, you can check user's login status and perform different task for different situation.

// this function will be called whenever there's ajax call
dd_action("wp_ajax_yourAction", "functionName");	

// executes for users that are not logged in.
add_action("wp_ajax_nopriv_yourAction", "functionNameWithoutLogin"); 

function functionName() {
	$name = $_POST['name'];
	// you can check user login status here
	if ( is_user_logged_in() ){	
		// do things here
	} else {
    echo "not logged";

function functionNameWithoutLogin() {

	// do things here
  echo "not logged";	


Credit: AJAX in Plugins, Action reference: wp_ajax_(action)


Add Menu Item in WordPress Admin

I was required to create a simple interface to add/edit data. So, it's compulsory to create a menu item for quick access. This is how you do it, you need to wrap add_menu_page in a function and use admin_menu hook.

//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
function new_menu_items(){
    add_menu_page( 'Induction List', 'Induction List', 'activate_plugins', 'induction_list', 'show_page' );
add_action( 'admin_menu', 'new_menu_items ' );

function show_page() {
	//once user click on the menu item, do this


Advanced Custom Fields Premium plugin

One of the reasons that makes me a WordPress developer is because of this extremely useful plugin - Advanced Custom Fields. This plugin certainly make WordPress a better CMS by allowing us to define new fields (all sort of form elements) for post and/or page. One of the must have add-ons is called Repeater field. With this add-on, you will able to create rows of data.

No doubt, ACF makes WordPress a better CMS and makes developers' life easier. Oh, good documentation and code examples too.

There's a very useful article titled conditional logic for advanced custom field written by Elliot Condon. With this simple hack, ACF will be a even better plugin.


Create Native WordPress Admin Table

WordPress has a great table layout, full with useful functionalities - sorting, pagination, search and bulk action. If you write your own plugin, you can reuse this wonderfully built UI component. As I said, WordPress provides front-end UI, backend logic is not part of the package but it's not hard to build.

I won't show you how to do that in here, please use the following links for very detailed tutorials


Database manipulations with $wpdb

WordPress' $wpdb is pretty powerful, it's a class of functions for all database manipulations. If you were required to make a custom query, you will need to read this. It also shows us how to protect queries against SQL injection attacks.


Sessions in WordPress

Sessions are useful to store temporarily data between pages. Some people will just use the native session_start(), but there is a proper way to do it in WordPress:

add_action( 'init', 'custom_session_start' );
function custom_session_start() {
	if ( !session_id() ) {

A few more...

Last but not least, here I have somemore posts that you might find it interesting:

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

orange county web designer 8 years ago
Thanks for all the tips. This really helped. I will put into practice straight away. Thanks for sharing.
Ankur 8 years ago
Good tutorial and thx for the wordpress site WpMatter. Didnt knew about it.
Chen 8 years ago
Sorry for asking but isn't Kevin Liew the famous Bruce Lee relative?
Kevin Admin 8 years ago
Well chen, I have no ideas :)
wordpress developement 7 years ago
Thanks for sharing this information,it's very good.