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:

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

Change locale for WooCommerce email

To change WooCommerce email language you need to change the locale after order submit.

Using $_GET might be dirty, but works 🙂

<?php
/**
* Plugin Name: Slovak WooCommerce emails
* Plugin URI: https://www.nevilleweb.sk/
* Description: Send only slovak WooCommerce emails
* Version: 1.0.0
* Author: Michal Zuber
* Author URI: https://nevilleweb.sk/
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
add_filter('plugin_locale', 'custom_locale', 10, 2);
function custom_locale($locale, $domain)
{
if ('woocommerce' == $domain && isset($_GET['wc-ajax']) && $_GET['wc-ajax'] == 'checkout') {
return 'sk_SK';
}
return $locale;
}

WooCommerce check if product in cart is in stock

Check if product in cart is in stock:

foreach (WC()->cart->cart_contents as $item) {
    $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
    $product = new \WC_Product_Factory();
    $product = $product->get_product($product_id);
    if ($item['quantity'] > $product->get_stock_quantity()) {
        $inStock = false;
        break;
    }
}

WordPress custom search for custom post type

After creating a custom post type for an Inventory plugin the client needed also customized search for the columns.

Custom search for custom post type

First the pre_get_posts looked as a good candidate for hooking, but ended up with filtering via posts_clauses_request

add_filter('posts_clauses_request', 'customSearchClause');
function customSearchClause($query)
{
    global $wp_query;

    if (!is_search()) {
        return $query;
    }

    $term = $wp_query->query['s'];
    $query['join'] = 'INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)';
    $query['where'] = "AND wp_posts.post_type = '$this->post_type'
    AND (
        (wp_posts.post_title LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product' AND wp_postmeta.meta_value LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product_code' AND wp_postmeta.meta_value LIKE '%{$term}%')
    )";

    return $query;
}

Use WordPress core as PHP framework

For a task I required using headless WordPress, just the core without a template. Using as a PHP framework. More info about integration is available at http://codex.wordpress.org/Integrating_Wordpress_with_Your_Website

Power tip:
To avoid plugins redirecting after requiring wp-blog-header.php define DOING_CRON const or might be DOING_AJAX or WP_ADMIN. Gist snippet. You could also check defined constants. To avoid redirect you also might redefine wp_redirect() function.

 

<?php

define('DOING_CRON', true);
require dirname(__FILE__).'/wp-blog-header.php';

// Your code goes here...

My first WordPress plugin

Finally finished my first WordPress plugin. With the help of  Woocommerce CSV importer I successfully imported 4175 products into WooCommerce and needed to import attributes. WP All Import is a very good paid alternative (tried there awesome demo), but as a newcomer coder I wanted to learn WP so wrote the plugin that imports those attributes.

virtuemart-import
The plugin to import
product-attributes
Set product attributes
woocommerce-product-attributes
All attributes
database-stats
DB stats for 4175 products with their attributes