Show the section only if custom taxonomy was chosen

You already check, if given post has any terms assigned. All you need to do is to move that check earlier in the code. <?php $terms = get_the_terms( get_the_ID(), ‘colour’ ); if ( $terms && ! is_wp_error( $terms ) ) : ?> <section class=”colour__chart”> <div class=”container”> <div id=”color-chart”> <h2>Colour Chart</h2> <hr class=”divider”> <?php foreach ( … Read more

Array to string conversion error in PHP 7.2 when returning user role as class

This code works and is perfectly valid, running on 7.3, except when your user has two roles: add_filter( ‘body_class’, function( $classes=”” ) { $current_user = new \WP_User(get_current_user_id()); $user_role = [‘administrator’, ‘moderator’]; //just a test, but this is what it’ll look like if it had 2 roles. $classes = []; $classes[] = ‘role-‘ . $user_role; return … Read more

Get a value inside a get_post_meta Array

$banner_subtitle = get_post_meta( $post_id, ‘header_section_meta’, true ); if (!empty($banner_subtitle[‘banner_subtitle’])) { echo $banner_subtitle[‘banner_subtitle’]; } or as a ternary $banner_subtitle = get_post_meta( $post_id, ‘header_section_meta’, true ); echo !empty($banner_subtitle[‘banner_subtitle’]) ? $banner_subtitle[‘banner_subtitle’] : ”;

Programatically upload a file to be stored inside blob field in database, NOT on filesystem

After some further experimenting and talking to a colleague, we found the answer (team effort!) altering how the file handler deals with the file content. // Assign values required $wx_fileName = $_FILES[‘file’][‘name’][$i]; $wx_fileMime = $_FILES[‘file’][‘type’][$i]; $wx_fileSize = $_FILES[‘file’][‘size’][$i]; $wx_tmpf = $_FILES[‘file’][‘tmp_name’][$i]; //$wx_file = fopen($_FILES[‘file’][‘tmp_name’][$i], “rb”); $wx_file = base64_encode(file_get_contents($_FILES[‘file’][‘tmp_name’][$i])); Basically replacing fopen() with file_get_contents() and base64 … Read more

change the default order of posts only for specific categories

Problem 1 $foo = is_category( … ); Is the same as: global $wp_query; // the main query $foo = $wp_query->is_category(….); So when filtering the query, don’t check the main query, check the query you’re filtering instead: $query->is_main_query() && $query->is_category( …. Problem 2 Look at this: array(‘573,1060,1061,1062,1063,1065,1066,1064’) You might think this is a list of 8 … Read more

how to Update 15k products on plugin activation with meta_option

Based on Paul’s original comment, we moved the code execution to a custom wp-cli command. Which also allowed us add arguments and change code behaviour based on arguments. Example code for anyone, including WPML language specification via global $sitepress; $sitepress->switch_lang(“en”); … ‘suppress_filters’ => false, <?php /** * Registers CLI command csa * * Allows to … Read more

Understanding webhooks

I think I managed to fix it add_action(‘rest_api_init’, function(){ register_rest_route(‘endpoint’, ‘/sportivity’, [ ‘method’ => ‘POST’, ‘callback’ => ‘sportivity_endpoint’, ]); }); function sportivity_endpoint($request){ $json = $request->get_json_params(); $json[‘test’] = ‘Jan’; return $json; } When I go to url/wp-json/endpoint/sportivity it returns the json now.