Created
February 6, 2014 16:50
-
-
Save snoise/8848058 to your computer and use it in GitHub Desktop.
管理画面からカテゴリを選択して、そのカテゴリの記事の末尾に文字列挿入wordpressプラグイン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: paka3 AddText | |
Plugin URI: http://www.paka3.com/wpplugin | |
Description: 管理画面からカテゴリを選択して、そのカテゴリの記事の末尾に文字列挿入 | |
Author: Shoji ENDO | |
Version: 0.1 | |
Author URI:http://www.paka3.com/ | |
*/ | |
//オブジェクトを生成 | |
new Paka3AddText; | |
//クラス定義 | |
class Paka3AddText { | |
//コンストラクタ | |
function __construct() { | |
add_action('admin_menu', array($this, 'adminAddMenu')); | |
add_filter('the_content',array($this, 'paka3_addtext')); | |
} | |
//管理メニューの設定 | |
function adminAddMenu() { | |
add_menu_page('テキスト追加ページだよ','テキスト追加', 'level_8', __FILE__, array($this,'addIndexText_option_page'), 'dashicons-smiley', 6); | |
add_submenu_page(__FILE__, 'テキスト追加ページだよ', '説明します!', 'level_8', __FILE__, array($this,'addIndexText_option_page')); | |
add_submenu_page(__FILE__, 'テキスト追加ページだよ', 'テキスト入力', 'edit_themes', 'mytheme_setting', array($this,'addText_option_page')); | |
} | |
//表示する内容と処理 | |
function addText_option_page() { | |
//**管理画面「テキスト追加」ページに表示する内容 | |
//1.入力値"paka3_add_text"があった場合の処理 | |
if(isset($_POST['paka3_options']) && check_admin_referer('paka3addtext')){ | |
//更新処理処理 | |
$val=$_POST['paka3_options']; | |
$val['post_category'] = $_POST['post_category']; | |
update_option('paka3_options', $val); | |
//更新メッセージ | |
echo '<div class="updated fade"><p><strong>'; | |
_e('Options saved.'); | |
echo "</strong></p></div>"; | |
} | |
//2.値の設定 | |
$opt = get_option('paka3_options'); | |
$add_text = isset($opt['text']) ? $opt['text']: null; | |
$post_category = isset($opt['post_category']) ? $opt['post_category']: false; | |
//3.表示する内容(HTML) | |
$wp_n = wp_nonce_field('paka3addtext'); | |
echo <<<EOS | |
<style> | |
ul.paka3_ul ul.children{ | |
text-indent:20pt;line-height:150%;} | |
</style> | |
<div class="wrap"> | |
<h2>選択したカテゴリの記事の末尾に、テキストを追加</h2> | |
<form method="post" action=""> | |
{$wp_n} | |
<table class="form-table"> | |
<tr valign="top"> | |
<th scope="row"><label for="add_text">追加するテキスト</label></th> | |
<td><input name="paka3_options[text]" type="text" id="add_text" value="$add_text" class="regular-text" /></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row"><label for="select_category">表示するカテゴリ</label></th> | |
<td><ul class="paka3_ul"> | |
EOS; | |
wp_category_checklist(0, false, $post_category, false, '', false); | |
echo <<<EOS | |
</ul> </td> | |
</tr> | |
</table> | |
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存" /></p> | |
</form> | |
</div> | |
EOS; | |
} | |
//表示する内容と処理 | |
function addIndexText_option_page() { | |
//**管理画面「テキスト追加」ページに表示する内容 | |
echo <<<EOS | |
<div class="wrap"> | |
<h2>「テキスト追加」インデックスページ</h2> | |
入力した文字列を末尾に追加するよー。<br /> | |
「テキスト入力」をクリックしてね | |
</div> | |
EOS; | |
} | |
//入力した文字列を呼び出す関数 | |
function get_text(){ | |
$opt = get_option('paka3_options'); | |
return isset($opt['text']) ? $opt['text']: null; | |
} | |
//選択したカテゴリーを呼び出す関数 | |
function get_post_category(){ | |
$opt = get_option('paka3_options'); | |
return isset($opt['post_category']) ? $opt['post_category']: false; | |
} | |
//記事の末尾に文字列を追加する | |
function paka3_addtext($contentData) { | |
//選択したカテゴリを取得 | |
$cats = $this->get_post_category(); | |
if(is_single()){ | |
foreach(get_the_category() as $cat){ | |
//カテゴリの存在確認 | |
if(array_search($cat->cat_ID,$cats)!==FALSE){ | |
//一つでもあれば返す | |
return $contentData."<h3>".esc_html($this->get_text())."</h3>"; | |
} | |
} | |
} | |
return $contentData; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment