How to set the title separator in Yoast SEO to any character

The title separator in the WordPress SEO plugin "Yoast SEO" can be chosen from all 14 types.As of June 2022
I think title tags are often separated by a vertical bar, but
There was a case where I could only choose the half-width "|" and wanted to
change it to the full-width "|", so
I will introduce how to do that.
Table of Contents
Yoast SEO Settings
From Search Appearance > General > Title Separator
First, select "|" and save.

At this point, the title tag will be output as follows.
<title>Company Profile | Site Title</title>
Add to functions.php
Add the following.
//Change Yoast SEO separator
function my_wpseo_title($title) {
//Replace " | ", " |", "| " etc. with "|"
$title = preg_replace('/\s?\|\s?/u', '|', $title);
return $title;
}
add_filter('wpseo_title', 'my_wpseo_title'); //Change title
add_filter('wpseo_opengraph_title', 'my_wpseo_title'); //Also change og:title
In line 4, '(space)|(space)' is replaced with '|'.
$title = preg_replace('/\s?\|\s?/u', '|', $title);
If you want to change the separator to a different character, please modify the second argument to your preference.
If the separator has been replaced like this, it's a success.
<title>Company Profile|Site Title</title>
Comments
If you found something different in your environment, please feel free to comment.




If this was helpful, we appreciate your support!
Any support received will be used for childcare.
Or support by buying something from the buttons below
(You don't have to buy the linked product.)
Amazon
Rakuten Ichiba
Yahoo! Shopping
PR