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
order-by-field.sql
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()

<?php
// Hack due https://core.trac.wordpress.org/ticket/23431 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
functions.php
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 https://github.com/WordPress/WordPress/blob/9041e7a07150b10ed28ec274374ce7d397b0ac9b/wp-includes/load.php#L203-L206

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/(.+)$ http://www.example.com/wp-content/uploads/$1 [R=301,L]
</IfModule>

view raw
htaccess
hosted with ❤ by GitHub

How to create .pot file from source code

I just learned how to create a .pot from https://wordpress.stackexchange.com/a/169282/33599

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

cd /tmp
svn co http://develop.svn.wordpress.org/trunk 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
bz_capabilities
bz_user_level
bz_dashboard_quick_press_last_post_id

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

Proof from the source code at https://github.com/WordPress/WordPress/blob/dd46042e3c3b43c5ee3c17862213b0280fb5abfd/wp-includes/class-wp-user.php#L787

Cleanup WordPress database

Handy cleanup SQL for WordPress database.

Delete orphan postmeta
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts p ON pm.post_id = p.ID
WHERE p.ID IS NULL
;
Delete orphan attachment
DELETE p1
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
delete-orphan.sql
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 WP.org 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.