Last week, I was working on a problem of the Datepicker formats been really weird on some cases for The Events Calendar plugin, as Modern Tribe does support WordPress minimum PHP required version for it's plugins, the developers have to solve some problems with the Tools that PHP 5.2 provides us.
The enemy! ?
Fatal error: Call to undefined function date_parse_from_format()
On the case of dates, this becomes a real problem since we don't have support for the date_parse_from_format or DateTime::createFromFormat() we needed to be able to parse formats like 15/02/1993 formatted with d/m/Y.
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. Read More
When you are using the WordPress structure to build your plugin from time to time you will need to insert an icon to make your plugin easier on the eye.
A little while ago a few developers and designers found out that using images to display simple Icons was a bad idea, because of the way it scales in to Mobile and the lack of methods to change it's color. These guys found out that the best way was to use .svg files, which are a vector with the drawing instead of a bitmap.
Read More
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?
I saw a tweet earlier today of a user asking how to solve common issue, how to use a single variable inside of the title of my WordPress posts, in this case events.
To understand how to do this task you first need to see that these kind of variables can be used inside of title without been inserted manually on by applying a little concept called filter, which will allow you to hook to a WordPress method and apply X or Y function to solve your issue.
Read More
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.
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
Variables used
We will use only two variables on this plugin:
static protected $_instance = null; -- Holds the static instance of our class after the plugin is initialized
static protected $prefix = 'OnlineNow'; -- Prefix we will use on the Database Options
Class Instance handling
When dealing with plugins in WordPress a good practice is to save a static variable of the main class on a private/protected variable and create a static method to instantiate the class whenever you call this method the first time:
static public function instance(){
null === self::$_instance and self::$_instance = new self;
return self::$_instance;
}
Plugin Initialization
Here is where our plugin will hook into WordPress and do the required actions to allot this plugin to work as we expect.
static public function init(){
// Lock this class to be initialized only once
if ( null !== self::$_instance ){
return false;
}
add_action( 'wp_login', array( self::instance(), 'login' ), 10, 2 );
add_action( 'clear_auth_cookie', array( self::instance(), 'logout' ), 10 );
// Register Shortcodes
add_shortcode( 'online:list', array( self::instance(), 'shortcode_list' ) );
add_shortcode( 'online:qty', array( self::instance(), 'shortcode_qty' ) );
add_filter('widget_text', 'do_shortcode');
return true;
}
Register when a User logs in
We need to register when a user do the login so that we can save it in the database to grab this value and display for the users.
We need a method that will allow us to get the users from the database option, I've also allowed the developer to exclude or include a user to the list.
public function get_users( $include = array(), $exclude = array() ){
// Retrieve the users from Database
$users = get_option( self::$prefix . '-users', array() );
// Parse Shortcode atts to exclude
if ( is_string( $exclude ) ){
$exclude = array_map( 'trim', explode( ',', $exclude ) );
}
// Exclude users based on shortcode attribute
$users = array_diff( (array) $users, (array) $exclude );
// Parse Shortcode atts to include
if ( is_string( $include ) ){
$include = array_map( 'trim', explode( ',', $include ) );
}
// Include users based on shortcode attribute
$users = array_merge( (array) $users, (array) $include );
// Garantee that the array is safe for usage
$users = array_unique( array_filter( (array) $users ) );
// Remove all non existent users
$users = array_map( array( $this, 'user_exists' ), $users );
// Garantee that the array is safe for usage
$users = array_filter( (array) $users );
return $users;
}
Then I used a method inside of the get_users to check if the user exists before return the list of WP_User objects - on video.
public function user_exists( $user_id ){
$user = new WP_User( $user_id );
// Check if the users exists
if ( ! $user->exists() ){
return false;
}
return $user;
}
List Online Users Shortcode
After we did all the hard work of storing the data about user on the database, now we will use that information to print an HTML with the list of users that are currently online.
public function shortcode_list( $atts ) {
$atts = (object) shortcode_atts( array(
'avatar' => false,
'exclude' => '',
'include' => '',
), $atts );
$users = $this->get_users( $atts->include, $atts->exclude );
$html = '<ul class="users-online">';
foreach ( (array) $users as $user ) {
$html .= '<li>';
// Allow the user to control the avatar size and if he wants to show
if ( is_numeric( $atts->avatar ) ){
$html .= get_avatar( $user , $atts->avatar );
}
$html .= '<span>' . $user->display_name . '</span>';
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
Display the quantity of Users Online
The user might also want to display the quantity of users that are online with a shortcode, so we will deal with that too.
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.
Last week I decided to install Mac OS X Yosemite (v10.10), and as expected a lot of things changed under the hood on Mac OS, so things are still not as easy to install as they were on my Mavericks installation.
Almost all of the problems I faced so far I was able to find articles on how to solve them, but when I was trying to install [fswatch](https://github.com/alandipert/fswatch "Cross-platform file change monitor" "_blank") I had a problem with the GCC compiler.
After digging a bit on Homebrew's GitHub Issues I found that you can edit the GCC formula and apply a patch that will make it work on Yosemite.
The Error
When Homebrew was installing the Bootstrap (make bootstrap) the following error was presented on my terminal:
config.status: executing default commands
make[1]: *** [stage2-bubble] Error 2
make: *** [bootstrap] Error 2
READ THIS: https://github.com/Homebrew/homebrew/wiki/troubleshooting
These open issues may also help:
gcc: compatibility 10.10 (https://github.com/Homebrew/homebrew/pull/31466)
gcc 4.8.3 bottle has invalid omp.h header (https://github.com/Homebrew/homebrew/issues/29670)
MacOS.(gcc|clang|llvm)_version can return nil (https://github.com/Homebrew/homebrew/issues/18781)
Solution
It's easy do resolve the issue, you only need:
brew edit gcc
Then you will find the following:
require "formula"
class Gcc < Formula
def arch
if Hardware::CPU.type == :intel
if MacOS.prefer_64_bit?
"x86_64"
else
"i686"
end
elsif Hardware::CPU.type == :ppc
if MacOS.prefer_64_bit?
"powerpc64"
else
"powerpc"
end
end
end
def osmajor
`uname -r`.chomp
end
Then add the code below the osmajor method definition:
# edit by b.nelissen 20140805
# tobinjones patch gcc https://github.com/Homebrew/homebrew/issues/29845
patch do
url "https://gcc.gnu.org/bugzilla/attachment.cgi?id=33180"
sha1 "def0cb036a255175db86f106e2bb9dd66d19b702"
end
Now you will need to run brew install gcc again and you should be good to go.