Astroやれや
結局気になってWordpressの方いじっちゃった・・・・
そもそもAstroやるきっかけが、お客様のサイト高速化とリニューアルのご依頼で、できるだけ環境似せようとおもったらカスタムフィールドが必要やってん。
ただACFのプラグイン入ってるサイトやったから素直にACF入れりゃよかったのに、なんならAPI読んでるだけやからWP側の重さとか考えなくていいのに自作でカスタムフィールド作った。
ただ私の個人ブログではそこまで細分化しなくてもカテゴリとタグで賄えてしまって。。。
今日の気分とかでいっか。と「うきうき」「わくわく」「かなしみ」の3つのチェックボックスと、metaのdescriptionとkeyword用のテキストフィールドを設置完了。APIでも取れるようにした。
(ChatGPT様のおかげ)
function add_custom_meta_box() {
add_meta_box('custom_meta_box', 'オプション', 'custom_meta_box_callback', 'post');
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function custom_meta_box_callback($post) {
// ノンスフィールドを使ってフォームをセキュアにする
wp_nonce_field('custom_meta_box_nonce', 'meta_box_nonce');
// チェックボックスの状態を取得
$checked_wakuwaku = get_post_meta($post->ID, 'meta_box_wakuwaku', true) === 'yes' ? 'checked' : '';
$checked_ukiuki = get_post_meta($post->ID, 'meta_box_ukiuki', true) === 'yes' ? 'checked' : '';
$checked_kanashimi = get_post_meta($post->ID, 'meta_box_kanashimi', true) === 'yes' ? 'checked' : '';
// チェックボックスを表示
echo '<p><label><input type="checkbox" name="meta_box_wakuwaku" ' . $checked_wakuwaku . '> わくわく</label></p>';
echo '<p><label><input type="checkbox" name="meta_box_ukiuki" ' . $checked_ukiuki . '> うきうき</label></p>';
echo '<p><label><input type="checkbox" name="meta_box_kanashimi" ' . $checked_kanashimi . '> かなしみ</label></p>';
// meta_descriptionの値を取得してテキストエリアとして表示
$meta_description = get_post_meta($post->ID, 'meta_description', true);
echo '<p><label for="meta_description">メタディスクリプション:</label>';
echo '<textarea id="meta_description" name="meta_description" rows="4" cols="50">' . esc_textarea($meta_description) . '</textarea></p>';
// meta_keywordの値を取得してテキストエリアとして表示
$meta_keyword = get_post_meta($post->ID, 'meta_keyword', true);
echo '<p><label for="meta_keyword">メタキーワード:</label>';
echo '<textarea id="meta_keyword" name="meta_keyword" rows="2" cols="50">' . esc_textarea($meta_keyword) . '</textarea></p>';
}
function save_custom_meta_box_data($post_id) {
// ノンスチェック
if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'custom_meta_box_nonce')) {
return;
}
// 自動保存チェック
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// 権限チェック
if (!current_user_can('edit_post', $post_id)) {
return;
}
// チェックボックスの値を更新
$wakuwaku = isset($_POST['meta_box_wakuwaku']) ? 'yes' : 'no';
$ukiuki = isset($_POST['meta_box_ukiuki']) ? 'yes' : 'no';
$kanashimi = isset($_POST['meta_box_kanashimi']) ? 'yes' : 'no';
update_post_meta($post_id, 'meta_box_wakuwaku', $wakuwaku);
update_post_meta($post_id, 'meta_box_ukiuki', $ukiuki);
update_post_meta($post_id, 'meta_box_kanashimi', $kanashimi);
// meta_descriptionとmeta_keywordの値をサニタイズして保存
if (isset($_POST['meta_description'])) {
update_post_meta($post_id, 'meta_description', sanitize_textarea_field($_POST['meta_description']));
}
if (isset($_POST['meta_keyword'])) {
update_post_meta($post_id, 'meta_keyword', sanitize_textarea_field($_POST['meta_keyword']));
}
}
add_action('save_post', 'save_custom_meta_box_data');
function get_custom_fields_for_rest($object, $field_name, $request) {
// メタフィールドの値を取得
$wakuwaku = get_post_meta($object['id'], 'meta_box_wakuwaku', true);
$ukiuki = get_post_meta($object['id'], 'meta_box_ukiuki', true);
$kanashimi = get_post_meta($object['id'], 'meta_box_kanashimi', true);
// チェックボックスの値に基づいて文字列を設定
$wakuwaku_value = ($wakuwaku === 'yes') ? 'わくわく' : '未選択';
$ukiuki_value = ($ukiuki === 'yes') ? 'うきうき' : '未選択';
$kanashimi_value = ($kanashimi === 'yes') ? 'かなしみ' : '未選択';
return array(
'wakuwaku' => $wakuwaku_value,
'ukiuki' => $ukiuki_value,
'kanashimi' => $kanashimi_value,
'meta_description' => get_post_meta($object['id'], 'meta_description', true),
'meta_keyword' => get_post_meta($object['id'], 'meta_keyword', true),
);
}
上記でカスタムフィールドがでてきたし、
本当いうと管理画面側のcssも時間ができたらつくりたいな
はよAstroやれや