WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What i want to do is fire up the add_submenu_page() function from within ajax on my plugin options page.

When i click an "Add League" link, i want to display an ajax input[text] that will save into the database, then add the submenu page using the values that have been inserted.

I am also hoping to be able to delete the entry as well as the newly created page.

share|improve this question
Please show us the code you have so far. – kaiser May 2 '12 at 11:25
Don't understand the upvotes... IMO, without sample code, this Q goes nowhere... Unless some good soul decides to build a test from scratch, or already has the exact functionality programmed. – brasofilo Jul 23 '12 at 16:02

As you can't suppose to get a full working example without any given code, I'll show you a rough wordpress AJAX setup:

Put all this in functions.php:

add_action('wp_head', 'my_action_javascript');
function my_action_javascript() {
<script type="text/javascript" >
jQuery(document).ready(function($) {
        type: 'POST',
        url: <?php echo admin_url('admin-ajax.php'); ?>,
        data: {
            action : 'your_AJAX_call',
            league : $('input#league').val() // get the user input
        success: function( response, textStatus, XMLHttpRequest ) {
        error: function( XMLHttpRequest, textStatus, errorThrown ) {
            alert('An error occured');

add_action( 'wp_ajax_nopriv_your_AJAX_call', 'your_AJAX_call' );
add_action( 'wp_ajax_your_AJAX_call', 'your_AJAX_call' );
function your_AJAX_call() {
    $league = $_POST['league'];
    // do sanitization
    // if sanitization is ok:
        // save to DB
        // echo sth like '<li><a href="new-league">New League</a></li>'
    exit;  // won't work without!

I recommend to return a json-encoded array, so you could provide more-detailed information. I.e. if the sanitization fails because it is too short:

// Your sanitization logic here
$arr = array ('success'=>false,'error'=>'Length must be at least 3 Characters');
echo json_encode($arr);


Then in your response function of the ajax call:

var response = jQuery.parseJSON(response);
alert('An error occured: ' + response.error);


Here is a good write-up about using ajax in wordpress:



Totally forgot about this:

add_submenu_page() won't work, because the page is already rendered at this point.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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