我已经陷入了(我不知道的)wordpress 自定义表单和自定义表单处理程序的深渊。我只需要一个有效的联系表格,而无需安装任何插件。我通常会使用 $_SESSION 超全局变量(传递错误和成功消息)和重定向后获取模式(避免通过页面重新加载进行双重发送)来执行此操作。代码看起来像这样
<form class="di-contact" method="post" action="<?php echo get_template_directory_uri() ?>/contact.php" style="width: 50%; margin: 0 auto">
<h3 id="titulo-single"><?php the_title() ?></h3>
<label for="di-name"><?php _e('Name', 'di-news-blog'); ?>:</label>
<input class="uk-input" type="text" id="di-name" name="di-name"><br>
<label for="di-email"><?php _e('E-mail', 'di-news-blog'); ?>:</label>
<input class="uk-input" type="email" id="di-email" name="di-email"><br>
<label for="di-message"><?php _e('Message', 'di-news-blog'); ?>:</label>
<textarea class="uk-textarea" id="di-message" name="di-message"></textarea><br>
<input type="submit" name="di-submit" value="<?php _e('Submit', 'di-news-blog'); ?>">
</form>
然后我会在contact.php中编写这样的代码:
<?php
//////////////////// MAIL PROPERTIES ///////////////////
$to = get_option('admin_email');
$from = htmlentities($_POST['di-email']);
$subject = __('Message from ','di-news-blog'). get_bloginfo('blogname');
//////////////////////// HEADERS ////////////////////////
$headers = "From:" . $from . "\r\n";
$headers .= "Reply-To:" . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//////////////////////// MESSAGE ////////////////////////
$message = '<p>You got a message from <strong>' .
htmlentities($_POST['di-name']) . '</strong><p/>';
$message .= '<p><strong>Email:</strong> ' . $from . '</p>';
$message .= '<strong>Message:</strong><br/>' .
htmlentities($_POST['di-message']);
//////////////////////////////////////////////////////////////
if ( isset( $_POST['di-submit'] ) ) {
if ( ! empty( $_POST['di-email'] ) || ! empty( $_POST['di-email'] ) || ! empty( $_POST['di-message'] ) ) {
mail($to, $subject, $message, $headers);
$_SESSION['success'] = '<p class="success">' . __('Message sent succesfully', 'di-news-blog') . '</p>';
wp_redirect(get_template_directory_uri() . '/contact'); exit;
} else {
$_SESSION['error'] = '<p class="failed">' . __('Please enter all the requested data and try again', 'di-news-blog') . '</p>';
wp_redirect(get_template_directory_uri() . '/contact'); exit;
}
}
但是笨手笨脚的我,我刚刚发现 wordpress 函数像get_option()
或__('','')
不会在像 contact.php 这样的页面中工作,还有 $_SESSION 超全局,我被告知我可以让它在functions.php
文件中使用类似的东西:
// SESSIONS //////
function register_my_session(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_my_session', 1);
function cyb_session_start() {
if( ! session_id() ) {
session_start();
}
}
function cyb_session_end() {
session_destroy();
}
add_action( 'init', 'cyb_session_start', 1 );
add_action( 'wp_logout', 'cyb_session_end' );
add_action( 'wp_login', 'cyb_session_end' );
这不起作用。
抱歉我的无知,但我找不到关于如何实现这一点的文档(我理解,不是 nijna php dev)。
我会将表单 POST 发送回/contact
(联系表单页面)并处理从那里发送的表单。
为了保持模板干净,我通常会创建一个函数,functions.php
然后您可以在其中从模板调用,并且我会在所调用的主题中创建一个特定的联系页面模板page-contact.php
(它将自动用于带有 slug 的页面contact
)。这将是唯一需要调用联系表单处理程序函数的模板。
您实际上可以将 Wordpress 包含在您自己的自定义(非模板)文件中,以便您可以使用 wordpress 功能,但我认为这对于您要执行的操作没有必要。
一个非常小的例子:
页面contact.php
<?php get_header(); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php contact_form_handler(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php include_once 'templates/no-posts-found.php'; ?>
<?php endif; ?>
<?php get_footer(); ?>
函数.php
function contact_form_handler() {
/* If this is a postback, handle the contact form */
if(isset($_POST['message'])) {
//Try to send the message and set $successful to true or false
echo ($successful ? "Message sent!" : "An error occurred");
}
/* Otherwise display the form itself */
else {
include_once 'templates/contact-form.php';
}
}
模板/contact-form.php
<form method="POST" action="/contact">
<textarea name="message"></textarea>
<input type="submit" value="Submit" />
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句