Custom sorting for WooCommerce products order by

I had to really customize the order by of the main WP query. Finally looks like I nailed it.

function pn_woocommerce_product_query( WP_Query $wp_query ) {
if ( $wp_query>get( 'orderby' ) == 'menu_order title' ) {
$wp_query>set( 'meta_query', [
'relation' => 'AND',
'internal_stock' => [
'key' => '_manage_stock',
'compare' => 'EXIST',
'stock_status' => [
'key' => '_stock_status',
'compare' => 'EXIST',
'price' => [
'key' => '_price',
'compare' => 'EXISTS',
] );
$wp_query>set( 'orderby', [
'internal_stock' => 'ASC',
'stock_status' => 'ASC',
'price' => 'ASC',
] );
add_action( 'woocommerce_product_query', 'pn_woocommerce_product_query' );
function pn_posts_orderby_request( $orderby ) {
// Output based on pn_woocommerce_product_query()
if ( $orderby == 'CAST(pn_postmeta.meta_value AS CHAR) ASC, CAST(mt1.meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC' ) {
$orderby = "pn_postmeta.meta_value ASC, FIELD (mt1.meta_value, 'instock', '3days', 'outofstock'), mt2.meta_value ASC";
return $orderby;

view raw
hosted with ❤ by GitHub

Add custom list item to Breadcrumb NavXT

A client wanted an additional list item in his Breadcrumb NavXT

Came up with the following action in theme functions.php:

Fixing YouTube embed in do_shortcode()

Fix interpreting YouTube link to embedded player in do_shortcode()

// Hack due issue
$embed = new WP_Embed();
$content = $embed->run_shortcode( $wp_query->queried_object->description );
echo '<div class="category-description">' . do_shortcode( $content ) . '</div>';

Remove Breadcrumb NavXT list item

In a project I had to remove Products list item from Breadcrumb NavXT

* Remove products breadcrumb
* @param object $breadcrumb_obj Breadcrumb object.
function my_bcn_after_fill( $breadcrumb_obj ) {
if ( is_category() || is_tag() || is_tax() ) {
foreach ( $breadcrumb_obj->breadcrumbs as $key => $item ) {
if ( 'Products' === $item->get_title() ) {
unset( $breadcrumb_obj->breadcrumbs[ $key ] );
add_action( 'bcn_after_fill', 'my_bcn_after_fill' );

view raw
hosted with ❤ by GitHub

Put WordPress in maintenance mode without a plugin

1. In your web root directory (where the wp-config.php is located) create file .maintenance
2. It’s content should be <?php $upgrading = time();
3. You can customize the page by creating wp-content/maintenance.php

See source

Get wp-content/uploads files from live site on localhost

While modifying clients WordPress sites I don’t download the uploads directory contents so to avoid 404s in Chrome’s Developer Tools console I modified .htaccess to redirect those URLs to the live site. Tested in a docker container.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_NAME} localhost
RewriteRule ^wp-content/uploads/(.+)$$1 [R=301,L]

view raw
hosted with ❤ by GitHub

How to create .pot file from source code

I just learned how to create a .pot from

Checkout WordPress development branch via SVN which has a script for creating .pot file.

cd /tmp
svn co wordpress-svn
php wordpress-svn/tools/i18n/makepot.php wp-plugin PATH_TO_PLUGIN PATH_TO_PLUGIN/language/PLUGIN_NAME.pot

makepot.php help

Usage: php makepot.php PROJECT DIRECTORY [OUTPUT]

Generate POT file from the files in DIRECTORY [OUTPUT]
Available projects: generic, wp-frontend, wp-admin, wp-network-admin, wp-tz, wp-plugin, wp-theme, glotpress, rosetta, wporg-bb-forums, wporg-themes, wporg-plugins, wporg-forums, wordcamporg

Sorry, you are not allowed to access this page after wp-admin login

If you see the following text, Sorry, you are not allowed to access this page. after logging into WP admin.
Screen Shot 2017-08-27 at 17.30.52

Then you might have change the database prefix. Usermeta table contains

Where the prefix bz_ is the database prefix specified in wp-config.php

Proof from the source code at

Cleanup WordPress database

Handy cleanup SQL for WordPress database.

Delete orphan postmeta
FROM wp_postmeta pm
LEFT JOIN wp_posts p ON pm.post_id = p.ID
Delete orphan attachment
FROM wp_posts p1
LEFT JOIN wp_posts p2 ON p1.post_parent = p2.ID
WHERE p1.post_parent > 0 AND p1.post_type = 'attachment' AND p2.ID IS NULL

view raw
hosted with ❤ by GitHub

What contributing to WordPress taught me

Now I know why every Automattician should start with happiness engineering. After helping out on forums, #wordpress IRC channel (now Slack) and contributing to WordPress Core they better understand the real user needs/problems. This problem solving hugely helped me with learning. Support has more real life scenarios that I can come up during learning. Hello world examples are too simple to acquire experience.

Wanna get better, then first try helping others. Best way to create outstanding software. At the end the end user is the consumer of the software so it should serve him and for that the developer should know the struggles of the user.

I also found some similar thoughts in the How WordPress Taught Me To Be a Better Developer blog post.