Shahid Karimi
Shahid Karimi

Reputation: 4357

Wordpress plugin form issue

I am new to wordpress plug in development. I have designed a search form however I have no idea where to handle and print the submitted form data. it is a wedget based plug in and the plugin form section code is here:

   function widget( $args, $instance ) {
    extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );
                $message = apply_filters( 'widget_content', $instance['content'] );
        echo $before_widget;
        //if ( $title )
        //  echo $before_title . $title . $after_title;
        echo '<div class="shk_location_form_holder">
                    <span class="shk_loc_title">'.$title.'
                    <form mthod="post">
                    <input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
                    <div style="height:5px"></div>
                    <input type="submit" Value="Search Locations" />
                    </form></div>';
        echo $after_widget;
                if(isset($_REQUEST['shk_inp_search_locations'])){
                    add_filter('the_content','handle_content');
                }
    }

Upvotes: 1

Views: 845

Answers (1)

Mr. BeatMasta
Mr. BeatMasta

Reputation: 1312

In WP plugins you usually have an empty action="" in a form, and handle it in the same function (by the way, as wordpress procedural code becomes very messy, it's better to write plugins using OOP), because, anyway plugins are loaded before any content is outputted in WP (this is the reason why writing ajax plugins is so easy in wp). So you can have everything structured like this:

function draw_form() {
    handle_submit();
?>
<div class="shk_location_form_holder">
    <span class="shk_loc_title"><?php echo $title; ?></span>
    <form mthod="post" action="">
        <input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
        <div style="height:5px"></div>
        <input type="submit" Value="Search Locations" />
    </form>
</div>
<?
}

function handle_submit() {
    if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') {
        echo 'you may want to end your program here, especially if it\'s ajax!';
        exit;
    }
}

Upvotes: 1

Related Questions