Wordpress插件表单问题



我是wordpress插件开发的新手。我设计了一个搜索表单,但我不知道在哪里处理和打印提交的表单数据。这是一个基于楔形的插件,插件表单部分代码在这里:

   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');
                }
    }

在WP插件中,你通常在表单中有一个空的action=",并在相同的函数中处理它(顺便说一下,由于wordpress过程代码变得非常混乱,最好使用OOP编写插件),因为无论如何插件都是在WP输出任何内容之前加载的(这就是为什么在WP中编写ajax插件如此容易的原因)。所以你可以把所有的东西都组织成这样:

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;
    }
}

最新更新