acf_form()验证和反馈

时间:2016-09-16 12:24:27

标签: php wordpress advanced-custom-fields

我使用以下代码创建了一个用于提交新帖子的前端表单(我希望大多数人都熟悉它):

$options = array(
    'post_id' => 'new_post',
        'new_post' => array(
        'post_type'      => 'post',
        'post_status'   => 'pending'
    ),
    'html_before_fields' => $html_before_fields,
    'html_after_fields' => $html_after_fields,
    'uploader' => 'basic',
    'submit_value' => __("Create", 'domain'),
    'updated_message' => __("Your post was submitted successfully! Please wait for it to be approved.", 'domain'),
);

acf_form($options);

这是我的functions.php的片段;

function _action_ssd_acf_submit_post_pre_save( $post_id ) {

    if( empty($_POST['acf']) ) {
        return;
    }

    if ( !isset($_POST['acf_post_submit']) ) { // this is a hidden input field in the form
        return;
    }

    if (  ) { // some more checks here
        wp_mail(); //removed parameters for cleaner code, I send email to the admin
    }  

    if( empty(get_user_meta(get_current_user_id(), 'this_user_can_submit', true)) ) {
        exit;
        return;
    } 


}

add_action( 'acf/pre_save_post', '_action_ssd_acf_submit_post_pre_save' );

如何保存帖子,因为它总是保存为草稿?

此外,当提交时出现错误时,我似乎无法找到更改update_message的方法。例如,如果用户没有所需的user_meta

所以,是的,我的问题是我无法停止提交帖子(如果出现验证错误)并在页面刷新后发送前端消息反馈。

1 个答案:

答案 0 :(得分:0)

如果您希望发布帖子,而不是将其设置为草稿,则需要使用'post_status' => 'publish'代替'post_status' => 'post'