【问题标题】:Adapt PHP form action for WordPress?为 WordPress 调整 PHP 表单操作?
【发布时间】:2018-04-21 19:22:54
【问题描述】:

我有一个 PHP 表单,我想适应 WordPress。我通过并制作了一个插件来创建一个小部件和一个表单。表单操作转到我创建的名为 listrak-newsletter-api.php 的文件,但是当我在下面的表单中提交时,我收到 404 错误。

这些文件都位于我的/wp-content/plugins/listrak-newsletter-api 目录中。

在 WordPress 之外独立运行,效果很好。但是自从将其迁移到 WordPress 中后,它变得相当复杂。我曾经有一个简单的 HTML 页面,其中包含一个对 listrak-newsletter-api.php 进行表单操作的表单,并且效果很好。但将其纳入 WordPress 似乎比应有的难度更大。

现在,我想将其保留为小部件,因为我可以将小部件放置在 WordPress 主题的侧边栏上我想要的位置。它出现在哪里以及当我激活它时它是如何出现的都很棒。功能只需要工作即可。

这个文件是/wp-content/plugins/listrak-newsletter-api/plugin.php

<?php

/**
* Plugin Name: Listrak Newsletter API
* Description: Newsletter integration with Listrak.
* Version: 1.0
*/

// Register and load the widget
function wpb_load_widget()
{
    register_widget('wpb_widget');
}
add_action('widgets_init', 'wpb_load_widget');

// Creating the widget 
class wpb_widget extends WP_Widget
{

    function __construct()
    {
        parent::__construct(
        // Base ID of your widget
            'wpb_widget', 
        // Widget name will appear in UI
            __('WPBeginner Widget', 'wpb_widget_domain'), 
        // Widget description
            array(
            'description' => __('Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain')
        ));
    }

    // Creating widget front-end

    public function widget($args, $instance)
    {
        $title = apply_filters('widget_title', $instance['title']);

        // before and after widget arguments are defined by themes
        echo $args['before_widget'];        

        // This is where you run the code and display the output
        echo '<div class="block-title"><span>EMAIL NEWSLETTER</span></div>';
        echo '<form action="/wp-content/plugins/listrak-newsletter-api.php" method="post">';
        echo '  <div class="tnp-field tnp-field-email"><label>Email</label>';
        echo '  <input class="email" name="email" required="" type="email"></div>';     
        echo '  <div class="tnp-field tnp-field-button"><input class="tnp-submit" value="Subscribe now!" type="submit"></div>';
        echo '</form>';
        echo $args['after_widget'];
    }

    // Widget Backend 
    public function form($instance)
    {
        if (isset($instance['title'])) {
            $title = $instance['title'];
        } else {
            $title = __('New title', 'wpb_widget_domain');
        }
        // Widget admin form
        ?>
        <p>
        <label for="<?php
                echo $this->get_field_id('title');
        ?>"><?php
                _e('Title:');
        ?></label> 
        <input class="widefat" id="<?php
                echo $this->get_field_id('title');
        ?>" name="<?php
                echo $this->get_field_name('title');
        ?>" type="text" value="<?php
                echo esc_attr($title);
        ?>" />
        </p>
        <?php
    }

    // Updating widget replacing old instances with new
    public function update($new_instance, $old_instance)
    {
        $instance          = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        return $instance;
    }
} // Class wpb_widget ends here

?>

这个文件是/wp-content/plugins/listrak-newsletter-api/listrak-newsletter-api.php

<?php

$host = $_SERVER['HTTP_HOST'];

if (isset($_POST['action'])) {

    $email = $_POST['email']; //obtain email from post, place into $email variable
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); //sanitizing email
    //$theAction = $_POST['action'];
    //wpSubscription($host, $email, $theAction);
    //$redirect = $_POST['redirect'];
    //header('Location: ' . $redirect);    

    if ($_POST['email'] == '') {
        echo "Please enter an email address";
    }
    if ($host == network_site_url()) {
        $sh_param   = array( //setting username & password array
            'UserName' => "",
            'Password' => ""
        );
        $authvalues = new SoapVar($sh_param, SOAP_ENC_OBJECT); //encoding username and password array
        $headers[]  = new SoapHeader("http://webservices.listrak.com/v31/", 'WSUser', $sh_param);
        $soapClient = new SoapClient("https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL", array(
            'trace' => 1,
            'exceptions' => true,
            'cache_wsdl' => WSDL_CACHE_NONE,
            'soap_version' => SOAP_1_2
        ));

        $soapClient->__setSoapHeaders($headers);
        $params = array( //parameters for soap xml integration with listrak
            'WSContact' => array(
                'EmailAddress' => $email,
                'ListID' => ''
            ),
            'ProfileUpdateType' => 'Overwrite',
            'ExternalEventIDs' => '',
            'OverrideUnsubscribe' => true
        );

        try {

            $rest = $soapClient->SetContact($params); //using SetContact method, send parameters

        }
        catch (SoapFault $e) { //if an error occurs, display it

            echo '<pre>';

            print($e->getMessage());

            echo '</pre>';
        }
    }
}
?>

【问题讨论】:

标签: php html wordpress forms


【解决方案1】:

我不知道您希望将旧表单保留到何种程度,因为如果它是您需要的联系表单,那么像联系表单 7 或忍者表单之类的插件会更轻松地解决问题。

尽管如此,如果您想保留表单及其背后的 php 逻辑,请先了解该表单的工作方式:

以下三种情况需要您创建一个子主题,这样您的更改不会在主题更新后被扫除。

案例 1) 提交后您在不同的页面结束。

您为处理表单的页面创建一个新模板,然后创建一个新页面并为其分配您刚刚创建的模板。

此模板将具有 php 来获取发布的值并存储它们或通过电子邮件发送它们,并将一些输出设置为 OK 或 ERROR。

在页面中您可以自定义内容部分或在模板中做任何事情。

2) 提交后您停留在同一页面(重新加载)。

这里您需要将用于放置表单的页面的主题提供的模板复制到子主题中,并进行更改以添加表单的处理。

3) 留在同一页面并使用 ajax 处理提交。

在这里,您必须设置一个 wp_ajax_* 处理程序以获取最有可能使用 jQuery 发送的数据,并在处理程序中处理数据。最后显示一些从处理程序传递到 ajax 调用的 ok 或错误消息。

要使这些工作中的任何一个工作,您还必须使用 wp_enqueue_scripts 和 wp_enqueue_styles 来提供实现工作所需的 css 和 javascript。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多