Personal WP Customization Notes (PWCN)

Records of customizations actually made in WordPress, notes when dealing with problems that occurred, customization of block themes, etc.

How to branch processing depending on whether a specific shortcode is in the body or not

It may not be used much, but if there is a specific shortcode in the body of a post or fixed page. .. .. If it wasn’t there. .. .. I will post a memo on how to branch the process.

I think it can be used as a way to improve page performance by not loading a specific script or CSS without the shortcode in situations where shortcodes are used to perform certain actions.

As an example, determine whether there is a shortcode 〇〇〇 in the body of the text,

  • If there is a shortcode, the string “Shortcode is working” will be displayed at the beginning of the text.
  • If there is no shortcode, the string “No shortcode exists” will be displayed at the beginning of the text.

I will show you the code that will do the same thing.

Determine if a specific shortcode exists in the text and conditionally branch

It will work by adding the code below to functions.php of the theme (child theme), changing the part marked with 〇〇〇 to the actual shortcode, and then saving it.

/***** Depending on the presence or absence of the shortcode, the string will be displayed at the beginning of the text. *****/
function pwcn_has_sc_function($content){
global $post;
//Does it contain the shortcode [〇〇〇]?
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, '〇〇〇' ) ) {
	// What to do when there is a shortcode
	$find_shortcode = 'shortcode is working';
	return $find_shortcode . $content;
}else{
	// What to do when there is no shortcode
	$no_shortcode = 'shortcode does not exist';
	return $no_shortcode . $content;
}
}
add_filter('the_content','pwcn_has_sc_function');

The discriminant function “has_shortcode” used here is not used very often, so I didn’t know about it either.

When I actually tested it, I found that the has_shortcoe function does not determine whether a shortcode exists, but rather determines whether a shortcode that actually works exists. Please note that if you stop the plug-in while it is making a determination, the determination result will be “false” and you may get unexpected results.

You can also do the same thing with this code (I personally find it easier to understand)

$content = get_post_field('post_content'); // Get the content of a post or page
$shortcode = '〇〇〇'; // The name of the shortcode you want to check for existence

if (has_shortcode($content, $shortcode)) {
	// What to do when there is a shortcode
	$find_shortcode = 'shortcode is working';
	return $find_shortcode . $content;
}else{
	// What to do when there is no shortcode
	$no_shortcode = 'shortcode does not exist';
	return $no_shortcode . $content;
}

と紹介しましたが、恐らくこの関数を使うケースは、ショートコードの有無によって、裏側でスクリプトやCSSを読み込むなどの処理をさせる目的が多いでしょう。その場合はWordPress公式のコードリファレンスに掲載されているサンプルコードを参考に機能実装するとよいでしょう。

As I introduced, this function is probably used to perform processing such as loading scripts and CSS behind the scenes, depending on the presence or absence of shortcodes. In that case, it would be a good idea to implement the function by referring to the sample code posted in the official WordPress code reference.

When determining and conditionally branching including reused blocks

The above code does not recognize shortcodes in reusable blocks.

The reason may be that the reusable block is loaded after determining whether there is a shortcode.

To solve this problem, we will switch to a method of re-obtaining the main text including reused blocks and making judgments based on it.

Specifically, it is retrieved again by using a built-in WordPress function called do_blocks.

Below is the code that identifies reused blocks as well.

/***** Depending on the presence or absence of shortcodes, strings are displayed at the beginning of the text (including reusable blocks) *****/
function pwcn_has_sc_reusein_function($content){
global $post;
	$include_reuse = do_blocks( $post->post_content );
//Does it contain the shortcode [〇〇〇]?
if( is_a( $post, 'WP_Post' ) && has_shortcode( $include_reuse, '〇〇〇' ) ) {
	// What to do when there is a shortcode
	$find_shortcode = 'shortcode is working';
	return $find_shortcode . $content;
}else{
	// What to do when there is no shortcode
	$no_shortcode = 'shortcode does not exist';
	return $no_shortcode . $content;
}
}
add_filter('the_content','pwcn_has_sc_reusein_function');

reference:do_blocks

Not limited to this time, when you retrieve the body text with “$post->post_content” and do something with it, you can use “do_blocks( $post->post_content )” as in the code above to create a reusable block. It is convenient because it solves the problem of not being able to make a distinction, and problems are unlikely to occur.

Also, similar to the previous section, you can also write the following to achieve the same behavior.

$content = do_blocks(get_post_field('post_content')); // Get the content of a post or page
$shortcode = '〇〇〇'; // The name of the shortcode you want to check for existence

if (has_shortcode($content, $shortcode)) {
	// What to do when there is a shortcode
	$find_shortcode = 'shortcode is working';
	return $find_shortcode . $content;
}else{
	// What to do when there is no shortcode
	$no_shortcode = 'shortcode does not exist';
	return $no_shortcode . $content;
}

Update (change) history of this article

更新日更新内容
December 15, 2021The page has been published
May 19, 2022Added an introduction section of code that takes reusable blocks into consideration
February 21, 2024Added another description method