WordPress的管理页面:wpdb

编程入门 行业动态 更新时间:2024-10-28 18:22:11
WordPress的管理页面:wpdb-> query()不保持重定向(Wordpress admin page: wpdb->query() doesn't keep redirect)

我有一个插件,它将HTML片段保存在_transients中。 现在,我希望能够让用户通过单击按钮来删除所有相关的瞬变。 我已经设法创建一个按钮,发送适当的请求到options.php页面,获取请求,并执行查询,但问题是,我留在/options.php,我想重定向到我的插件页面。 以下是代码的相关部分:

function fbClearTransients(){ global $wpdb, $_wp_using_ext_object_cache; if(!empty($_POST["fb-clear-transient"]) && intval($_POST["fb-clear-transient"]) == 1) { $sql = "DELETE FROM $wpdb->options WHERE option_name LIKE '%_transient_fb_%'"; $clean = $wpdb -> query( $sql ); } } function fbClearCache() { $clear = fbClearTransients(); } add_action('admin-init', 'fbClearCache'); [...] <form action="options.php" method="post"> <input type="hidden" name="fb-clear-transient" value='1'> <?php submit_button( __('Clear cache'), 'secondary', 'fb_clear_trans_button', false ); ?> </form>

所以,总结一下:查询工作,一切正常,除了我没有重定向回我的插件页面,就像使用初始保存按钮 - 保存设置的那个一样。

I have a plugin that keeps HTML snippets in _transients. Now, I want to be able to let the users delete all the related transients by clicking on a button. I've managed to create a button, send proper request to options.php page, get the request, and execute query, but the problem is that I'm left at /options.php and I want to redirect to my plugin page. Here are relevant parts of the code:

function fbClearTransients(){ global $wpdb, $_wp_using_ext_object_cache; if(!empty($_POST["fb-clear-transient"]) && intval($_POST["fb-clear-transient"]) == 1) { $sql = "DELETE FROM $wpdb->options WHERE option_name LIKE '%_transient_fb_%'"; $clean = $wpdb -> query( $sql ); } } function fbClearCache() { $clear = fbClearTransients(); } add_action('admin-init', 'fbClearCache'); [...] <form action="options.php" method="post"> <input type="hidden" name="fb-clear-transient" value='1'> <?php submit_button( __('Clear cache'), 'secondary', 'fb_clear_trans_button', false ); ?> </form>

So, to sum up: query works, everything works, except that I'm not redirected back to my plugin page, as with initial Save button - the one that saves the settings.

最满意答案

鉴于我已经修改了原来的查询,将我带到了admin.php?page = myplugin,我需要以某种方式告诉系统我想在那里结束,因此简单地添加:

header('Location: admin.php?page=my-plugin');

为我修好了。

Given that I've modified original query that would've take me to the admin.php?page=myplugin, I needed to somehow tell the system I want to end up there, so adding simply:

header('Location: admin.php?page=my-plugin');

fixed it for me.

更多推荐

本文发布于:2023-04-28 07:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   WordPress   wpdb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!