[wp-trac] [WordPress Trac] #39003: menu_page_url() not working on Ajax call
WordPress Trac
noreply at wordpress.org
Mon Oct 14 15:34:48 UTC 2024
#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 davidhbrown):
Replying to [comment:6 zamandevs]:
> Use cases needed.
My use case is that I have one admin subpage for editing (or creating new)
records and another for displaying the listing of these records. To reduce
clutter and help avoid accidental clicks on the listing page, records can
be deleted only from their individual edit page. After deletion, further
edits are invalid, so it would be preferable to return to the listing
subpage. The delete is handled by an ajax call (as are edits) and the
response to a successful deletion would include a location URL -- the
listing page -- which the receiving JavaScript would assign to
window.location.
(Just creating a custom post type wouldn't be appropriate to this
application. These records are, as it happens, linked to a custom post
type and these pages are subpages of that post type's admin page, but the
records need to be used in a non-WordPress context in a different database
with different permissions.)
> It doesn't make sense to get the admin menu in the ajax call as all of
the menus load on admin init. if requires ajax can explicitly include
necessary menu files as we do in the other areas.
I do agree with the general principle of not loading more of WordPress
than you have to for ajax calls.
Workarounds for this case require calling menu_page_url() while generating
the admin page and storing the URL(s) somewhere client-side in the
delivered HTML or JavaScript. E.g., set a property in the localized
JavaScript (wp_localize_script) that contains the appropriate URL and
return the name of that property instead of the direct URL. Could also
create a hidden input or data-location property or something in the HTML,
but that seems messier.
(I implemented the first option; it's working fine for me.)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39003#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list