【发布时间】: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>';
}
}
}
?>
【问题讨论】:
-
有一个WordPress开发问题的网站:wordpress.stackexchange.com