我是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');
}
}发布于 2011-12-31 09:52:52
在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;
}
}https://stackoverflow.com/questions/7836119
复制相似问题