LoginSignup
0
0

WordPressでカクヨムやなろうのルビ記法を使えるようにする

Posted at

やりたかったこと

WordPressで|文字《ルビ》をルビタグに置き換えたい。

できたコード

functions.php
function convert_ruby_strings($content) {
    $pattern = '/\|(.+?)《(.+?)》/';
    $content = preg_replace_callback($pattern, function($matches) {
        $base_text = isset($matches[1]) ? $matches[1] : '';
        $ruby_text = isset($matches[2]) ? $matches[2] : '';
        return '<ruby><rb>' . $base_text . '</rb><rt>' . $ruby_text . '</rt></ruby>';
    }, $content);
    return $content;
}
add_filter('the_content', 'convert_ruby_strings');

とりあえずこれで、WordPress本文に入力した|文字《ルビ》がちゃんと文字ルビと表示されるようになる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0