0
我目前正在爲我的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']));
}
[PHP Parse/Syntax Errors;和如何解決它們?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) –