• tenzan188

    (@tenzan188)


    投稿ページ、固定ページ、それぞれで、サイトでは、見せたくない情報がありまして、編集画面を開いたときだけ、備忘録的に使えるWidgetがないものかと思っていました。

    functionに加入するコードを考えたのですが、どなたかプルーフしていただけるとありがたいのですが、お知恵をお貸しください。

    
    /*Custom Memo Widget*/
    add_action('admin_menu', 'custom_memo_hooks');
    add_action('save_post', 'save_custom_memo');
    function custom_memo_hooks() {
    	add_meta_box('custom_memo', 'Custom Memo', 'custom_memo_input', 'post', 'normal', 'high');
    	add_meta_box('custom_memo', 'Custom Memo', 'custom_memo_input', 'page', 'normal', 'high');
    }
    function custom_memo_input() {
    	global $post;
    	echo '<input type="hidden" name="custom_memo_noncename" id="custom_memo_noncename" value="'.wp_create_nonce('custom-memo').'" />';
    	echo '<textarea name="custom_memo" id="custom_memo" rows="30" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_memo',true).'</textarea>';
    }
    function save_custom_memo($post_id) {
    	if (!wp_verify_nonce($_POST['custom_memo_noncename'], 'custom-memo')) return $post_id;
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    	$custom_memo = $_POST['custom_memo'];
    	update_post_meta($post_id, '_custom_memo', $custom_memo);
    }
    
    

    当方のテスト環境では、うまくいきましたが、間違いなどご指摘していただけると、ありがたいです。

    • このトピックはtenzan188が8年前に変更しました。
3件の返信を表示中 - 1 - 3件目 (全3件中)
  • Colorful-life.

    (@colorfullifeinfo)

    こんにちは

    「間違い」まででは無いのですが、ぱっと見で感じた事をご参考まで。

    ・最低でもデータの出力時にはエスケープした方が良いかも(必要なら入力時も)

    function custom_memo_input() {
    	global $post;
    	echo '<input type="hidden" name="custom_memo_noncename" id="custom_memo_noncename" value="'.wp_create_nonce('custom-memo').'" />';
    	echo '<textarea name="custom_memo" id="custom_memo" rows="30" cols="30" style="width:100%;">'. esc_textarea( get_post_meta($post->ID,'_custom_memo',true) ) .'</textarea>';
    	// wp_kses_data() などの使用も検討
    	// https://wpdocs.osdn.jp/%E3%83%87%E3%83%BC%E3%82%BF%E6%A4%9C%E8%A8%BC
    }

    $_POST['custom_memo_noncename']$_POST['custom_memo']の存在確認をした方が良いかも
    ・ユーザーが編集権限を持っているか確認した方が良いかも

    function save_custom_memo($post_id) {
    	if ( false === isset($_POST['custom_memo_noncename']) || false === isset($_POST['custom_memo']) )
    		return;
    	if (!wp_verify_nonce($_POST['custom_memo_noncename'], 'custom-memo')) return $post_id;
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    	if ( false === current_user_can( 'edit_post', $post_id ) )
    		return;
    	$custom_memo = $_POST['custom_memo'];
    	update_post_meta($post_id, '_custom_memo', $custom_memo);
    }
    gblsm

    (@gblsm)

    このプラグインが似た処理内容ですので参考になるかもしれません。
    https://ja.wordpress.org/plugins/notely/

    トピック投稿者 tenzan188

    (@tenzan188)

    ありがとうございます。
    勉強になります。
    いまのところ公開できるシロモノではないので、自己使用用です。
    ページごとに異なるパスワードを設定する時や、物件所在地、家主情報などを書き込むのに便利なだけでいいと思っています。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「自作カスタム・メモで、質問させてください。」には新たに返信することはできません。