为什么我的自定义元框输入不能保存

时间:2012-01-25 作者:Raskolnik

我在函数中添加了以下函数。php添加一个自定义元框,使最终用户能够输入库短代码。然后,我打算使用它在页面上的特定位置放置一个库(远离主内容输出)。我使用的函数来自smashing magazine 而且,正如您可能已经猜到的那样,当涉及到php和标准wordpress函数时,我几乎不知道我在做什么。(我确实打算在不久的将来全面学习PHP)。

我的元框显示良好,但我输入的数据未保存。一、 e.当我在帖子上单击“更新”时,元框会恢复为我的占位符文本。

// 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 );

}
提前感谢任何人提供的任何见解,非常感谢您的帮助!

1 个回复
最合适的回答,由SO网友:Jared 整理而成

尝试更改:

<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 将其拆分为数字和字母,以便可以在元素中用作类,但这不是您想要的。

结束

相关推荐

如何从OUSIDE WordPress/php验证用户?

我正在开发一个将嵌入wordpress页面的ajax应用程序。ajax应用程序与tomcat上运行的servlet交换数据。现在,servlet需要一种方法来确定请求是否来自登录到wordpress的用户。如果用户已登录,servlet还必须能够确定用户id,以便能够查询数据库。如果用户未记录该请求,则该请求将被拒绝。因此,换句话说,我需要让servlet仅在导致请求的用户登录到wordpress(版本3.3.x)时执行请求。servlet(tomcat)和wordpress(apache2)都运行在同一