wp プラグインの作成13 記事内の特定の文字を置き換えるサンプル
管理画面で設定して、投稿画面で文字を置き換えるというプラグインのサンプル。
ソースの中身の主なヤツ
・管理画面で設定するクラス
・投稿記事がDBから読み込まれたときに実行するクラス
・「管理画面で設定するクラス」を呼び出す関数
・「投稿記事がDBから読み込まれたときに実行するクラス」を呼び出す関数
・「管理画面が読み込まれた」というフックの設定。
・「投稿記事がDBから読み込まれた」というフックの設定。
ソース
/**
* @package testZebra
* @version 1.0
*/
/*
Plugin Name: 特定の文字置換え
Plugin URI: https://tips.recatnap.info/
Description: 記事内の特定の文字を置き換えるサンプル
Author: 丸ごとシマウマ
Version: 1.0
Author URI: https://tips.recatnap.info/
*/
/**
* ログイン後(管理ページが開かれたとき)に実行するクラス
*/
class managementChangeCharacter {
function __construct() {
$this->add_pages();
}
function add_pages() {
add_options_page("記事の文字置換え", "記事の文字置換え", 8, __FILE__, array($this, 'adminChgChar'));
}
function adminChgChar() {
//$_POST['replaceChar'])があったら保存
if ( isset($_POST['replaceChar'])) {
$opt = $_POST['replaceChar'];
// データベースの「wp_options」テーブルにレコードの追加や更新
update_option('ChangeCharacter', $opt);
// データベースに保存した絡みのメッセージ
echo '<div class="updated fade"><p><strong>';
_e('Options saved.'); // 「_e()」の中でechoしてるっぽい。戻り値は無い感じ?
echo '</strong></p></div>' . "\n";
}
// DBから読み込み
$repChar = get_option("ChangeCharacter");
$oriChar = isset($repChar["oriChar"]) ? $repChar["oriChar"]: "";
$repChar = isset($repChar["repChar"]) ? $repChar["repChar"]: "";
// タイトル部のHTML
$tmpTitle = '<div id="icon-options-general" class="icon32"><br /></div><h2>テキスト設定</h2>' . "\n";
// フォーム部のHTML
$tmpForm = '';
$tmpForm .= '<form action="" method="post">' . "\n";
$tmpForm .= '<table class="form-table">' . "\n";
$tmpForm .= '<tr valign="top">' . "\n";
$tmpForm .= '<th scope="row"><label for="inputtext">検索文字</label></th>' . "\n";
$tmpForm .= '<td><input name="replaceChar[oriChar]" type="text" id="inputtext" value="' . $oriChar . '" class="regular-text" /></td>' . "\n";
$tmpForm .= '</tr>' . "\n";
$tmpForm .= '<tr valign="top">' . "\n";
$tmpForm .= '<th scope="row"><label for="inputtext">置換え文字</label></th>' . "\n";
$tmpForm .= '<td><input name="replaceChar[repChar]" type="text" id="inputtext" value="' . $repChar . '" class="regular-text" /></td>' . "\n";
$tmpForm .= '</tr>' . "\n";
$tmpForm .= '</table>' . "\n";
$tmpForm .= '<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存" /></p>' . "\n";
$tmpForm .= '</form>' . "\n";
// 出力
echo $tmpTitle;
echo $tmpForm;
}
}
/**
* 投稿ページが開かれたとき
*/
class replaceChangeCharacter {
function __construct() {
}
/**
* DBから投稿記事を読み込んだ内容を「引数:$content」でうけとる。
*/
function replaceContent($content) {
// DBから読み込み
$repChar = get_option("ChangeCharacter");
$oriChar = isset($repChar['oriChar']) ? $repChar["oriChar"]: "";
$repChar = isset($repChar['repChar']) ? $repChar["repChar"]: "";
// 文字を置き換えてリターン
return str_replace($oriChar, $repChar, $content);
}
}
// ======================================
// 呼び出し用
function managementChangeCharacter() {
new managementChangeCharacter();
}
function replaceChangeCharacter($content) {
$con = new replaceChangeCharacter();
return $con->replaceContent($content);
}
// ======================================
// アクションフック一覧
/**
* ログイン後(管理ページが開かれたとき)
*/
add_action('admin_menu', 'managementChangeCharacter');
// ======================================
// フィルタフック一覧
/**
* 投稿ページが開かれたとき
*/
add_filter('the_content', 'replaceChangeCharacter');
?>