[wp-trac] [WordPress Trac] #39003: menu_page_url() not working on Ajax call
WordPress Trac
noreply at wordpress.org
Fri Mar 26 15:26:21 UTC 2021
#39003: menu_page_url() not working on Ajax call
---------------------------------------------+-----------------------------
Reporter: vinoth06 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting
| Review
Component: Administration | Version: 4.6.1
Severity: normal | Resolution:
Keywords: needs-testing reporter-feedback | Focuses:
---------------------------------------------+-----------------------------
Comment (by donmhico):
Hello @vinoth06,
Sorry for the delay in response. I re-checked and you are right
`menu_page_url()` returns nothing in AJAX. I believe it's because `global
$_parent_pages;` is `NULL` in AJAX request vs in normal admin request.
Upon diving a little deeper, I don't see `menu.php` being loaded in
`admin-ajax.php`. So what you can do is edit your function to be
{{{#!php
<?php
// AJAX handler.
function test_ajax_menu_page_url() {
// Include the menu.
if ( WP_NETWORK_ADMIN ) {
require ABSPATH . 'wp-admin/network/menu.php';
} elseif ( WP_USER_ADMIN ) {
require ABSPATH . 'wp-admin/user/menu.php';
} else {
require ABSPATH . 'wp-admin/menu.php';
}
// Outputs - http://wp.test/wp-admin/admin.php?page=test_menu if
logged in as admin.
// Outputs nothing if logged-out.
echo menu_page_url( 'test_menu_slug', false );
// Try the below it will give the exact URL of the page.
//echo admin_url( '/admin.php?page=test_menu' );
// Conclusion: If you use menu_page_url in ajax return it will send
empty string.
wp_die();
}
add_action( 'wp_ajax_ampu', 'test_ajax_menu_page_url' );
add_action( 'wp_ajax_nopriv_ampu', 'test_ajax_menu_page_url' );
}}}
As for including this in the core, i'm not sure if we are suppose to have
the `menu` available in AJAX. Let's hear more feedback.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39003#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list