Blog – Page 5

How to Create Custom WP-CLI Commands

Recently I’ve been improving my Simple WordPress Crossposting plugin with some custom WP-CLI commands and got a thought in my mind – “why not create a tutorial about that?”

So here it is, below I am going to show you different examples of custom WP-CLI commands with subcommands, arguments and flags.

Tapping into switch_to_blog() Function Performance

It just happened that in my WordPress-related work I focused mostly on WordPress Multisite. I had a couple of big client projects in the past, then I developed WordPress plugins for multisite specifically, for example this one or this another one and here we go.

So I have quite a picture in my mind how the things should work the best.

In this tutorial I am about to talk about switch_to_blog() function, especially about its usage within a loop:

  • How does it work exactly?
  • Is it slow?
  • Can we improve it somehow?

First of all let me show you a code snippet I am talking about.

Importing Products in Multisites

Recently I was testing my new plugin related to a multisite network of WooCommerce stores and noticed that before doing a product import into one of the stores in a network you first need to do some additional network configuration.

I mean if you just go to a product import and try to upload a CSV file for example, you will definitely get an error like that:

product import error when importing WooCommerce product in a Multisite network
Trying to upload a CSV or TXT import file will cause this error when running a WooCommerce store within a multisite network. The error message can be like on the screenshot “Sorry, you are not allowed to upload this file type” or “Sorry, this file type is not allowed for security reasons”.

Understanding HPOS (High-Performance Order Storage)

In this tutorial I would like to guide you through the High-Performance Order Storage (HPOS) feature in WooCommerce – how to turn it on for your store, how it may affect it and how to update your plugins in order to make them work with the latest WooCommerce versions.

How to Copy a Page from One WordPress Site to Another

In this tutorial I would like to cover two ways how you can copy a WordPress page from one site to another. I am not going to use the export and import approach, because I think it is too complicated and requires extra steps which we normally don’t need.

And of course everything in this tutorial applies not only to regular WordPress pages, but also:

  • pages with tons of custom fields (ACF, Carbon Fields, Simple Fields etc),
  • pages, created with page builders (Elementor, WPBakery, Beaver Builder etc).
  • pages, created with either classic or the block editor.

So let’s get started now.

Bulk Update Product Stock Quantities

In the previous tutorial we already discussed updating WooCommerce product stock quantities programmatically when we covered how to do it with REST API specifically.

The whole idea can be deconstructed into two simple things:

  1. Connect to woocommerce_product_set_stock (or woocommerce_variation_set_stock action hooks for product variations).
  2. Send a REST API request to /wc/v3/products/{$product_id} (or /wc/v3/products/{$product_id}/variations/{$variation_id} for product variations).

That’s pretty much it. But there is a “but”. If a specific WooCommerce order in your store can contains a lot of products, then you have a problem. For example, there are 50 products in a single order, then guess what? 50 REST API requests are going to be sent in order to sync all the products stock quantities.

So there is no other way than to update the stock quantities in bulk. But how to do it?

Disable REST API without Plugins

Before I provide you a code snippet which you can copy and paste to your functions.php or something I would like to discuss why do even need to disable JSON REST API in WordPress?

The long story short is to provide less information about your website to those who shouldn’t probably have it. For example if we add at the end of site URL /wp-json/wp/v2/users, we can list all the registered users! Without emails of course but anyway.

If you want to prevent this from hapenning, you can either disable WordPress REST API completely or just its specific endpoints. And that’s what we’re going to do in this guide.