【问题标题】:WP Custom Registration FormWP海关登记表
【发布时间】:2017-09-08 10:58:09
【问题描述】:
如何在不使用任何插件的情况下从头开始创建或修改 WP 注册表单?
在网上找不到任何追索权。全部带有插件。
提前致谢
【问题讨论】:
标签:
wordpress
customization
custom-wordpress-pages
【解决方案1】:
我通常从头开始创建一个插件,以便能够制作自定义联系表单 - 请参阅:https://codex.wordpress.org/Writing_a_Plugin
您的插件需要显示一个表单,即:
<form id="myform" method="post" action="contact_form.php">
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<textarea rows="10" name="message" />
<input type="submit" value="Submit" />
</form>
然后在你的
contact_form.php
做类似的事情:
$first = $_POST['first_name'];
$last = $_POST['last_name'];
$message = $_POST['message'];
$email = "Name: " . $first . " Last name: " . $last . " Message: " . $message;
$to = "youremail@yourdomain.com";
$subject = "New message from " . $first . " " . $last;
$body = $email;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
希望对你有所帮助!
学习如何构建 WP 插件将对您未来有很大帮助,因此值得深入研究 WP 文档以更广泛地了解该主题。祝你好运! :-)
【解决方案2】:
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
?>
<p>
<label for="first_name"><?php _e( 'First Name', 'mydomain' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
</p>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
}