ショートコードをクラス化する : WordPress

Pocket

WordPressのショートコードをクラス化する。

shortcode.php

<?php
/**
 * shortcode class
 *
 * @author Sawai Hiroshi
 */
class ShortCode {

    public function __construct()
    {
        add_shortcode( 'br', array( &$this, 'br' ) );
    }

    /**
     * multi line break short code
     *
     * @param array $atts number of line bread
     * Usage [bar num="3"] 3 times line break
     */
    public function br( $atts )
    {
        extract( shortcode_atts(
            array(
                'num' => '1',
            ),
            $atts
        ) );
        /** @var $num */
        $num = intval( $num, 10 );
        $html = '';
        for ( $i = 0 ; $i < $num; $i++ ) {
            $html .= '<br>';
        }
        return $html;
    }
}

functions.php

new shortCode();

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。