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 […]
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 […]
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.
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
To apply a coupon on a Woocommerce price programmatically use the following function.
To show the wc_price() without formatted HTML use the following function:
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 […]
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 […]
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 […]
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 […]
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() […]
To delete all posts by a specific meta key and optionally meta value I run the following SQL query:
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 […]
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 […]
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; […]
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.
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.
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.
With the following function you can check if a page is in a certain 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 […]
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.).
To delete posts with the same post title via SQL I run the following query on the database:
With this SQL query you can select all the posts from a specific term (and post type): With this query you can delete all these posts: