facebook twitter hatena line email

Cms/wordpress/プラグイン作成方法

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

helloworld

以下ファイルをutf-8で作成すればプラグイン一覧に表示される

wp-content/plugins/helloworld/index.php

<?php
/*
Plugin Name: helloworld
Plugin URI: http://example.com/plugin.php
Description: 本文の上部にhelloworldを表示します。
Version: 1.0
Author: author1
Author URI: http://example.com/author1
*/
// 本文の上部にhelloworldを追加
function content_add_helloworld($content) {
  return sprintf("<p>helloworld</p>%s", $content);
}
// フィルターフック名, フィルター関数名設定 
add_filter('the_content','content_add_helloworld');
?>

アクションフックとフィルターフックドキュメント

  • アクションフック(アクション時にfunctionを噛ます

http://wpdocs.sourceforge.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3_API/%E3%82%A2%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%95%E3%83%83%E3%82%AF%E4%B8%80%E8%A6%A7

  • フィルターフック(表示時にfunctionを噛ます

http://wpdocs.sourceforge.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3_API/%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%83%BC%E3%83%95%E3%83%83%E3%82%AF%E4%B8%80%E8%A6%A7


プラグイン関数をクラス化

class PluginHelloworld
{
  // 本文の上部にhelloworldを追加
  function content_add_helloworld($content) {
    return sprintf("<p>helloworld</p>%s", $content);
  }
}
// フィルターフック名, フィルター関数名設定 
add_filter('the_content',array('PluginHelloworld', 'content_add_helloworld'));