gpt4 book ai didi

wordpress - 从 add_menu_page 重定向

转载 作者:行者123 更新时间:2023-12-02 22:59:23 25 4
gpt4 key购买 nike

我使用 add_menu_page 添加了一个菜单,该菜单一切正常,但单击时我希望此菜单页面打开特定帖子 ID 的帖子编辑器。

作为概念证明,我尝试将 javascript 重定向回显到 do 函数中,如下所示...

// Load up the menu page
function register_availability_custom_menu_page() {
add_menu_page('custom menu title', 'Availability', 'add_users', 'options', 'options_do_page');
}

function options_do_page() {
echo "<script type=\"text/javascript\">";
echo "window.location = '/whatever_page.php';";
echo "</script>";
}

这种方法确实有效,但我想知道这是否是最好的方法,是否有更好的方法来重定向到我所在的页面?

更新

我现在也尝试使用 wp_redirect 与此代码...

add_action( 'admin_menu' , 'admin_menu_new_items' );

function admin_menu_new_items() {
wp_redirect( home_url() );
exit;
}

这给了我一个标题已发送错误,有人可以建议我哪里出错了吗?

最佳答案

如果我理解正确,那么您不需要重定向。而不是在函数 add_menu_page 中使用 $menu_slug ,输入目标页面的地址,例如:

$the_post_title = 'The Portfolio';

add_action( 'admin_menu', 'wpse_59050_add_menu' );

function wpse_59050_add_menu()
{
global $the_post_title;
$our_page = get_page_by_title( $the_post_title );

$settings_page = add_menu_page(
'Edit '.$our_page->post_title,
'Edit '.$our_page->post_title,
'add_users',
'/post.php?post='.$our_page->ID.'&action=edit',
'',
'',
2
);
}

该函数来自以下 WordPress 答案:Highlighting a Menu Item by Post Name 。您需要一些 jQuery 来正确突出显示该顶级菜单,请检查我和 TheDeadMedic 在该问题中的答案。

另一个也很有用:Add highlighting to new Admin Dashboard Menu Item .

关于wordpress - 从 add_menu_page 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com