wordpressで夢小説を楽に書きたい その2
前回(https://note.com/rhak/n/n8e821ee5fedc)のjavascriptでゴリゴリするやり方がうちのサイトではだめだったので、しょうがないからプラグイン作ったのでメモよ。
元々やりたかったことは、
1.エディタでは素のテキストが表示されている
2.読む時に変換がかかる
だったんですが、前回の記事に追記した通り、キーワードを素のまま出すか全変換かけるかの2択スクリプトだったので、たとえば「空さん(名前)」と「空(単語)」が同時に出てくるとだめだったんですね。
なので、やっぱり先人のDreamMakerさんがやっているように、「置換用文字列にキーワードを一度置き換える」をやらないとだめだと気が付きました。
というわけで、以下のようにすることにした。
1.エディタで素のテキストを入力するが、保存時に置換文字列に変換される(もし変換したくない単語があったら置換文字列から修正する)
2.読むときに変換がかかる
こうですね。
名前の保存はcookieですが、表示時にphp側でcookieを読んでサーバー側で置換します。
以下、ソース。変換方式としてはDreamMakerさんの二番煎じです。
※下記はプラグインのバックアップなので、もしかしたら動かないかもしれない。なんかおかしかったら自分でいじったりしてみてほしい。
<?php
/*
Plugin Name: Simple Name Change Novel Editor
Plugin URI: https://note.com/rhak/n/nb50706370405
Description: めっちゃ簡単に名前変換して投稿するエディタ
Version: 1.0
Author: RyoHayama
Author URI: https://vault.ivory.ne.jp/
License: GPL2
*/
// カスタム投稿タイプの追加
add_action('init', 'sncne_add_custom_post_type');
function sncne_add_custom_post_type(){
register_post_type(
'novels', // 1.投稿タイプ名
array( // 2.オプション
'label' => '小説', // 投稿タイプの名前
'public' => true, // 利用する場合はtrueにする
'has_archive' => true, // この投稿タイプのアーカイブを有効にする
'menu_position' => 5, // この投稿タイプが表示されるメニューの位置
'menu_icon' => 'dashicons-edit', // メニューで使用するアイコン
'supports' => array( // サポートする機能
'title',
'editor',
)
)
);
}
add_action('init', 'sncne_add_taxonomy');
function sncne_add_taxonomy()
{
//タクソノミー
register_taxonomy(
'novel_category', //タクソノミー名
'novels', //紐付けるカスタム投稿タイプ名
array(
'label' => '連載', //ラベル
'labels' => array(
'add_new_item' => '連載を追加', //追加ボタンラベル
),
'public' => true, //パブリックに公開するか
'show_ui' => true, //管理画面に表示可否(trueにしておく)
'show_in_nav_menus' => true, //ナビゲーションメニューでこのタクソノミーを選択可(true)/不可(false)
'hierarchical' => true //true:階層あり / false:階層なし
)
);
}
add_action('admin_menu', 'sncne_create_custom_fields');
function sncne_create_custom_fields()
{
add_meta_box(
'name_change_setting', //編集画面セクションID
'名前変換項目', //編集画面セクションのタイトル
'sncne_insert_custom_fields', //編集画面セクションにHTML出力する関数
'novels', //投稿タイプ名
'normal' //編集画面セクションが表示される部分
);
}
function sncne_insert_custom_fields()
{
$i = 1;
do{
$getkey = 'name_'.$i;
$getvalue = get_post_meta( get_the_ID(), $getkey, true );
if (isset($getvalue) && $getvalue != '') {
$custom_fields[$getkey] = $getvalue;
$i++;
continue;
}else{
$i--;
break;
}
}while(1);
$len = count($custom_fields);
?>
<script>
var i = <?php echo $len ?> ;
function addInput() {
i++ ;
var input_data = document.createElement('input');
input_data.type = 'text';
input_data.id = 'name_' + i;
input_data.name = 'name_' + i;
var parent = document.getElementById('form_area');
parent.appendChild(input_data);
}
function removeInput(){
let parentnode = document.getElementById('form_area');
parentnode.lastElementChild.remove();
}
</script>
<form method="post" action="admin.php?page=site_settings">
<div id="form_area">
<?php foreach($custom_fields as $key => $value){ ?>
<input id="<?php echo $key ?>" type="text" name="<?php echo $key ?>" value="<?php echo $value ?>">
<?php } ?>
</div>
</form>
<input type="button" value="変換項目追加" onclick="addInput()">
<input type="button" value="変換項目削除" onclick="removeInput()">
<?php
}
add_action('save_post', 'sncne_save_post', 10, 3);
function sncne_save_post($post_id)
{
/* 登録したフックを外しておかないと無限ループになる */
remove_action('save_post', 'sncne_save_post', 10, 3);
if (get_post_type() == 'novels'){
$i = 1;
$content = $_POST['post_content'];
while(1){
$key = 'name_'.$i;
$value = $_POST[$key];
if (isset($value)) {
update_post_meta($post_id, $key, $value);
$content = str_replace($value, '%'.$key.'%', $content);
$i++;
continue;
}else{
break;
}
}
/* 更新するための配列を用意 */
$my_post = array(
'ID' => $post_id, /* IDは必須 */
'post_content' => $content, /* 必要なものを任意で */
);
/* wp_update_post関数で投稿を更新 */
wp_update_post($my_post);
}
/* 外したフックを再度つける */
add_action('save_post', 'sncne_save_post', 10, 3);
}
add_filter('the_content', function($content) {
if (get_post_type() == 'novels'){
$i = 1;
do{
$getkey = 'name_'.$i;
$getvalue = get_post_meta( get_the_ID(), $getkey, true );
$custom_fields[$getkey] = $getvalue;
if (isset($getvalue) && $getvalue != '') {
$i++;
continue;
}else{
break;
}
}while(1);
$term = get_the_terms( $POST->ID, 'novel_category');
$pattern = $term[0]->slug;
$cookiesText = $_COOKIE;
foreach($cookiesText as $key => $value){
if(preg_match("/^$pattern/", $key)){
$newkey = explode('-', $key);
$cookies[$newkey[1]] = $value;
}
}
if(isset($cookies)){
foreach($cookies as $key => $value){
$content = str_replace('%'.$key.'%', $value, $content);
}
}else{
foreach($custom_fields as $key => $value){
$content = str_replace('%'.$key.'%', $value, $content);
}
}
}
return $content;
});
add_action('get_header', 'sncne_setcookie');
function sncne_setcookie() {
if(isset($_POST)){
$term = get_the_terms( $POST->ID, 'novel_category');
if(isset($_POST["submit"])){
foreach($_POST as $key => $value){
if(preg_match("/^name_/", $key)){
setcookie($term[0]->slug.'-'.$key, $value, time()+365*24*60*60, '/');
$_COOKIE[$term[0]->slug.'-'.$key] = $value;
}
}
}else if(isset($_POST["delete"])){
foreach($_POST as $key => $value){
if(preg_match("/^name_/", $key)){
setcookie($term[0]->slug.'-'.$key, "", time()-3600, '/');
}
}
}
}
}
function sncne_shrtcd_form_start() {
return "<form id='namechange' method='post' action='./'>";
}
add_shortcode('form_start', 'sncne_shrtcd_form_start');
function sncne_shrtcd_form_end() {
return "</form>";
}
add_shortcode('form_end', 'sncne_shrtcd_form_end');
function sncne_shrtcd_input($args) {
$term = get_the_terms( $POST->ID, 'novel_category');
$name = "";
if(isset($_COOKIE[$term[0]->slug.'-'.$args[0]])){
$name = $_COOKIE[$term[0]->slug.'-'.$args[0]];
}
return "<input id=".$args[0]." maxlength='20' name=".$args[0]." type='text' value='".$name."' />";
}
add_shortcode('input_name', 'sncne_shrtcd_input');
function sncne_shrtcd_submit($args) {
return "<input type='submit' name='submit' value=".$args[0].">";
}
add_shortcode('submit_name', 'sncne_shrtcd_submit');
function sncne_shrtcd_delete($args) {
return "<input type='submit' name='delete' value=".$args[0].">";
}
add_shortcode('delete_name', 'sncne_shrtcd_delete');
?>
上記を記載したファイルをpluginディレクトリにアップロードして、プラグイン画面に存在させれば、あとはプラグインエディターで編集できます。ラクチン。
実は私は結局wordpressやめて静的サイトに戻してしまったのです。理由としては、wordpressのアプデとカスタマイズがめんどくさいから!
と思ってたら、DreamMakerさんのサイトが2024/6/10時点で404 NOT FOUNDになってしまったので、頭抱えてます。。。
また新しい変換するタイプの静的サイト用の変換ページなりサイトなり作ると思うので、乞うご期待。
質問や動かんやんけなどはここへのコメントか hym.a3.ryo★gmail.comまで。