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

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

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

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

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

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