
使用プラグイン Advanced Custom Fields
個別記事投稿ページに設置してあるラジオボタンをクイック編集出来るようにプログラムを組んだのですが、クイック編集で選択した物が管理画面の個別記事投稿ページに反映されていませんでした、
色々と参考サイトを見ましたが、参考になるサイトが無くて色々とカスタマイズしましたが、知識が浅く限界がありました。
わかる方居ましたら教えてください。
参考サイト https://increment-log.com/quickedit-custom-field/
カスタムフィールド名
publish_type
ラジオボタン選択肢
free : 無
premium : 有
/** * 投稿一覧に項目を追加する */ function my_posts_columns($defaults) { $defaults['publish_type'] = '状況'; return $defaults; } add_filter('manage_posts_columns', 'my_posts_columns'); /** * 投稿一覧に追加した項目に値を表示する */ function my_posts_custom_column($column, $post_id) { switch ($column) { case 'publish_type': $post_meta = get_post_meta($post_id, 'publish_type', true); if ($post_meta == 'free') { echo '無'; } elseif ($post_meta == 'premium') { echo '有'; } else { echo ''; } break; case 'display': } } add_action('manage_posts_custom_column', 'my_posts_custom_column', 10, 2); //クイック編集にカスタムフィールド(掲載状況)の入力フォームを表示 function display_my_custom_quickedit($column_name, $post_type) { static $print_nonce = TRUE; if ($print_nonce) { $print_nonce = FALSE; wp_nonce_field('quick_edit_action', $post_type . '_edit_nonce'); //CSRF対策 } ?> <fieldset class="inline-edit-col-right inline-custom-meta"> <div class="inline-edit-col column-<?php echo $column_name ?>"> <label class="inline-edit-group"> <?php switch ($column_name) { case 'publish_type': ?> <input type="radio" name="fields[field_570f985309a6c]" value="free" checked>無 <input type="radio" name="fields[field_570f985309a6c]" value="premium">有 <?php break; } ?> </label> </div> </fieldset> <?php } add_action('quick_edit_custom_box', 'display_my_custom_quickedit', 10, 2); //カスタムフィールドの保存処理 function save_custom_meta($post_id) { $slug = 'post'; //カスタムフィールドの保存処理をしたい投稿タイプを指定 if ($slug !== get_post_type($post_id)) { return; } if (!current_user_can('edit_post', $post_id)) { return; } $_POST += array("{$slug}_edit_nonce" => ''); if (!wp_verify_nonce($_POST["{$slug}_edit_nonce"], 'quick_edit_action')) { return; } if (isset($_REQUEST['free'])) { update_post_meta($post_id, 'publish_type', 'free'); } elseif (isset($_REQUEST['premium'])) { update_post_meta($post_id, 'publish_type', 'premium'); } } add_action('save_post', 'save_custom_meta'); //管理ページ(投稿一覧)でスクリプトの読み込み function my_admin_edit_foot() { global $post_type; $slug = 'post'; //他の一覧ページで動作しないように投稿タイプの指定をする if ($post_type == $slug) { echo '<script type="text/javascript" src="', get_stylesheet_directory_uri() . '/admin/js/main.js', '"></script>'; } } add_action('admin_footer-edit.php', 'my_admin_edit_foot');
main.js
//カスタムフィールドのクイック編集動作 (function($) { var $wp_inline_edit = inlineEditPost.edit; inlineEditPost.edit = function( id ) { $wp_inline_edit.apply( this, arguments ); var $post_id = 0; if ( typeof( id ) == 'object' ) $post_id = parseInt( this.getId( id ) ); if ( $post_id > 0 ) { var $edit_row = $( '#edit-' + $post_id ); var $post_row = $( '#post-' + $post_id ); var $publish_type = !! $('.column-publish_type>*', $post_row).attr('checked'); $( ':input[fields[field_570f985309a6c] checked', $edit_row ).attr('checked', $publish_type ); } }; })(jQuery);





回答1件
あなたの回答
tips
プレビュー