1

I have a PHP form that I'd like to adapt for WordPress. I went through and made a plugin that creates a widget and a form. The form action goes to a file I created called listrak-newsletter-api.php but when I submit to it in the below form, I get a 404 error.

These files are all located in my /wp-content/plugins/listrak-newsletter-api directory.

Stand alone, outside of WordPress, this works great. But since migrating it into WordPress, it's becoming quite convoluted. I used to have a simple HTML page with a form that had a form action to listrak-newsletter-api.php and that worked great. But taking this into WordPress seems to have made it a little more difficult than it should be.

Now, I want to keep it as a widget because I'm able to place the widget where I want, on the sidebar, of the WordPress theme. Where it shows up and how it shows up when I activate it are great. Functionality just needs to work.

This file is /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

?>

This file is /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>';
        }
    }
}
?>
0

I think your action url is wrong. Did you try with <?php echo plugins_url( 'listrak-newsletter-api.php', __FILE__ ); ?>

like this way echo '<form action="' . plugins_url( 'listrak-newsletter-api.php', __FILE__ ) . '" method="post">'; on you code

Anyway I still recommended you to use WP Ajax to do this read here for more info https://codex.wordpress.org/AJAX_in_Plugins

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.