Redirect from add_menu_page

I added a menu using add_menu_page, which everything works correctly, but when I click this page I want this menu page to open a message editor for a specific message identifier.

As a proof of concept, I tried to redirect javascript redirect to do function like this ...

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

This approach really works, but I was wondering if this is the best approach, is there a better way to redirect to the page I'm after?

UPDATE

I also tried using wp_redirect with this code ...

 add_action( 'admin_menu' , 'admin_menu_new_items' ); function admin_menu_new_items() { wp_redirect( home_url() ); exit; } 

This gives me a message about an error already posted, can anyone suggest where I am going wrong?

+4
source share
3 answers

If I understand this correctly, you do not need a redirect. Instead of using $menu_slug in the add_menu_page function add_menu_page put the address of the landing page, for example:

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

This feature is taken from the following WordPress post: Highlighting a menu item by name . You will need jQuery to make this top-level menu highlight properly, check both my and TheDeadMedic answers in this Q.

This other is also useful: Add highlight to a new menu item in the admin control panel .

0
source

I found a two-step solution that does not use JS / JQ.

Step 1:
At the top of your script, install the following PHP to display CSS, which hides your first page:

 add_action('admin_head', 'hide_my_first_page'); function hide_my_first_page(){ echo '<style> a[href="admin.php?page=admin_menu_slug"] + ul > li.wp-first-item{ display: none; } </style>'; } 

Where admin_menu_slug is the 4th argument passed to add_menu_page(); .

Step 2:
Take the function of the page you want to run and pass it as the 5th argument to add_menu_page();

0
source

This has recently done for the pootle page builder , and this AFAIK is the best way,

 /** * Redirecting from Page Builder > Add New page * @since 0.1.0 */ function add_new() { global $pagenow; if ( 'admin.php' == $pagenow && 'page_builder_add' == filter_input( INPUT_GET, 'page' ) ) { header( 'Location: ' . admin_url( 'whatever-page.php' ) ); die(); } } add_action( 'admin_init', 'add_new' ); 

Replace page_builder_add with your slug page and admin_url( 'whatever-page.php' ) URL you want to redirect to.

We always use scrutinizer-ci for best code practice and maintain a code rating of more than 9.5 / 10, so all code (including this) is safe and optimized;)

Lemme knows how this works for ya.

Greetings

0
source

All Articles