facebook twitter hatena line email

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

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

MVCな感じでプラグインを書く

  • 記事上にhelloworld
  • 管理画面の設定にhelloworld項目を設定

index.php

<?php
/*
Plugin Name: helloworld
Plugin URI: http://example.com/plugin
Description: 本文の上部にhelloworldを表示します。
Version: 1.0
Author: author1
Author URI: http://example.com/author1
*/
require_once dirname(__FILE__) . '/AddFilter.php';
require_once dirname(__FILE__) . '/AddAction.php';
// 追加フック定義
add_filter('the_content', array('PluginHelloworld_AddFilter', 'the_content'));
add_filter('admin_menu', array('PluginHelloworld_AddAction', 'admin_menu'));

AddAction.php

<?php
/**
 * 追加アクションクラス
 */
class PluginHelloworld_AddAction
{
  /**
   * 管理項目メニュー設定
   */
  public function admin_menu()
  {
    require_once dirname(__FILE__) . '/actions/AdminMenuAction.php';
    $plugin = new PluginHelloworld_AdminMenuAction();
    return $plugin->action();
  }
}

AddFilter.php

<?php
/**
 * 追加フィルタークラス
 */
class PluginHelloworld_AddFilter
{
  /**
   * 記事本文フィルター
   */
  public function the_content($content)
  {
    require_once dirname(__FILE__) . '/filters/TheContentFilter.php';
    $plugin = new PluginHelloworld_TheContentFilter();
    return $plugin->filter($content);
  }
}

actions/AdminMenuAction.php

<?php
require_once dirname(__FILE__) . '/AbstractAction.php';
/**
 * 管理メニューアクションクラス
 */
class PluginHelloworld_AdminMenuAction extends PluginHelloworld_AbstractAction
{
  /**
   * アクション実行
   */
  public function action()
  {
    add_options_page('helloworldオプション設定', 'helloworldの設定', 'administrator', dirname(dirname(__FILE__)) . '/views/AdminMenuView.php');
  }
}

actions/AbstractAction.php

<?php
/**
 * 基底アクションクラス
 */
abstract class PluginHelloworld_AbstractAction
{
  public function action()
  {
    dir('no exists action method');
  }
}

filter/TheContentFilter.php

<?php
require_once dirname(__FILE__) . '/AbstractFilter.php';
/**
 * 記事本文フィルタークラス
 */
class PluginHelloworld_TheContentFilter extends PluginHelloworld_AbstractFilter
{
  /**
   * フィルター実行
   */
  public function filter($content)
  {
    return sprintf("<p>helloworld</p>%s", $content);
  }
}

filter/AbstractFilter.php

<?php
/**
 * 基底フィルタークラス
 */
abstract class PluginHelloworld_AbstractFilter
{
  public function filter()
  {
    dir('no exists filter method');
  }
}

views/AdminMenuView.php

helloworld