在表单提交中,我为我的插件定制了用户评论。我正在检查注释字段是否为空,并且注释字段至少填充了30个字符。如果两者都可以,我想插入注释。
这是我的代码:
<?php
global $current_user, $post;
if( isset( $_POST[\'send\'] ) && isset( $_POST[\'test_nonce\'] ) && wp_verify_nonce( $_POST[\'test_nonce\'], \'testify_nonce\' ) ) {
//global $error;
$error = new WP_Error();
$comment_content = $_POST[\'comment_content\'];
if( empty( $comment_content ) ) {
$error->add( \'comment_empty\', __("Comment field can\'t be empty.") );
}
if ( strlen( $comment_content ) < 30 ) {
$error->add( \'comment_short\', __("Your comment is too short. Write down at least 30 characters.") );
}
//test test test
var_dump($error);
if( is_wp_error( $error ) ) { echo 1; }
//test test test
if( is_wp_error( $error ) ) {
echo \'<div class="alert alert-danger" role="alert">\';
echo $error->get_error_message();
echo \'</div>\';
} else {
$commentdata = array(
\'comment_post_ID\' => $post->ID,
\'comment_author\' => $current_user->display_name,
\'comment_author_email\' => $current_user->user_email,
\'comment_author_url\' => $current_user->user_url,
\'comment_content\' => htmlentities( $comment_content ),
\'comment_type\' => \'\',
\'comment_parent\' => 0,
\'user_id\' => $current_user->ID,
\'comment_approved\' => \'1\' //approve by default
);
//Insert new comment and get the comment ID
$comment_id = wp_insert_comment( $commentdata );
if( ! is_wp_error( $comment_id ) ) {
echo \'<div class="alert alert-success" role="alert">\';
_e( \'Your comment is successfully submitted.\', \'text-domain\' );
echo \'</div>\';
} else {
echo \'<div class="alert alert-danger" role="alert">\';
echo $comment_id->get_error_message();
echo \'</div>\';
} //endif( ! is_wp_error( $comment_id ) )
} //endif( is_wp_error( $error ) )
} //endif( $_POST[\'send\'] )
?>
<form method="post" enctype="multipart/form-data">
<textarea name="comment_content" id="" class="form-control" rows="6"></textarea>
<?php wp_nonce_field( \'testify_nonce\', \'test_nonce\' ); ?>
<button type="submit" name="send" class="btn btn-primary"><?php _e( \'Submit\', \'text-domain\' ); ?></button>
</form>
但在提交表格时,如果我
var_dump( $error );
:
object(WP_Error)[398]
public \'errors\' =>
array (size=0)
empty
public \'error_data\' =>
array (size=0)
empty
它是空的,但仍然是
if( is_wp_error( $error ) ) { echo 1; }
正在显示
1
. 这就是为什么评论没有插入。
我做错了什么?
最合适的回答,由SO网友:cybmeta 整理而成
功能is_wp_error
检查给定的var是否是WP_Error
类别(source code as WP 4.2.2):
function is_wp_error( $thing ) {
return ( $thing instanceof WP_Error );
}
如您所见,如果给定变量是WP\\u Error类的实例,则即使对象为空,函数也会返回true。你的
$error
变量是
WP_Error
实例,空但为a
WP_Error
实例,因此它返回true。你可以做点什么:
if ( is_wp_error( $error ) && ! empty( $error->errors ) )
或发起
WP_Error
仅在特定条件下(此处填写,未测试):
$errors = array();
if ( empty( $comment_content ) ) {
$errors[\'comment_empty\'] = esc_html__("Comment field can\'t be empty.");
}
if ( strlen( $comment_content ) < 30 ) {
$errors[\'comment_short\'] = esc_html__("Your comment is too short. Write down at least 30 characters.");
}
if( ! empty( $errors ) ) {
$wp_error = new WP_Error();
foreach ( $errors as $code => $message ) {
$wp_error->add( $code, $message );
}
}
if ( is_wp_error( $wp_error ) ) {
}