Cocoonの吹き出しをショートコードで表示する方法

Cocoonを使っていて、吹き出しを表示させたかったのですが、
プラグインで違うエディタにしていたため、使うのが面倒だったんですよね。。

なので、ショートコードで表示しようと思ったら、ショートコードがなさげ。。

なので、今回は、Cocoonの吹き出しを表示するショートコードを作ってみました!

使い方

[speech title="男性(左)"]テスト![/speech]

使い方は、タイトルを指定して使います。
タイトルに含むものを検索しています!
file
ちなみに、タイトルには]この記号は使わないほうが良いです。
ショートコードの終端文字と同じなので、、

結果

file

実装

add_shortcode('speech', function ($atts, $content) {
    if (!isset($atts['title']) || !$atts['title']) {
        return '「title」が指定されていません。';
    }
    $title = $atts['title'];

    $speech_balloon = get_speech_balloons($title);
    if (!$speech_balloon) {
        return $title . 'で登録されている呼び出しはありません。';
    }

    if (is_array($speech_balloon)) {
        $speech_balloon = $speech_balloon[0];
    }
    ob_start();
    generate_speech_balloon_tag($speech_balloon, $content);
    return ob_get_clean();
});

解説

Cocoon内にあった関数を解説しておこうと思います。

get_speech_balloons

get_speech_balloons($title);

この関数で、登録した吹き出しを検索できます。

generate_speech_balloon_tag

generate_speech_balloon_tag($speech_balloon, $content);

この関数は、検索した吹き出し内容をもとに、
吹き出しのHTMLを作ってくれます。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です