Modify Category Queries in WordPress

WordPress has a lot of powerful tools that will speed up the development process by a great amount, the first one that you will encounter when creating themes is the WP_Query.

For those who are not aware of what is the WP_Query, you can assume its the WordPress tool that will give you control over the Queries made to fetch posts in the database.

On this article I will talk a little bit of how to change and modify your WordPress theme or plugin to allow better control over the WP_Query taxonomies Module, which gives the developer a very easy way to handle the Taxonomy related searches within WordPress.

How deprecate code on WordPress Plugin

As you start to develop plugins to sell or distribute for free on the WordPress plugin repository you will see yourself in one of the worst situations as a product developer.

In order to improve your code you will need to let go of old code and introduce new stuff to the codebase.

You will face the following problem, how will you do it without breaking themes and other plugins that rely on your current code?

How to use AJAX on WordPress

If you already know what is an AJAX request please just jump right to the section where I will talk about how to start coding.

What is an AJAX request

First you will need to understand that an AJAX request is nothing more then a simple GET or POST to an URL that you defined, you will pass some parameters and this URL should answer you with some other data that you will be able to parse on the JavaScript and using it you will be able to give your users the needed visual feedback.

Display online Users on WordPress

Earlier today I ran into a problem where I needed a simple way to display which registered users are online right now on the site.

I’ve done a Video explaining what was my solution and how to apply it to a theme, but I myself hate when I see a video without any code that I can easily take a look at, so below the video you will find a explanation of the code step by step, and in the end the plugin I created for this purpose.

Please note that I will comment further the code on the video, but there are some comments on each one of the snippets here

How to make WordPress Database Case Insensitive

When I started to work with WordPress I didn’t know anything about how databases works or anything related to encoding and character set, but as I started into adventure myself on places like pre_get_posts, and the WP_Query filters such as posts_join and posts_where, I quickly found that understanding how the database works was a key to unlocking better performance.

Once I was trying to do a search on some User meta data and I didn’t know why one of my results where not been shown as I expected so delving further and debugging I found that there an information called Collation which is the set of Rules that your database table will follow.

The problem was this meta data was a city value, and I did not have control over the user input so it needed to be case-insensitive, which was not happening because the Collation was set to be utf8_bin that will make your MySQL compare queries that involve LIKE via a Binary structure so it will A is not equal to a.

This case sensibility issue on WordPress databases or any MySQL is easy to solve, you just need to change the collation of the tables that you want to be insensitive by executing the following SQL query on your database.

ALTER TABLE `wp_posts` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE `wp_postmeta` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; 

Where the _ci at the end will convert the Collation to be Case Insensitive, remember that changing the Collation of a table might lead you to have security problems of people trying to pose as Administrator account, which in WordPress is not the case because there are validations for that but if you are in another system you might want to double check.