Dims
Dims

Reputation: 51229

How to issue post requests from WordPress sidebar?

Is it possible to issue post requests from WordPress sidebar?

I want to open page wich requires some parameters to be sent by POST request. But I found that neither FORM tag, not JAVASCRIPT or ONCLICK syntax is not allowed in text widget in sidebar.

Is this any way to accomplish this?

Thanks

UPDATE 1

I am on the free account on wordpress.

Upvotes: 2

Views: 170

Answers (2)

Justin Nafe
Justin Nafe

Reputation: 3062

You may want to create your own plugin and widget for this. You could output the form and input tags within your widget.

In the example below, you could output the form in the "widget" function. For example, you could create a directory in your plugins directory and name it foo, then create a php file called foo.php and use code similar to the following:

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/

    /**
     * Foo_Widget Class
     */
    class Foo_Widget extends WP_Widget {
        /** constructor */
        function __construct() {
            parent::WP_Widget( /* Base ID */'foo_widget', /* Name */'Foo_Widget', array( 'description' => 'A Foo Widget' ) );
        }
    /** @see WP_Widget::widget */
    function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );
        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title; ?>
        <form action="" method="post">
                    <input type="text" name="mytext" />
            <input type="text" name="result" value="<?php echo isset($_POST['mytext']) ? $_POST['mytext'] : ''; ?>" />
<input type="submit" name="submitbutton" value="Submit" /> </form>
        <?php echo $after_widget;
    }

    /** @see WP_Widget::update */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form( $instance ) {
        if ( $instance ) {
            $title = esc_attr( $instance[ 'title' ] );
        }
        else {
            $title = __( 'New title', 'text_domain' );
        }
        ?>
        <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 $title; ?>" />
        </p>
        <?php 
    }

    } // class Foo_Widget
    // register Foo_Widget widget
    add_action( 'widgets_init', create_function( '', 'register_widget("Foo_Widget");' ) );
?>

Upvotes: 1

James Kemp
James Kemp

Reputation: 349

Are you looking to avoid editing the template code?

If not, then you should be able to open up sidebar.php from your Wordpress theme directory and it it directly in there.

Upvotes: 1

Related Questions