WordPress knowledge sharing

Code to automatically add spaces between Chinese and English numbers in WordPress articles

Our website content is mainly achieved by mixed arrangement of words and pictures. When mixed arrangement of Chinese and English, we don't know whether we have noticed a small detail, that is, adding spaces between Chinese and English numbers in WordPress articles. An article with beautiful and reasonable layout can give visitors a good reading experience and improve the second visit rate; At the same time, search engines also have requirements for the layout of articles when they include and index articles, so it is important to do a good job in the layout of website articles. So today it is implemented in pure code Automatically add spaces between Chinese and English numbers in WordPress articles

The adjustment of adding spaces between Chinese and English numbers will not only help improve the user experience, but also be beneficial to SEO. Search engine spiders will also read articles according to certain rules.

The method is simple. Add the following code in the current theme functions.php file:

//Automatically add spaces between Chinese and English numbers of WordPress articles (write to the database)
add_filter( ‘wp_insert_post_data’, ‘fanly_post_data_autospace’, 99, 2 );
function fanly_post_data_autospace( $data , $postarr ) {
$data[‘post_title’] = preg_replace(‘/([\x {4e00}- \x{9fa5}]+)([A-Za-z0-9_]+)/u’, ‘${1} ${2}’, $data[‘post_title’]);
$data[‘post_title’] = preg_replace(‘/([A-Za-z0-9_]+)([\x {4e00}- \x{9fa5}]+)/u’, ‘${1} ${2}’, $data[‘post_title’]);
$data[‘post_content’] = preg_replace(‘/([\x {4e00}- \x{9fa5}]+)([A-Za-z0-9_]+)/u’, ‘${1} ${2}’, $data[‘post_content’]);
$data[‘post_content’] = preg_replace(‘/([A-Za-z0-9_]+)([\x {4e00}- \x{9fa5}]+)/u’, ‘${1} ${2}’, $data[‘post_content’]);
return $data;
}

The above code is executed when WordPress edits an article in the background, that is, it is automatically executed when publishing, updating, and saving an article. It only takes effect for newly published articles. The content written to the database will be automatically added with spaces, and the objects to be processed are the article title and the article content. As for the previously published articles, only batch updates are needed.

Another method is not to write directly to the database, but to execute when WordPress outputs the content of the article. The code is as follows:

//Automatically add spaces between Chinese and English numbers of WordPress articles (not written to the database)
add_filter( ‘the_content’,’fanly_post_content_autospace’ );
function fanly_post_content_autospace( $data ) {
$data = preg_replace(‘/([\x {4e00}- \x{9fa5}]+)([A-Za-z0-9_]+)/u’, ‘${1} ${2}’, $data);
$data = preg_replace(‘/([A-Za-z0-9_]+)([\x {4e00}- \x{9fa5}]+)/u’, ‘${1} ${2}’, $data);
return $data;
}

This method is implemented only when the front end displays the content, and automatically adds spaces between Chinese and English numbers. At the same time, it is only effective for the content of the article, and the article title cannot be realized.

The above two pieces of code are implemented in different ways. You can use one or both.

Method of manually correcting published articles in batches : Enter the background of wp, check the title and select all the articles on this page, click the inverted triangle next to "Batch Operation", select "Edit">Apply, ignore the middle options, and directly click the update on the right side. This can correct 20 articles on one page at a time, and repeat the operation to quickly correct all the previous articles.

The code of this article comes from the Tear Snow Blog https://zhangzifan.com/wordpress-post-autospace.html

The code that automatically adds spaces between Chinese and English numbers in WordPress articles replaces plug-ins, which saves system resources. You can manually batch correct the published articles. After the first method of using Weieis Blog (that is, writing the modification results into the database), all blog articles have been batch corrected, and the user experience is still very good.

Like( zero )
Article name: Code to automatically add spaces between Chinese and English numbers in WordPress articles
Article link: https://www.vpsss.net/3700.html
Copyright notice: The resources of this website are only for personal learning and exchange, and are not allowed to be reproduced and used for commercial purposes, otherwise, legal issues will be borne by yourself.
The copyright of the pictures belongs to their respective creators, and the picture watermark is for the purpose of preventing unscrupulous people from stealing the fruits of labor.