HOME>WEBプログラム覚書>WordPressのショートコードをいろいろなプロジェクトで使いたい。

WordPressのショートコードをいろいろなプロジェクトで使いたい。

WordPressのショートコードって便利なのでWordPress以外でも使いたいのですが、 再利用しにくい実装なので、クラス化しようと思ったら既にしてありました。

参照ページ

そのまま使う場合はこんな感じ。

Badcow\Shortcodes\Shortcodes サンプル

  1. <?php
  2. $sc = new \Badcow\Shortcodes\Shortcodes();
  3.  
  4. // ショートコードハンドラ設定
  5. $sc->addShortcode('origin', function(Array $attributes, $content, $tag_name){
  6.     $attr = '';
  7.     foreach ($attributes as $key => $value) {
  8.         $attr .= sprintf('%s => %s<br />', $key, $value);
  9.     }
  10.     return sprintf(
  11.         '
  12.        <p>
  13.        $attributes:<br />
  14.        %s
  15.        $content:<br />
  16.        %s<br />
  17.        $tag_name: %s<br />
  18.        </p>
  19.        ',
  20.         $attr,
  21.         $content,
  22.         $tag_name
  23.     );
  24. });
  25.  
  26. // パースしてハンドラ実行
  27. echo $sc->process('
  28. 囲み型ショートコード
  29. [origin id="001"]
  30. ここは囲み型ショートコードの内部です。
  31. [/origin]
  32. ');
  33. ?>

PHP 5.3以降は無名関数が使えるので、コールバックが書きやすいですね。PHPらしくないけど。
この一点だけでも5.3以上に上げたくなりますね。

実行結果

囲み型ショートコード

$attributes:
id => 001
$content:
ここは囲み型ショートコードの内部です。
$tag_name: origin

プロジェクト用にクラス化しておくと便利かも。

Shortcode.php

  1. <?php
  2. namespace App;
  3.  
  4. class Shortcode
  5. {
  6.     /**
  7.      * @var \Badcow\Shortcodes\Shortcodes|null
  8.      */
  9.     public $shortcode = null;
  10.  
  11.     /**
  12.      * ショートコードのタグを登録した場合、
  13.      * shortcode_tagname(Array $attributes, $content, $tag_name)
  14.      * という関数を作らないとダメ。
  15.      *
  16.      * @var array
  17.      */
  18.     public $tags = [
  19.         'test',
  20.         'amazon',
  21.         'youtube'
  22.     ];
  23.  
  24.     /**
  25.      * コンストラクタ
  26.      */
  27.     public function __construct()
  28.     {
  29.         $this->shortcode = new \Badcow\Shortcodes\Shortcodes();
  30.         // ショートコードハンドラ設定
  31.         foreach ($this->tags as $value) {
  32.             $this->shortcode->addShortcode($value, [$this, 'shortcode_' . $value]);
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * $textをレンダリング
  38.      *
  39.      * @param string $text
  40.      * @return string
  41.      */
  42.     public function render($text)
  43.     {
  44.         return $this->shortcode->process($text);
  45.     }
  46.  
  47.     /**
  48.      * testショートコード [test att="val"][/test] を
  49.      * 処理する関数。
  50.      *
  51.      * @param array $attributes
  52.      * @param string $content
  53.      * @param string $tag_name
  54.      * @return string
  55.      */
  56.     public function shortcode_test(Array $attributes, $content, $tag_name)
  57.     {
  58.         $attr = '';
  59.         foreach ($attributes as $key => $value) {
  60.             $attr .= sprintf('%s => %s<br />', $key, $value);
  61.         }
  62.         return sprintf(
  63.             '
  64.            <p>
  65.            $attributes:<br />
  66.            %s
  67.            $content:<br />
  68.            %s<br />
  69.            $tag_name: %s<br />
  70.            </p>
  71.            ',
  72.             $attr,
  73.             $content,
  74.             $tag_name
  75.         );
  76.     }
  77.     /**
  78.      * amazonショートコード [amazon att="val"][/amazon] を
  79.      * 処理する関数。
  80.      *
  81.      * @param array $attributes
  82.      * @param string $content
  83.      * @param string $tag_name
  84.      * @return string
  85.      */
  86.     public function shortcode_amazon(Array $attributes, $content, $tag_name)
  87.     {
  88.         $attr = '';
  89.         foreach ($attributes as $key => $value) {
  90.             $attr .= sprintf('%s => %s<br />', $key, $value);
  91.         }
  92.         return sprintf(
  93.             '
  94.            <p>
  95.            $attributes:<br />
  96.            %s
  97.            $content:<br />
  98.            %s<br />
  99.            $tag_name: %s<br />
  100.            </p>
  101.            ',
  102.             $attr,
  103.             $content,
  104.             $tag_name
  105.         );
  106.     }
  107.  
  108.     /**
  109.      * Shortcode::tags で登録しているのに
  110.      * 関数が存在しない場合、メッセージを出力
  111.      *
  112.      * @param string $name
  113.      * @param array $arguments
  114.      */
  115.     public function __call($name, Array $arguments)
  116.     {
  117.  
  118.         echo sprintf(
  119.             '%s::%s() が存在しないかアクセスできません。',
  120.             get_class(),
  121.             $name
  122.         );
  123.         var_dump($name);
  124.         var_dump($arguments);
  125.     }
  126. }
  127. ?>

Shortcode.php

  1. <?php$sc = new Shortcode();
  2.  
  3. echo $sc->render('
  4. 囲み型ショートコード
  5. [test id="001"]
  6. ここは囲み型ショートコードの内部です。
  7. [/test]
  8. ');
  9.  
  10. echo $sc->render('
  11. 自己完結型ショートコード
  12. [amazon asin="xxxxxxxx" category="book"]
  13. ');
  14.  
  15. echo $sc->render('
  16. 自己完結型ショートコード
  17. [youtube id="yyyyyyyy" size="large"]
  18. ');?>

実行結果

囲み型ショートコード

$attributes:
id => 001
$content:
ここは囲み型ショートコードの内部です。
$tag_name: test


自己完結型ショートコード

$attributes:
asin => xxxxxxxx
category => book
$content:

$tag_name: amazon


App\Shortcode::shortcode_youtube() が存在しないかアクセスできません。string(17) "shortcode_youtube" array(3) { [0]=> array(2) { ["id"]=> string(8) "yyyyyyyy" ["size"]=> string(5) "large" } [1]=> string(0) "" [2]=> string(7) "youtube" } 自己完結型ショートコード

今はPHPの探し物はPackagistで探す時代らしいです。

投稿日 2013年8月22日 03:20
カテゴリ PHP
タグ WordPress | ライブラリ
トラックバック URL http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1329

コメント

コメントする
Name
Email Address
URL