DevKinsta Docker WPCLI with PHP 8.1

I needed PHP version 8.1 for my CLI in DevKinsta Docker, but it was 7.4. So I found this quick way to change to version 8.1. Login as super user $ docker exec -u 0 -it devkinsta_fpm bash Change PHP version CLI $ sudo update-alternatives –set php /usr/bin/php8.1 Login as www-data user $ docker exec […]

Update wpseo (Yoast) term title and description programmatically

Yoast metadata terms are stored in a row in the options table called “wpseo_taxonomy_meta”. The meta value of this option row contains a serialized string with all the SEO metadata for each term. To update the metadata of a WordPress term you can use the WPSEO_Taxonomy_Meta::set_value( $term_id, $taxonomy, $meta_key, $meta_value ) function. To for example […]

Add content behind admin order line item meta

There are moments when you need to add content behind the order line items in your woocommerce admin. In my case for example I needed this to show a reference id stored under a product. You can get the product id from the $item->get_data(); array.

Rewrite product and shop base the same

By default, Woocommerce uses a different base permalink structure for product and shop. But suppose you want to be able to use the following URLs Shop: /products Shop category: /products/product-category/ Product: /products/product-category/product-name …then add the following permalinks programmatically to your functions.php

Add Product Rich Snippets to WordPress Programmatically

To make structured product data such as prices or reviews as clear as possible for Google, it is wise to implement Google rich snippets on every product page on your website. Google offers 3 ways to add structured data to your website. JSON-LD, RDFa and Microdata. I prefer JSON-LD because this is the first option […]

WooCommerce price based on user role

One of my clients needed different product prices for their users based on their role. His website included a user role for resellers who have to see lower prices than default customers. To modify the price based on the user role I needed 2 things to know: How to check the current user’s role? What […]

WordPress user has role

In some situations it is very helpful to know whether a WordPress user has a specific role (or not). For example to show a different sidebar based on the user’s role or add a discount to the cart total when the current user is a reseller of your products and you want to give him […]

WooCommerce PDF invoice plugin disable attachment by user role

A customer of mine wanted to use the WooCommerce PDF Invoices & Packing Slips plugin, but wanted to make a small adjustment before he could use the plugin. WooCommerce PDF Invoices & Packing Slips plugin This WooCommerce extension automatically adds a PDF invoice to the confirmation email of the order that is sent to your […]

WordPress get domain

There are times when you only want to use your domain to show in your footer, for example, or use it as a dynamic logo. In order to obtain the domain you need your site url and then retrieve the protocols here. You can do this in the following ways: The reason I use site_url() […]

SQL delete orphaned post meta data

When using lots of plugins and different themes or when testing your own custom made theme or plugin, a lot of post meta can remain in your database that is not linked to a post. By applying the following query to your database, you can delete all meta data that is no longer linked to […]

Get page ID by template

A useful functionality in WordPress is the use of page templates. This allows you to create custom made template that you specifically link to a specific page. Sometimes it is useful to check which pages are attached to this template. For example, if you want an overview of exactly those pages or if you want […]

WordPress functions outside WordPress

Today I was working on a project where I needed to use WordPress functionality outside of WordPress. After searching the web, Thanks to this Stackoverflow question I found out that I needed to include the wp-load.php file to get this done. To get the wp-load file, you need the path to it. So here came the next problem; […]

SQL query to add post meta for each WordPress post

To add post meta to all posts (posts with post type “post”) in your WordPress database you should use the following SQL query: To add post meta to another post type, simply change the post type in the WHERE clause.

Get nav menu items by location

WordPress default get_nav_menu_items() function uses the menu name as identifier to retrieve the right menu items. I think it is better to retrieve the menu items by location since the menu name is a value which can be changed in the database and the location is hardcoded in your functions.php file by registering the menu.

Get terms with a parent

With the following function you can return only the terms with a parent term (so are child terms), by post id and taxonomy. Notice that there terms also can be parents when using multiple levels of hierarchy.

Next previous link in menu

In some cases you need to know the next and previous url of your current post in a WordPress menu. For example if you want to build a multiple step form or wizard application based on a WordPress menu. The function below takes the menu name as a parameter and uses the global $post object to […]

Get first term by post id

With the following function you can get the first term of a post by the post’s id. The function requires 3 parameters: $post_idThe id of the post to retrieve the term from. $taxonomyThe taxonomy to retrieve terms from. $outputWhat you want to return from the term. (term_id, name, slug, description etc.).