So I have been working on this for... 3 days now - and have resorted to just pasting in the code directly from the wordpress codex to get that to work then go from there.

That code however doesn't work so I am assuming the problem is something fundamental - and hopefully simple.

This is how I call the admin page in my main plugin file:

add_action('admin_menu', 'hostel_booking_menu');

function hostel_booking_menu() {
add_menu_page('Hostel Calendar Settings', 'Hostel Booking', 'administrator', 'hostel-booking-settings', 'hostel_booking_settings_page', 'dashicons-admin-generic');
add_submenu_page( 'hostel-booking-settings', 'Create Delete Rooms', 'Rooms', 'administrator', 'hostel-booking-settings-rooms', 'hostel_booking_settings_page_rooms' );
add_submenu_page( 'hostel-booking-settings', 'Orders', 'Orders', 'administrator', 'hostel-booking-settings-orders', 'hostel_booking_settings_page_orders' );
add_submenu_page( 'hostel-booking-settings', 'Settings', 'Settings', 'administrator', 'hostel-booking-settings-reservations', 'hostel_booking_settings_page_reservations' );


function hostel_booking_settings_page_rooms() {
include 'view-create-rooms.php';

Here is the relevant code right at the top of view-create-rooms.php (that is copied directly from the codex):


add_action( 'wp_ajax_my_action', 'my_action_callback' );

function my_action_callback() {
  global $wpdb; // this is how you get access to the database

  $whatever = intval( $_POST['whatever'] );

  $whatever += 10;

        echo $whatever;

  wp_die(); // this is required to terminate immediately and return a proper response

  <script type="text/javascript" >
  jQuery(document).ready(function($) {

    var data = {
      'action': 'my_action',
      'whatever': 1234

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    $.post(ajaxurl, data, function(response) {
      alert('Got this from the server: ' + response);

It is clearly being sent to the right file as I get the classic admin ajax response of "0", therefore whenever I refresh this page I get an error message saying: "Got this from the server: 0"

I have added the admin menu as per many tutorials and can only assume it is something simple and fundamental. I am testing this on a local install but have tested the plugin on two local installs one with the latest version of WP with the 2015 theme.

Any help will be GREATLY appreciated.


1 Answer 1


Your AJAX add_action and associated callback function shouldn't be inside view-create-rooms.php, move it into your main plugin file. When an AJAX request is sent, you're making a new separate request to the server, so your submenu page will not be loaded and your AJAX action never gets added to the queue.

  • Ok, firstly - thank god for stackexchange! Secondly - why on earth does it not mention this in the codex page on ajax for plugins??? Context of where the code should go isn't mentioned at all for admin side. Don't want to ask too much but just to clarify - does this mean that every ajax callback needs to be inside the main function file because looking at other plugins there doesn't seem to be a whole lot in the main file. Lastly, (without being too cheeky) I have recently learned PHP but clearly don't understand the fundamentals or the mechanics - any resources you can recommend?
    – Rob Frost
    Jun 20, 2015 at 17:14
  • Again, many thanks! (Couldn't fit that on the first comment)
    – Rob Frost
    Jun 20, 2015 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.