web-dev-qa-db-ja.com

カスタムメタボックスの入力が保存されないのはなぜですか。

エンドユーザーがギャラリーのショートコードを入力できるようにするカスタムメタボックスを追加するために、functions.phpに次の関数を追加しました。それから、これを使ってギャラリーをページの特定の場所に(メインのthe_content出力から離れたところに)配置するつもりです。私が使っている機能は スマッシュマガジン から来ています、そして、あなたが推測したかもしれないように、私はそれが標準的なワードプレス機能からphpに来るとき私が何をしているかほとんどわかりません。 (私は近い将来PHPを完全に学ぶつもりです)。

私のメタボックスは問題なく表示されていますが、入力したデータが保存されていません。すなわち投稿の[更新]をクリックすると、メタボックスがプレースホルダのテキストに戻ります。

// Load the post meta box setup function on the post editor screen.
add_action ( 'load-post.php', 'rs_post_meta_boxes_setup' );
add_action ( 'load-post-new.php', 'rs_post_meta_boxes_setup' );

//Meta box setup function
function rs_post_meta_boxes_setup() {

    //Add meta boxes to the 'add_meta_boxes' wordpress hook.
    add_action ( 'add_meta_boxes', 'rs_add_post_meta_boxes' );

    // Save post meta once created on the 'save_post' hook.
    add_action( 'save_post', 'rs_save_job_gallery_meta', 10, 2 );
}

// Create meta boxes for display on the post editor screen
function rs_add_post_meta_boxes() {

    add_meta_box(
        'rs-job-gallery', //Meta box unique ID
        esc_html__( 'Job Gallery', 'rs-theme' ), //Title
        'rs_job_gallery_meta_box', //Callback Function
        'job', //Admin page on which to display meta box
        'normal',
        'default'
        );
}

// Display the post meta box
function rs_job_gallery_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'rs_job_gallery_nonce' ); ?>

    <p>
        <label for="rs-job-gallery"> <?php _e( "Add the job gallery to the page. Use the following format: [galleryview id=x] where x is the ID of the job gallery.", 'redmansutherland' ); ?></label>
        <br/>
        <input class="widefat" type="text" placeholder="[galleryview id=X]" name="rs-job-gallery" id="rs-job-gallery" value="<?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?>" size="30"/>   
    </p>

    <?php }

// Save the meta box's post metadata.
function rs_save_job_gallery_meta( $post_id, $post ) {

    // Verify the nonce before proceeding.
    if ( !isset( $_POST['rs_job_gallery_nonce'] ) || !wp_verify_nonce( $_POST['rs_job_gallery_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    // Get the post type object.
    $post_type = get_post_type_object( $post->post_type );

    // Check if the current user has permission to edit the post.
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    // Get the posted data and sanitize it for use as an HTML class.
    $new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? sanitize_html_class( $_POST['rs_job_gallery'] ) : '' );

    // Get the meta key.
    $meta_key = 'rs_job_gallery';

    // Get the meta value of the custom field key.
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    // If a new meta value was added and there was no previous value, add it.
    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    // If the new meta value does not match the old value, update it.
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );

    // If there is no new meta value but an old value exists, delete it.
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );

}

誰かが提供できる洞察を前もってありがとう、あなたの助けは大いに感謝されています!

2
Raskolnik

変更してみてください。

<input class="widefat" type="text" placeholder="[galleryview id=X]" name="rs-job-gallery" id="rs-job-gallery" value="<?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?>" size="30"/>

これに:

<input class="widefat" type="text" placeholder="[galleryview id=X]" name="rs_job_gallery" id="rs-job-gallery" value="<?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?>" size="30"/>

理由:入力フィールドに "name"属性が設定されていると、それが$_POST['input_name']変数になります。カスタムメタ名にはアンダースコアを使用する必要があるので、入力フィールドの名前も同様にしてください。

また、このコードを変更する必要があります。

$new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? sanitize_html_class( $_POST['rs_job_gallery'] ) : '' );

このようなものに:

$new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? esc_attr( $_POST['rs_job_gallery'] ) : '' );

sanitize_html_classはそれを数字と文字に分割しているので要素のクラスとして使うことができますが、それはあなたが望むものではありません。

2
Jared