WordPressプラグインのテンプレート

Pocket

WordPressのプラグインを作ろうと色々調べています。
ショートコードを使う方法が簡単ですね。
ショートコードを使ってプラグインを作るときのテンプレートを作ってみました。

<?php
/*
Plugin Name: プラグインテンプレート
Plugin URI: http://stabucky.com/
Description: ショートコードを使うプラグインを作るためのテンプレート。設定で、firstwordを「い」、lastwordを「は」とし、編集で「[myplugin myword="ろ"]」というショートコードを書くと、「いろは」と表示される。
Author: stabucky
Version: 0.01
Author URI: http://stabucky.com/
*/

add_shortcode('myplugin','myplugin_filter');
add_action('admin_menu','myplugin2');
function myplugin2(){
  add_options_page('設定', 'プラグインテンプレート', 8, __FILE__, 'myplugin3');
}
function myplugin3(){
?>
<div class="wrap">
<h2>プラグインテンプレート</h2>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<table>
<tr valign="top">
<th scope="row">firstword</th>
<td><input type="text" name="firstword" value="<?php echo get_option('firstword'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">lastword</th>
<td><input type="text" name="lastword" value="<?php echo get_option('lastword'); ?>" /></td>
</tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="firstword,lastword" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
function myplugin_filter($atts) {
  if(!get_option('firstword')){
    $firstword='';
  }else{
    $firstword=get_option('firstword');
  }
  if(!get_option('lastword')){
    $lastword='';
  }else{
    $lastword=get_option('lastword');
  }
  extract(shortcode_atts(array(
    'myword'=>''
  ),$atts));
  return $firstword.$myword.$lastword;
}
?>

これをテキストエディタに貼り付けて、拡張子「.php」を付けてWordPressのプラグインのフォルダに保存します。アクティベートするとすぐに使えます。
このプラグイン自体は大した意味がありませんが、逆に余計なことがほとんど書かれていないので改造は容易だと思います。

一応、このプラグインの説明を書いておきます。
本文に「[myplugin myword=文字列]」のように書いておくと設定であらかじめ指定しておいた文字列でmywordに指定した文字列をはさみます。
例えば、設定で、firstwordを「い」、lastwordを「は」としておきます。
編集で「[myplugin myword="ろ"]」というショートコードを書いて、公開すると「いろは」と表示されます。

myplugin_filterを適当に書き直せばお好みのプラグインを作ることができます。

[ 2012年8月6日 | カテゴリー: WordPress | タグ: , ]

« | »

コメントを残す

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

送信してください。


タグ

カテゴリー

最近の投稿

最近のコメント

固定ページ

アーカイブ

stabucky

写真

メタ情報