我有一个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>\';
}
}
}
?>