WordPress ショートコードプラグインのサンプル

WordPress ショートコードプラグインのサンプルを作ってみました。

細かい説明はしていませんが、雛形として使えるはずです。このプログラムに含まれる関数などをキーワードにして調べれば、大体わかるはず。simple_shortcode.phpという名前で保存して、$WORDPRESS/wp-content/plugins/simple_shortcode/ へ置きます。

<?php
/*
  Plugin Name: Simple Shortcode
  Plugin URI: https://www.hiro345.net/blogs/hiro345/
  Description: ショートコードを追加
  Version: 1.0
  Author: hiro345
  Author URI: https://www.hiro345.net/blogs/hiro345/
 */

// ショートコード処理用関数
function simple_shortcode($atts) {
    extract(shortcode_atts(array(
                'id' => '001',
                'width' => 600,
                'height' => 400,
    ), $atts));
    $html = <<< HERE
<div>Hello, This is a message from Simple Shortcode</div>
HERE;
    $html .= esc_attr(get_option('simple_shortcode_text'));
    return $html;
}

// ショートコード処理用関数の登録。これで [simplesc] が使えるようになる。
add_shortcode('simplesc', 'simple_shortcode');

// ネストするショートコード処理用関数の登録。
// これで [simpleblsc]text[/simpleblsc]のようなショートコードが使えるようになる。
function simple_block_shortcode($atts, $content = null) {
    extract(shortcode_atts(array(
                'key1' => '',
    ), $atts));
    $content = do_shortcode($content);
    if (isset($content)) {
        $values = explode('|', $content);
        $count = count($values);
        $key1 = ($key1) ? " key1=\"${key1}\"" : 'key1=null';
        $html = "<div>key1 ... ${key1}</div><p>";
        for ($i = 0; $i < $count; $i++) {
            $html .= "value:";
            $html .= $values[$i];
            $html .= ", ";
        }
        $html .= "</p>\n";
        return $html;
    } else {
        return '';
    }
}

// ネストするショートコード処理用関数の登録。これで [simpleblsc] が使えるようになる。
add_shortcode('simpleblsc', 'simple_block_shortcode');

// プラグイン用管理画面で設定する値の初期化
function simple_shortcode_init_options() {
    if (!get_option('simple_shortcode_installed')) {
        update_option('simple_shortcode_num', -1);
        update_option('simple_shortcode_text', 'simple_shortcode');
        update_option('simple_shortcode_installed', 1);
    }
}

// プラグインアクティベーション時の処理
register_activation_hook(__FILE__, 'simple_shortcode_init_options');

// プラグイン用管理メニューの登録処理
function simple_shortcode_add_admin_menu() {
    add_submenu_page('plugins.php', 'simple_shortcodeの設定', '[simple_shortcode]の設定', 8, __FILE__, 'simple_shortcode_admin_page');
}

// プラグイン用管理メニューをメニューへ登録
add_action('admin_menu', 'simple_shortcode_add_admin_menu');

// プラグイン用管理画面
function simple_shortcode_admin_page() {
    if ($_POST['posted'] == 'Y') {
        update_option('simple_shortcode_num', intval($_POST['simple_shortcode_num']));
        update_option('simple_shortcode_text', stripslashes($_POST['simple_shortcode_text']));
    }
    if ($_POST['posted'] == 'Y') :
        echo '<div class="updated"><p><strong>設定を保存しました</strong></p></div>';
    endif;
?>

<div class="wrap">
<h2>simple_shortcodeの設定</h2>
<form method="post" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
    <input type="hidden" name="posted" value="Y">
    <table class="form-table">
        <tr valign="top">
            <th scope="row"><label for="simple_shortcode_num">simple_shortcode_num<label></th>
            <td>
                <input name="simple_shortcode_num" type="text" id="simple_shortcode_num" value="<?php echo get_option('simple_shortcode_num'); ?>" class="regular-text code" /><br />
                simple_shortcode_num 数値を指定します。
            </td>
        </tr>
        <tr valign="top">
            <th scope="row"><label for="simple_shortcode_text">simple_shortcode_text<label></th>
            <td>
                <input name="simple_shortcode_text" type="text" id="simple_shortcode_text" value="<?php echo esc_attr(get_option('simple_shortcode_text')); ?>" class="regular-text code" /><br />
                simple_shortcode_text 文字列を指定します。
            </td>
        </tr>
    </table>
    <p class="submit">
        <input type="submit" name="Submit" class="button-primary" value="変更を保存" />
    </p>
</form>
</div>
<?php
}
?>

アンインストール機能を提供するためには、uninstall.php を用意しておきます。これも$WORDPRESS/wp-content/plugins/simple_shortcode/におきます。

<?php
	delete_option('simple_shortcode_num');
	delete_option('simple_shortcode_text');
	delete_option('simple_shortcode_installed');
?>

用意ができたら、プラグインを有効化してから、エントリーに次のように記入してみましょう。それぞれのところで処理が実行されるはずです。

WordPress へようこそ。これは最初の投稿です。編集もしくは削除してブログを始めてください !

[simplesc]

[simpleblsc key1="value1"]1|2|3|4[/simpleblsc]

同じタグの記事: Linux
同じタグの記事: PHP
同じタグの記事: WordPress
同じカテゴリの記事: Program
関連書籍: PHP
関連書籍: WordPress