is_admin returning false in backend in server side rendered block

If you have a server side rendered block in the backend, it is rendered via the REST API endpoint /wp/v2/block-renderer/xyz/blockname. This endpoint calls your render function. In the frontend the render function is called directly. The function is_admin() checks if a backend page was requested. In a REST API Request is no backend page, so … Read more

Pass a PHP variable to another file

The WordPress template functions don’t support passing variables (to my knowledge) by default, so you need to write your own function. For example like this, // functions.php function my_template_include_with_variables( string $path=””, $data = null ) { $file = get_template_directory() . “https://wordpress.stackexchange.com/” . $path . ‘.php’; if ( $path && file_exists( $file ) ) { // … Read more

Edit tag cloud widget number

Add the following to your theme’s function.php. Default values are shown below, except changing ‘number’ from 45 to 15. Only the changed values need to be included, so you can either leave the default values or remove/comment out those lines. For WordPress Tag Cloud widget: function custom_tag_cloud_widget() { $args = array( ‘smallest’ => 8, ‘largest’ … Read more

Include files in functions.php

Including a file inside a subdirectory is the same as any other as long as you know the path to the file. Since it’s possible to move the ‘wp-content’ directory, don’t pass that part of the path to the require statement; instead, use the WP_CONTENT_DIR constant. require_once WP_CONTENT_DIR . ‘/new-directory/my-file.php’; Also, you can leave out … Read more

Woocommerce get selected shipping zone id for the current user

The problem is that WC_Cart get_shipping_packages() method gives an array of shipping packages and the function wc_get_shipping_zone( $package ) expect to have as argument a unique package. So you need to get only one package, using for example php reset() function like: // Get cart shipping packages $shipping_packages = WC()->cart->get_shipping_packages(); // Get the WC_Shipping_Zones instance … Read more