前端定制贴子类型的简单表单验证。不起作用

时间:2014-08-07 作者:user57391

所以,除了验证之外,一切都很顺利。当表单为空提交时,不会显示错误消息,您仍然可以在表单为空时提交表单。我一定错过了一些非常明显的东西。我已经花了几个小时在网上搜索,自己也尝试了几次修复,但都没有成功。任何帮助都将不胜感激。

<?php add_shortcode(\'testimonial-form\', \'testimonial_form_sc\') ?>

<?php function testimonial_form_sc(){ ?>

<div class="row">
<h3>Send Us Your Testimonial</h3>
<form id="add_testimonial" name="add_testimonial" method="post" action="" enctype="multipart/form-data">

<!-- post name -->

    <label for="username" style="display:block">Name:</label>
    <input type="text" value="" name="username" style="display-block" />

<!-- post content -->
    <label for="description" style="display:block">Testimonial:</label>
    <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>

    <input type="file" name="thumbnail" id="thumbnail">

    <input class="small-button" type="submit" value="Send" id="submit" name="submit" style="display:block;"/>

<input type="hidden" name="action" value="add_testimonial" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>

</div> 

<?php

if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&     $_POST[\'action\'] == "add_testimonial") {

// Do some minor form validation to make sure there is content
if (isset ($_POST[\'username\'])) {
    $title =  $_POST[\'username\'];
} 
else {
    echo \'Please enter your name\';
}
if (isset ($_POST[\'description\'])) {
    $description = $_POST[\'description\'];
} 
else {
    echo \'Please enter your testimonial\';
}

// add the form input to $new_testimonial array
$new_testimonial = array(
\'post_title\'    =>   $title,
\'post_content\'  =>   $description,
\'post_status\'   =>   \'publish\',           // Switch this to draft if you want to moderate posts
\'post_type\' =>   \'testimonial\'  //post type to insert the post into
);

//SAVE THE POST
$pid = wp_insert_post($new_testimonial);

// Upload image for post thumbnail

 if (!function_exists(\'wp_generate_attachment_metadata\')){
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file][\'error\'];
                }
                $attach_id = media_handle_upload( $file, $pid );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($pid,\'_thumbnail_id\',$attach_id);
        }

include \'validate-image-size.php\';

//Redirect to the new post on save
$link = get_permalink( $pid );
wp_redirect( $link );

}} // End of the if statement that started the form and the end of the shortcode     [testimonial-form]

//post the post
do_action(\'wp_insert_post\', \'wp_insert_post\');`

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

验证无效,因为带有空数组的变量在isset中返回true;

转到http://writecodeonline.com/php/ 然后尝试此代码,您将了解:

$test = "";
if ( isset($test) ){
    echo "true";
}
它的正确函数(而不是isset)为空。尝试一下:

$test = "";
if ( empty($test) ){
    echo "empty";
}
此外,如果您正在wordpress中进行大量建模和验证,我建议使用wonderfull PHPActiveRecord。我学会了如何使用它here, 我爱上了它。:)

结束

相关推荐

在使用GravityForms上传工具时,如何通过函数更改图片的作者?

我想知道是否有办法手动更改图像的作者。我使用的Gravity表单将帖子作者设置为当前用户,但不会将他们设置为上传图像的作者。我想可能有一种方法可以添加一个函数,该函数可以查找当前用户,然后设置他们上传给该用户的图像的作者。