wordpress 站点中的自定义表单和操作属性

马科斯·迪保罗

我已经陷入了(我不知道的)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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 wordpress 站点中自定义属性

来自分类Dev

如何在 Wordpress 站点中插入来自自定义表单的数据?

来自分类Dev

Wordpress中的自定义表单操作

来自分类Dev

在自定义模板上操作Wordpress Menu href属性

来自分类Dev

WordPress和自定义编码

来自分类Dev

WordPress和自定义htaccess规则

来自分类Dev

Wordpress Woocommerce 向产品添加属性/自定义属性

来自分类Dev

在Wordpress中的自定义管理页面上提交表单

来自分类Dev

WordPress管理表单自定义$ _POST请求

来自分类Dev

自定义Wordpress Woocommerce帐单地址编辑表单

来自分类Dev

CSS Wordpress联系人表单自定义

来自分类Dev

自定义登录表单到WordPress管理栏

来自分类Dev

将自定义PHP表单添加到Wordpress

来自分类Dev

如何在wordpress上创建自定义联系表单?

来自分类Dev

WordPress中对user_register挂钩的自定义操作

来自分类Dev

WordPress的-自定义标题

来自分类Dev

自定义WordPress主题

来自分类Dev

API Wordpress自定义

来自分类Dev

WordPress的-自定义标题

来自分类Dev

自定义 WordPress 循环

来自分类Dev

wordpress 站点中的滑块效果

来自分类Dev

在 Woocommerce Wordpress 管理中自定义保存属性按钮

来自分类Dev

加载自定义wordpress插件的js和css文件

来自分类Dev

WordPress meta_query和orderby自定义字段

来自分类Dev

WordPress自定义页面模板和WP-API

来自分类Dev

WordPress MetaSlider和高级自定义字段集成

来自分类Dev

新的WordPress用户的自定义登录和注册

来自分类Dev

Wordpress中的自定义链接和名称元框

来自分类Dev

在自定义div中包装wordpress帖子和标题