exchange words in echo

The PHP echo language construct that outputs the parameters passed. You don’t want to do that in the middle of str_replace. To replace $term->name with an empty string, use: $wptitle = str_replace( $term->name, ”, $wptitle ); Then when you want to print it, use: echo $wptitle;

How to separate categories with commas?

All you have to do is to track if you already echoed some category and add a comma in such case: function get_level($category, $level = 0) { if ($category->parent == 0) { return $level; } $category = get_term( $category->parent ); return get_level( $category, $level + 1 ); } function display_cat_level( $level = 0, $link = … Read more

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’] : ”;

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