[wp-trac] [WordPress Trac] #39003: menu_page_url() not working on Ajax call

WordPress Trac noreply at wordpress.org
Fri Dec 27 16:55:48 UTC 2019


#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 vinoth06):

 Hi donmhico

 Thanks for you code, I have modified your code which will get the result
 from AJAX request. Kindly check and share feedback.

 I have created as an plugin, so you can add it in your plugin directory to
 activate.

 I have added the comments too for your clarifications.

 {{{#!php
 <?php
 /**
  * Plugin Name: Menu Page URL on AJAX
  * Plugin URI: https://buffercode.com/plugin/frontend-dashboard
  * Description: Menu Page URL on AJAX
  * Version: 1
  * Author: vinoth06
  * Author URI: https://buffercode.com/
  * License: GPLv2
  * License URI: https://www.gnu.org/licenses/gpl-2.0.html
  *
  */

 if ( ! defined( 'ABSPATH' ) ) {
         exit;
 }

 add_action( 'admin_init', 'enqueue_jquery' );
 function enqueue_jquery() {
         wp_enqueue_script( 'jquery' );
 }

 // Create admin menu page.
 function test_admin_menu_page() {
         add_menu_page( 'Test Menu', 'Test Menu', 'manage_options',
 'test_menu_slug', 'test_menu_page' );
 }

 // Hope its not admin_init. it should be admin_menu like in the line 21,
 Correct me if am wrong
 //add_action( 'admin_init', 'test_admin_menu_page' );
 add_action( 'admin_menu', 'test_admin_menu_page' );

 // AJAX handler.
 function test_ajax_menu_page_url() {
         // 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', 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' );

 function test_menu_page() {
         ?>
     <form method="post" class="submitOnAjax" action="<?php echo admin_url(
 'admin-ajax.php?action=ampu' ) ?>">
         <button type="submit">Submit</button>
     </form>
     <script>
         jQuery(document).ready(function ($) {
             $('body').on('submit', '.submitOnAjax', function (e) {
                 var form = $(this);
                 $.ajax({
                     type: 'POST',
                     url: form.attr('action'),
                     data: form.serialize(),
                     success: function (results) {
                         //Check the Value here
                         console.log(results);
                         // It will echo empty string
                     }
                 });
                 e.preventDefault();
             });
         });
     </script>
         <?php
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/39003#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list