从前端表单提交带有自定义域的自定义发布类型

时间:2017-07-06 作者:Khalil Themes

我正在尝试使用2个自定义字段创建一个自定义类型的帖子。现在,我通过模板页面在前端有了工作自定义帖子类型和工作表单。问题是I can not get the custom field working, 这意味着自定义字段不能通过前端表单保存,也不能以单个形式显示该自定义字段的值。php页面。现在我可以从前端提交文章,只包含标题和内容。

这是我在中的自定义帖子类型和表单functions.php

function ty_post_type_init() {
  $labels = array(
    \'name\' => _x(\'Betting Tips\', \'the custom posts\', \'your_text_domain\'),
    \'singular_name\' => _x(\'Betting Tips\', \'the custom post\', \'your_text_domain\'),
    \'add_new\' => _x(\'Add New\', \'custom_posts\', \'your_text_domain\'),
    \'add_new_item\' => __(\'Add New Betting Tips\', \'your_text_domain\'),
    \'edit_item\' => __(\'Edit Betting Tips\', \'your_text_domain\'),
    \'new_item\' => __(\'New Betting Tips\', \'your_text_domain\'),
    \'all_items\' => __(\'All Betting Tips\', \'your_text_domain\'),
    \'view_item\' => __(\'View Betting Tips\', \'your_text_domain\'),
    \'search_items\' => __(\'Search Betting Tips\', \'your_text_domain\'),
    \'not_found\' =>  __(\'No betting tips found\', \'your_text_domain\'),
    \'not_found_in_trash\' => __(\'No betting tips found in Trash\', \'your_text_domain\'), 
    \'parent_item_colon\' => \'\',
    \'menu_name\' => __(\'Betting Tips\', \'your_text_domain\')
  );

  $args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true, 
    \'show_in_menu\' => true, 
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => _x( \'custom_posts\', \'URL slug\', \'your_text_domain\' ) ),
    \'capability_type\' => \'post\',
    \'has_archive\' => true, 
    \'hierarchical\' => false,
    \'menu_position\' => null,
    \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
  );

  register_post_type(\'custom_posts\', $args);
  flush_rewrite_rules();
}
add_action( \'init\', \'ty_post_type_init\' );

function ty_front_end_form() { ?>
    <form id="custom-post-type" name="custom-post-type" method="post" action="">
        <p>
            <label for="title">Post Title</label><br />
            <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
        </p>

        <p>
            <label for="custom_field_one">Custom Field</label>
            <input type="text" value="" id="custom_field_one" size="60" tabindex="20" name="custom_field_one">
        <p>

        <p>
            <label for="description">Post Description</label><br />
            <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
        </p>

        <p>
            <?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=category\' ); ?>
        </p>

        <p>
            <label for="post_tags">Post Tags</label>
            <input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" />
        </p>

        <p align="right">
            <input type="submit" value="Publish" tabindex="6" id="submit" name="submit" />
        </p>

        <input type="hidden" name="post-type" id="post-type" value="custom_posts" />

        <input type="hidden" name="action" value="custom_posts" />

        <?php wp_nonce_field( \'name_of_my_action\',\'name_of_nonce_field\' ); ?>

    </form>
    <?php

    if( $_POST ){
        ty_save_post_data();
    }
}
add_shortcode(\'custom_posts\',\'ty_front_end_form\');

function ty_save_post_data() {

    if ( empty($_POST) || !wp_verify_nonce($_POST[\'name_of_nonce_field\'],\'name_of_my_action\') ) {
       print \'Sorry, your nonce did not verify.\';
       exit;
    } else { 

        if (isset ($_POST[\'title\'])) {
            $title =  $_POST[\'title\'];
        } else {
            echo \'Please enter a title\';
            exit;
        }

        if (isset ($_POST[\'description\'])) {
            $description = $_POST[\'description\'];
        } else {
            echo \'Please enter the content\';
            exit;
        }

        if (isset($_POST[\'post_tags\'])){
            $tags = $_POST[\'post_tags\'];
        } else {
            $tags = "";
        }

       $custom_field_one = $_POST[\'custom_field_one\'];

        $post = array(
            \'post_title\' => wp_strip_all_tags( $title ),
            \'post_content\' => $description,
            \'post_category\' => $_POST[\'cat\'], 
            \'custom_field_one\' => $custom_field_one, // 
            \'tags_input\' => $tags,
            \'post_status\' => \'publish\', 
            \'post_type\' => custom_posts  
        );
        wp_insert_post($post);

        $location = home_url(); 

        echo "<meta http-equiv=\'refresh\' content=\'0;url=$location\' />";
        exit;
    } // end IF

}
然后我创建了一个页面模板custom-form.php

<?php
    /**
     * Template Name: My Custom Form
     */
    get_header();

    ty_front_end_form();

    get_footer();
?>
这是我的single.php 页码:

<h1 class="single-post-title"><?php esc_html(the_title()); ?></h1>
<div class="my-single-content">
    <?php esc_html(the_content()); ?>
</div>
<div class="custom-field-one">
    <?php echo get_post_meta($post->ID, "custom_field_one", true); ?>
</div>
请帮助我获取单个自定义字段的值。php通过前端表单。

非常感谢!

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

我不明白如果你不使用它,为什么要注册一个短代码。你的custom-form.php\'s代码可以是:

<?php
    /**
     * Template Name: My Custom Form
     */
    get_header();

    echo do_shortcode(\'[custom_posts]\');

    get_footer();
?>
现在,回到您的问题,如何保存和/或获取自定义字段值。嗯,您保存自定义字段(meta)值的方法完全不正确。我想知道当您搜索相关查询时,您在SERPs中得到了什么,或者您甚至搜索了吗?

保存post_meta 值,首先获取新创建帖子的ID并保存自定义字段值。为了缩短答案,我将只包含相关代码

$post = array(
    \'post_title\' => wp_strip_all_tags( $title ),
    \'post_content\' => $description,
    \'post_category\' => $_POST[\'cat\'],
    \'tags_input\' => $tags,
    \'post_status\' => \'publish\', 
    \'post_type\' => \'custom_posts\'
);
$post_id = wp_insert_post($post);

add_post_meta( $post_id, \'custom_field_one\', $custom_field_one, false );
您也可以这样做

$post = array(
    \'post_title\' => wp_strip_all_tags( $title ),
    \'post_content\' => $description,
    \'post_category\' => $_POST[\'cat\'],
    \'tags_input\' => $tags,
    \'post_status\' => \'publish\', 
    \'post_type\' => \'custom_posts\',
    \'meta_input\' => array(
        \'custom_field_one\' => $custom_field_one
    )
);
$post_id = wp_insert_post($post);
Disclaimer: 我不确定代码的其余部分,但只对所询问的内容提供了帮助。

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。