Polylang AI Translation Plugin

I proudly announce you my AI GPT extension for the the popular Polylang translation plugin (700.000 active installs). With the plugin you can translate all your posts, products, post types, pages, terms, categories by one click. The WordPress Polylang AI translation is able to translate records one by one, but also in bulk. It is […]

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

Cartflows get cart total discount

For one of my projects I needed the total discount of the items added in Cartflows to my cart so I could show the customer what he/she would save. I created the following function which loops over all the cart items and substracts the “custom price” from the “regular price”. The custom price is an […]

Create a taxonomy archive page

WordPress does not offer an archive page for taxonomies from its core. By this I mean a page on which an overview can be found of the terms that fall under this taxonomy. Let’s say we have a taxonomy called “topic” and want to show these topics on an archive page. Posts with for example […]

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 […]

How to protect your WordPress forms against spam

The reason I started writing this post is because I see a lot of people searching the internet for solutions to protect their WordPress forms from spam. If you are using a plugin like Contact form 7 or Gravity forms you can use for example the Akismet plugin. But if you build your own custom […]

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 […]

My WordPress base theme

Before the start of every WordPress theme that I develop, I use a simple basis. This is a theme with a number of patterns that I always use. The files and directories css grid.css reset.css responsive.css includes script-enqueue.php helpers.php js main.js images header.php footer.php functions.php index.php style.css CSS directory My css directory consists of 3 […]

WordPress get display name

Depending on whether you have the user’s ID (If not, checkout: Get user ID in every situation) you can get the display name by creating the user object first: When the user object is created you can simply obtain the display name from it as follows:

Get user ID in every situation

WordPress offers a number of options to obtain the user ID. There are various options depending on the situation. There are 3 types of situations: A list of users The current logged-in user A specific user A list of users When iterating over a list of users it is pretty simple to obtain the ID […]

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() […]

Difference between site url and home url

WordPress has 2 inbuilt functionalities to get the site url: The site_url() will always be the location where you can reach the site by tacking on /wp-adminon the end, while home_url() would not reliably be this location. You can also get this same field by using get_bloginfo( ‘wpurl’ ). The home_url() would be where you have set your homepage by setting General […]

Get featured image url

To get the featured image URL from a WordPress post you first have to be clear which URL you should have. There are multiple sizes of the image available. The default size is “post-thumbnail”, but you can also get the “small”, “medium” and “large” sizes, or custom sizes added by add_image_size(). Use the following function […]

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.

Custom post type archive content

One of the much discussed points of WordPress is the missing archive content of custom post types. Where custom taxonomy terms have their own textarea where you can edit the term content, there is for custom post types no such thing. Fortunately we can create functionality by ourself to solve this problem with the help op the wp_options […]

wp_get_nav_menu_items() current menu item

When displaying a menu in WordPress I sometimes find it pleasant to use the wp_get_nav_menu_items() function. This function returns only an array of menu item objects of the corresponding menu. So it allows you to build your custom menu exactly the way you want, without dealing with WordPress predefined output. However, if you would use the function wp_nav_menu() you benefit […]

Programmatically 301 redirect by url

There are times when you need to remove posts from your website and want to redirect them to another post. You can simply do this by checking the old post’s ID and set a 301 redirect to another post and keep the old post in your database. This basically gives the right result, but with larger projects […]

Extending admin search with post meta

The default search functionality in your WordPress admin is not based on post meta. To be able to search by post meta you need to extend the posts_join and posts_where query functions by adding a filter on both. First I will add the filter for posts_join as follows: Then I add the filter for posts_where: You can add these […]

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.

Extend search with predefined search query strings

Almost every WordPress developer had some annoyance with the basic search functionality of WordPress. Today I customised the search functionality for one of my projects so I am able to add a list of comma separated search query strings to a post where I want it to be found on. So for example a product like “bicycle” needs to […]

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.).