Parse error: syntax error, unexpected '{': What could be wrong?
Parse error: syntax error, unexpected '{': What could be wrong?
我目前正在为我的 wordpress 网站制作注册表单。我收到这个错误
Parse error: syntax error, unexpected '{' in
C:\xampp\htdocs\WoodClef\wp-content\themes\woodclef\inc\custormregform.php
on line 14
代码如下:
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
这里是第 14 行
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
如果我应该删除 if 语句,它可以工作,但我不想删除它。
完整代码如下:
<?php
// How To create User Registration Form
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
global $wp_roles;
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
}
//2. Add validation.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( '<strong>ERROR</strong>: You must include a role.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
我相信你错过了一个')'...这样看起来更好吗?
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) ) {
其中一个 ')' 用于关闭 if 和一个用于关闭 "in_array" 函数。
我目前正在为我的 wordpress 网站制作注册表单。我收到这个错误
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\WoodClef\wp-content\themes\woodclef\inc\custormregform.php on line 14
代码如下:
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
这里是第 14 行
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
如果我应该删除 if 语句,它可以工作,但我不想删除它。
完整代码如下:
<?php
// How To create User Registration Form
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
global $wp_roles;
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
}
//2. Add validation.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( '<strong>ERROR</strong>: You must include a role.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
我相信你错过了一个')'...这样看起来更好吗?
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) ) {
其中一个 ')' 用于关闭 if 和一个用于关闭 "in_array" 函数。