不存储数据的自定义POST的WordPress自定义元字段

时间:2018-05-08 作者:James Paul

我已经创建了名为freebie的自定义帖子类型,因为我在其中创建了一个自定义元节,其中添加了一个输入字段。它不存储在该字段中输入的数据,也不显示在该字段中输入的值。我已经附上了编码。

function adding_freebie_metabox($post) {

    add_meta_box(
          \'my-meta-box\'
        , __(\'Freebie extra deatails\', \'lwprjs\')
        , \'render_my_freebie_metabox\'
        , \'freebie\'
        , \'normal\'
        , \'default\'
    );

}

add_action(\'add_meta_boxes_freebie\', \'adding_freebie_metabox\');

// Add field
function render_my_freebie_metabox($meta_id) {

    // make sure the form request comes from WordPress
    wp_nonce_field(basename(__FILE__), \'freebie_meta_box_nonce\');

    ?>

        Enter freebie details such as URL of download and also demo URL
        <table class="form-table">
            <tbody>
                <tr>
                    <th><label for="freebie-demo">Demo URL</label></th>
                    <td><input style="width: 100%" id="freebie-demo" name="freebie-demo"
                        type="text"
                        value="<?php get_post_meta( $post->ID, $meta_field[\'freebie-demo\'], true ); ?>"></td>
                </tr>
            </tbody>
        </table>

    <?php
}

function food_save_meta_box_data($post_id) {

    // verify meta box nonce
    if (!isset($_POST[\'freebie_meta_box_nonce\']) || !wp_verify_nonce($_POST[\'freebie_meta_box_nonce\'], basename(__FILE__))) {
        return;
    }

    // return if autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
        return;
    }

    // Check the user\'s permissions.
    if (!current_user_can(\'edit_post\', $post_id)) {
        return;
    }

    // store custom fields values
    // cholesterol string
    if (isset($_REQUEST[\'freebie-demo\'])) {
        update_post_meta($post_id, \'_freebie_demo\', sanitize_text_field($_POST[\'freebie-demo\']));
    }
}

add_action(\'save_post_freebie\', \'food_save_meta_box_data\');

2 个回复
最合适的回答,由SO网友:Suresh Sapkota 整理而成

根据您的代码,希望您已经注册了CPT(自定义帖子类型),并提供了有效的标签和;具有函数的参数:

register_post_type( \'freebie\', $args );
以下是完整的代码,您的自定义元框将仅对我们的“免费”CPT可见,并带有一个输入字段,用于保存此CPT帖子的演示url。在函数中添加以下代码行。php

// Adding meta box for freebie custom post type
function demo_url_meta_box() {

    add_meta_box(
        \'demo_url_meta_box\',
        __( \'Demo URL\', \'\' ),
        \'demo_url_meta_box_callback\',
        \'freebie\',
        \'side\',
        \'low\'

    );
}

add_action( \'add_meta_boxes_freebie\', \'demo_url_meta_box\' );
// Callback function of metabox
function demo_url_meta_box_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( \'demo_url_nonce\', \'demo_url_nonce\' );

    // postmeta key: _demo_url
    $demo_url = get_post_meta( $post->ID, \'_demo_url\', true );


    echo \'<textarea style="width:100%" id="freebie-demo" name="freebie-demo">\' . esc_attr( $demo_url ) . \'</textarea>\';
}
/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id
 */
function save_demo_url_meta_box_data( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'demo_url_nonce\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'demo_url_nonce\'], \'demo_url_nonce\' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {

        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }

    }
    else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    /* OK, it\'s safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST[\'freebie-demo\'] ) ) {
        return;
    }

    // Sanitize user input.
    $de_url = sanitize_text_field( $_POST[\'freebie-demo\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'_demo_url\', $de_url );
}
// add meta box data when save_post is hooked
add_action( \'save_post\', \'save_demo_url_meta_box_data\' );
希望这对你有帮助!!!

SO网友:Willem Munts

也许可以尝试添加一个不同的模板来显示它。

add_filter( \'template_include\', \'include_template_function\', 1 );
在php之上

function include_template_function( $template_path ) {
    if ( get_post_type() == \'freebies-info\' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( \'freebies-info.php\' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . \'/freebies_info.php\';
            }
        }
    }
    return $template_path;
}
本部分if ( get_post_type() == \'freebies-info\' ) { 应该是你的register_post_type 而不是freebies-info此代码用于添加php文件,例如freebies-info.php.然后在同一文件夹中创建一个名为that的php文件。这可能如下所示:

<?php
 /*Template Name: New Template
 */

get_header(); ?>
<div id="primary">
    <div id="content" role="main">
    <?php
    $mypost = array( \'post_type\' => \'freebies-info\', );
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( have_posts() ) : the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                <strong>Title: </strong><?php the_title(); ?><br />
            </header>
                <div class="">
                <?php the_content(); ?>
                </div>
        </article>
    <?php endwhile; ?>
    </div>
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
记住,我为你做了这个免费信息。

$mypost = array( \'post_type\' => \'freebies-info\', );
在这里,你应该把你称之为register_post_type 您可以找到创建costum post类型的位置。

希望这对你有帮助,或者你有更多的想法来解决它,祝你好运!

结束