How to insert ruby (pronounced kana) using shortcode

PubDate:

/ ModDate:

WordPress Create Shortcodes | Personal WP Customization Notes (PWCN)

<Description based on the Act on Premiums and Representations> The content on this site may include product promotions.

In Japanese, ruby is used to add pronunciations to difficult-to-read kanji. I tried implementing it because it can be made into a shortcode with a fairly simple code.

According to the ruby annotation element, the following is the basic tag structure:

<ruby>Kanji here<rp>(</rp><rt>reading kana here</rt><rp>)</rp></ruby>

The styles are set in major browsers, so you can use it immediately by adding this tag.

As a specification of this HTML tag, in browsers that cannot display it, the reading kana is displayed in parentheses () after the kanji.

Add the following program to output shortcodes to functions.php of the enabled theme.

function pwcn_ruby_shortcode( $atts ){
	$atts = shortcode_atts( array(
	'text' => '',
	'furigana' => '',
), $atts );


	$text = $atts['text'];
	$rubi = $atts['furigana'];

	if(isset( $text )){
		$tx = esc_html( $text );
	}else{
		$tx = '';
	}

	if(isset( $rubi )){
		$rb = esc_html( $rubi );
	}else{
		$rb = '';
	}

	return '<ruby>'.$tx.'<rp>(</rp><rt>'.$rb.'</rt><rp>)</rp> </ruby>';
}
add_shortcode('pwcn-ruby','pwcn_ruby_shortcode');

After adding the code, use the following shortcode in the body.

[pwcn-ruby text="" furigana=""]

If you put the kanji in the parameter “text” and the reading kana (ruby) in the parameter “furigana”, the ruby will be displayed above the kanji.

By the way, it looks like this (I will post it as an image since it may have stopped functioning).

How to insert ruby (pronounced kana) using shortcode|Personal WP Customization Notes (PWCN)

If you try to add ruby to the entire text, it will get quite messy with short codes, so it may be better to limit it to difficult-to-read kanji or things that have a special reading method.

As a side note, you can’t create blocks in WordPress. .. Many people may think that, but the advantage of using a block is that it can be displayed in the editor in the same way as the front end, and can be edited using buttons, etc. Other than that, shortcodes are sufficient. You can make it work.

I don’t think you’ll ever be able to use shortcodes in WordPress, so if you learn how to create a shortcode program by referring to the pages below, I think you’ll be able to expand your range of content creation.


This page is an English translation of the Japanese page. The information on the Japanese page may be more accurate because we maintain the Japanese page as the basis and not all content matches.
You can view the Japanese page by clicking the “View Japanese Page” button on the top right of the page, so please take a look.

If you found the content posted on this page or the reference code introduced helpful, please leave a comment in the comment section at the end of the page, or spread the word on SNS etc.

The methods and codes posted on this page have been confirmed to work in my environment as of the last update date. Please note that in principle, we will not respond even if we receive information about malfunctions or detailed customization methods in the comments section.

Until the end Thank you for reading.

The copyright and ownership of all content published on this page, including texts, images, and codes, belongs to this site, and reproduction is strictly prohibited.