Using AJAX In Your WordPress Theme Admin

/ Wordpress / by Paul Robinson / 139 Comments
This post was published back on January 15, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.
This post is very old now. While it is still valid in terms of showing how to use AJAX in WordPress, please note it lacks any form of validation or sanitization.

I recently received an e-mail asking if I could help make an admin for a theme that was powered by AJAX & therefore did not need the page to be refreshed to save the data. Unfortunately I couldn’t help as although I have built admins for plugins & themes before, I didn’t have the knowledge to add the AJAX functionality to it. However I’ve spent the past day or so reading & trying to understand the theory behind AJAX usage within WordPress’ admin & I think I’ve managed to come up with something.

Good For You! How Does That Help Me?

Well I wanted to understand it so I could write a simple tutorial for those who are getting into theme development & want to add that little bit of ‘je ne sais quoi’ to the admin of a theme.

So How Do We Start?

We add our admin page link to WordPress’ menu. Since we are going to add code into the header we need to do a few things different to a normal admin page or the header code will be output to all of the pages in the WordPress admin. Exactly what we don’t want since it is useless for anything other than running our admin page.

Here is the code to add your page into the WordPress admin:

First we call add_action() this tells WP to run a function into the admin_menu hook. The function test_add_theme_page, change the name if you like (just make sure they match), checks that the admin page we are on is actually the current file. If it is, we can add the code into the header (code coming in a minute) using the admin_head hook. No matter what page we are on we still need to add the link for the admin page into the admin menu so we run the function add_theme_page(). I explained the useage in my other tutorial, but just in case here it is again.

__(‘Test Admin’)
This is the text shown as the browser title, that’s the text shown in the title bar of your browser.
__(‘Test Admin’)
This is the text shown as the link title in the WordPress menu. It will appear under the Appearance tab
edit_theme
This is the role required to access the page.
basename(__FILE__)
This is a unique identifier. I’ve used the current file’s filename.
test_theme_page
The function that will create the contents of the options page.

Adding the jQuery

As I mentioned earlier add_action('admin_head', 'test_theme_page_head'); adds code into the header of the page. This is what we will use to add our jQuery code. Here is the function I have used:

Ok, I’m going to attempt to explain everything written here. It might take a while so bare with me. 😆

First we define the function we named in our add_action() call. We then break out of PHP so we can easily add our jQuery code. We open a script tag as normal, then we start our jQuery with a standard run on DOM load. I’ve passed the $ through the run on DOM function so you should be able to use the standard $ inside it, however I chose to continue using the jQuery that WordPress defines for conflict prevention with other JS libraries.

Next we target our form, which hasn’t been made yet, so pick an ID or something here for it, and attach a function to it’s submit event. We serialize the data from the form, this excludes the submit buttons value & encodes data in standard URL query format, storing it in the variable data. We can use jQuery(this) because we are inside the element that is holding the data, in this case the form. Next we create a standard AJAX call using jQuery’s jQuery.post function. ajaxurl is a javascript global variable defined by WordPress & should always be used for AJAX requests inside WordPress for security reasons. data is our serialized form data & the the function is to be ran on the completion (callback) of our AJAX request. response is the data returned from the AJAX request.

Inside the callback we check the response data to see if it contains 1. This will be sent back by our AJAX function we will create later, if it is 1 we take it as meaning true & show an options saved box, if it is anything else we show an options not saved box. The message boxes are simple jQuery that show a message in styling defined by WordPress & then use a simple setTimeout & clearTimeout to show & hide the boxes. I’m not going to add to the length of this tutorial by explaining them, if you want to know more about them leave a comment & I’ll explain more about the functions. We finally use return false in the submit event to stop the form from submitting and refreshing the page.

Creating The HTML Form

Next we are going to create the form needed for the data to be inserted by the user. My form isn’t pretty & does absolutely nothing, by that I mean although it saves data the data isn’t used in any way. Here is my code, remember that this function is used by the add_theme_page function we called earlier:

The is the function that creates the page you see when you visit the admin page. Although you can use the header section to add your own CSS code I have just used the default WordPress CSS. That is why the HTML starts with a div with the class of wrap.

The form is the important part. You must give it a name, and the ID must be the same as the one you used in the jQuery submit event function earlier. The interior of the form can include anything your theme requires, however you need 2 extra input fields. These fields are hidden ones that send required data for the AJAX to work correctly. First is the action hidden field, the value should be whatever you want WordPress to call it’s hook that will be used to hook the function we will make to get results from the AJAX call. The other hidden field security is the infamous nonce code used by WordPress for security. The value must be as shown however you can change the word inside wp_create_nonce() to anything you like (preferable something relevent to your theme), remember it though as we’ll need it later.

We have also added in some PHP to recall the options saved in the database. We simply store the options into a variable & then echo out the data into the value fields. In the case of checkboxes we check for the value on & the add ‘checked’ if it is detected. I’ve used a ternary as it is shorter when writing PHP inline with HTML.

Creating The PHP AJAX Function

Ok so we are nearly done. We just have to create the function called by jQuery, so here’s what I ended up with:

First is add_action the name of this hook was determined by the value of our action field in our form, you just prepend wp_ajax_ to the front of it. The function can be called anything you like again, but try to keep them all relevent to your theme.

Now onto the actual function. First we use a function defined by WordPress to check if the nonce key we received is active & valid. This is where you need to recall that string I told you to remember as it has to be written in the first parameter identically to the one you wrote in wp_create_nonce(). If like me you haven’t used the default post value $_POST['_ajax_nonce'] you will need to tell the function what the $_POST key for the nonce code is, in our case security is the code. This is gotten from the name parameter of our hidden form field. Next we transfer our data from the $_POST global array to a variable called $data, we unset both the nonce key & our action which are passed by the form & are no longer needed. One thing to note is that if check_ajax_referer() does not validate the code it will die outputting ‘-1’, so when debuging if you receive that value you know what is doing it.

To stop our next piece of code throwing an error we need to get the options stored in the database & if they are empty create a blank array on the variable that would normally hold them. get_option() asks WordPress to go to the database and get the data in the wp_options table with the key provided. In our case that is test_theme. All we do is check if the returned value is an array. If it is we grab the data. If not we set a blank array.

Next it gets a little strange. I don’t know if it is a bug or not, but if the values sent from the form are the same as the current database values update_option() will return as if it failed, so we need to screen the options first. We check to see if the options in the database are the same as the once recieved from the form submission by checking for differences using array_diff(). To get the differences correctly we check both arrays against each other & then merge the results together. We also set a blank array if there is no data sent to trigger our jQuery error. If $diff is not an empty array we have differences and can safely save the options. If the update is successful stop PHP processing & echo out 1 to our jQuery function. If it fails then stop processing & echo 0 to our jQuery function. Else if the variable $diff is empty we do not need to update the database since the options are identical so just stop processing & echo out 1.

A quick note about update_option(). You can change the part in the parentheses to anything you like, but try to make it unique & relevent to your theme in some way. Also you may wonder how it is storing an array in a single database field, this is because update_option() auto serializes an array before saving it. In the same vein, get_option() unserializes a serialized array back into a normal array when it grabs data. These are important things to remember when using both these functions.

We’ll that is it, I think. I don’t think I’ve missed anything. I’ve been at this post so long I’ve lost feeling in my legs. 😆 I hope this post helps you, despite it ending up a lot more complicated that I’d wanted it to. You may need to read the post a few times to get a proper understanding of what is going on. If you have any problems or notice something I’ve missed please, please, please email me or leave me a comment & I’ll sort it out as quickly as I can.

Thank you for reading, good luck, and I hope I haven’t made your brain explode. 😉

139 Comments

Author’s gravatar

Wow! Thanks for this Paul. I’m going to take my time going through this.

Reply
Author’s gravatar author

No problem. Let me know if there is anything you need help with. 😉

Author’s gravatar

wow. I think you just saved my life! 🙂

The documentation on the codex is somewhat lacking, thank you so much.

I was pulling some hair out over this, since I could get my jQuery to work otherwise.

It seems redundant to have to code a plugin to do some simple ajax, but there we go.

If you want to see what I’m boxing with, please check out http://wordpress.org/support/topic/359527?replies=1

Thanks again,

Seb

Reply
Author’s gravatar

I do have a question.

Now that I have things working, how come the jquery spits out message #2?

I would be forever grateful for a hint 🙂

Reply
Author’s gravatar

Well someone asked me to create this tutorial so big thanks to them for giving me the kick needed to do this tutorial.

I’m not sure what you mean by ‘message #2’. If you mean you get ‘0’ or ‘-1’ in return it’s probably because without the use of check_ajax_referer() WP will refuse to respond via AJAX for security reasons. Also when using $.post() the URL can be accessed via a JS variable called ajaxurl rather than the way you have in the WP forum post.

Hope that helps & let me know if you need any help. 😉

Author’s gravatar

Thanks for the reply,

First, I am doing this on the front end, so I need to do this:

jQuery.post("",.

I am getting a proper response now.

I don’t know how to check $_POST against the fields in my database, so instead I use check_ajax_referer – which, I guess, is fine if I do validation on the fields to make sure nothing is empty. Perhaps another $_POST field, like the date, which will only be sent if the submit query is sent post validation.

If you have any insights, please do tell. Time for me to find your donate button!

This is what I have so far:


function playlist_insert() {
global $wpdb;
$table_name = $wpdb->prefix . "playlist_master";
$playlist_name=$_POST['playlist_name'];
$description=$_POST['description'];
$setuserid=$_POST['setuserid'];
$setadate=$_POST['setdate'];

$insert = "INSERT INTO " . $table_name .
" (playlist_name, user_id, description, date) " .
"VALUES ('" . $playlist_name . "','" . $setuserid . "','" . $description . "','" . $setadate . "')";

$results = $wpdb->query( $insert );

check_ajax_referer('test_mplsecur', 'mplsecur');

$data = $_POST['mplsecur'];

if(!empty($data)) {
die('1');
} else {
die('0');
}
}

Reply
Author’s gravatar

Oops.

edit: I need to not use ajaxurl 🙂

Reply
Author’s gravatar

Hi,

First off thanks for the donation. 😉 I’ve also cleaned up your code a little, WP can be a little touchy with code in comments, lol.

You may also want to change the way you are entering data into your database too, you need to escape your user inputted variables just in case anyone tries to attack your database. There is a great section on WP’s codex here, but the main idea is this.

When you have user input that will be entered into a database, it needs to be escaped. Here is how your query would look:

That should escape your code to stop any attacks. Sorry if you already know this, I just thought it was important to point out. 😉

I don’t know if this helps, but here is how I would write your code:

I’m not sure if that’s what you were asking, or if I’ve gotten the right idea about what you wanted, but hopefully it’s correct.

I’ve moved check_ajax_referer() to the top as it is a security check to make sure that the AJAX request came from WP & not an external source. It should always be the first thing you do. Next is the prepared version of your query, WP’s perpare function uses sprintf() syntax should you need to look it up. Then we check to see how many rows were affected by the insert, if more than 0 were, great return ‘1’, otherwise it was 0 or FALSE which both mean failure so return ‘-1’. Why ‘-1’? Well that’s what check_ajax_referer() returns should the security check fail. So by making your jQuery validation function check for ‘1’ & ‘-1’ instead of ‘1’ & ‘0’ you kill two birds with one stone. 😉

I hope I haven’t gone overboard, but I wasn’t sure what you were asking & wanted to cover everything I could think of… Let me know if you need any other help, or if I’ve completely misunderstood what you were asking.

Again, a massive thanks for the donation. 😉

Author’s gravatar

Thanks to you too 🙂

I’m trying to learn all this as I go along, so any and all help I can scratch up is valuable.

According to firebug, the modified code responds with 0, hmmm.

Re. iMac. I know the feeling, I am angling for one too!

Reply
Author’s gravatar

Returns ‘0’? Wow… I’m not even sure how that can happen. Are you sending a nonce code when the form is submitted via AJAX, using wp_create_nonce() to make it?

Normally check_ajax_referer() will die and return ‘-1’ on failure, but it is the only thing I can think would cause the ‘0’ to be returned. Very very bizzare. 😆

Yeah. To be honest I’ve never really had a need for a Mac, but more and more technologies are coming out, like the iPad/iPod/iPhone, that require OSX to develop. So the only way to learn using these techs is to get a Mac. I was going to try for a Mac Book, but they are nearly as dear as iMac’s here in the UK, so I couldn’t see the point.

Author’s gravatar

wow, you are fast!

input type="hidden" name="mplsecur" value=" "

this should be fine. Christ I need more hours so I can work and learn more each day and enable debugging skills.

I’m following this at the moment: http://cs75.tv/2009/fall/ – pretty cool, free course from Harvard.

Regarding mac: Currently I run a HP laptop running osx which is so much better than win7 for a work environment, imo. I sleep better at night and at day everything is solid and pretty. Should you want a hackintosh desktop, look into Gigabyte motherboards. Rumor has it they are either built for mac or are used in macs.

Btw, I have an idea for another tutorial: jQuery.load in wordpress 😉

Say “someone” is building a nice ajax form for their users and want to show the result dynamically. WordPress doesn’t allow $('#MyDynamicDiv').load('MyFancySqlAndHtmltables.php');

Reply
Author’s gravatar

Using AJAX in WP can cause security problems & so has to be done very carefully. That’s why WP has functions such as wp_create_nonce() & check_ajax_referer().

I’m assuming that your line of code should have said:

WordPress is a pain when posting code in comments, I’m trying to find a way to get it sorted, but so far I’ve got nothing. 🙁

If that is what you had then it should be fine, and I’m a bit confused as to what could be stopping your function from working correctly. In fact I’m completely confused as to what is producing the ‘0’…

I was thinking of a Hackintosh, in fact I even tried it on my current PC. It did work, but it was never very stable & crashed a lot. So instead I decided I should get a real iMac and start my fund. I am adding money to the fund myself whenever I can, but any help is a great help too. So a huge thank you for the donation you made. 😉

Author’s gravatar

How did you get OSX on your HP Laptop? I have been looking for a way to develop for the iPhone/iPad without buying a mac.

Author’s gravatar author

Not really allowed to say, as it is against the OSX license to install OSX on any computer other than an official Mac.

However if you search around on Google for ‘hackintosh’ you’ll get a lot of results explaining how to do it, I’d just like to err on the side of caution since I’m not sure of the legality of Hackintosh’s in my country.

Take note though that it can be tricky to do & you are breaking the law even if you buy a copy of OSX without the Mac.

Author’s gravatar

That’s exactly what I have in the form. I’ve tried to remove the nonce just for kicks, but we still return 0.

Oh, could .htaccess have anything to do with it? I am running on a subdomain for testing which I have blocked off (well, I think. Google can’t find it but I haven’t verified from outside).

Other than that, I can only see that we should be returning either -1 or 1?

Reply
Author’s gravatar

I’m afraid I have no idea. If you want you could try pasting your entire code into http://pastebin.com and then posting the URL you get. I’ll have another look over it and see if I can find anything.

Author’s gravatar

Front: http://pastebin.com/m721aeec0

Plugin: http://pastebin.com/m7d300bbb

MySQL: http://pastebin.com/m5935e60f

Thanks for spending some time here!

Reply
Author’s gravatar author

I think I’ve found the problem. First you need to make sure you have:

in your plugin file. Without it WP won’t know to call the playlist function when it gets your AJAx call. If you already have that in (as I could’t see it on the pastebins) then I’m stumpped.

Author’s gravatar

Alright, a little progress:

Now, the response is -1. The Bermuda Triangle of Ajax responses!

I’ve tried to change %d to %s just in case. Outcommented the nonce, too.

there was

and

– changed them both to results, but still -1.

This is why I gave up the Rubrick’s Cube 🙂

If you nail this, you will have the end-all front-end ajax-solution too! (and a new imac in no time) 🙂

Reply
Author’s gravatar author

I never could do a Rubrick’s Cube. 😆

Well ‘%d’ & ‘%s’ are just placeholders for digit & string. So you need to change them depending on if the value inserted by the variable is a integer (%d) or a string/numeric value (%s).

Well first you need to figure out if the ‘-1’ is coming from check_ajax_referer() or the else in the code. So easiest way to find out is to change:

and see if you get 0 instead. If you do then the problem is that the MySQL query is not being completed properly. If you still get ‘-1’ then the problem is that you nonce code is not being verified by WordPress properly.

If you are still having problems, I’ll be glad to help more, but the website is going into maintainance as we are updating the site with a new theme. 😉 If you still need help you can contact me via email while the site is offline at pablorobinson [at] gmail [dot] com. We are going offline at 3:00PM – 4:00PM GMT.

Author’s gravatar

wow, new theme! Looking forward to that.

Response is 0 now 🙂

Reply
Author’s gravatar author

You caught me just before I put the site into maintenance. If it’s ‘0’ now it means that the query isn’t working correctly. Try manually writing the query out & putting into PHPmyAdmin’s SQL editor just to make sure there are no mistakes. 😉

Email me if you don’t want to wait until later, when hopefully the new theme will be up. 🙂

Author’s gravatar

Hi again, I like the new furniture.

I found the culprit!

I replaced

for

and it works just perfect.

Thanks for the insights, brother. Now I’m off to your tutorial on deletion but it looks straight forward, now I have some more kung-fu. 🙂

Reply
Author’s gravatar author

Glad you managed to get it working. No problem about the help, that’s what I’m here for. 😉

Thanks. It took a little longer that I’d expected to sort it all out, but I’m very happy with it. 😀

Reply
Author’s gravatar

For me your solution is working if everything is in the same file.In other situations doesn’t work.I try to load with a function from functions.php a script in another file with ajax.The script returns json.

Reply
Author’s gravatar

Also beside the response(which is correct) I get also 0. I don’t need this 0, because I send json, and is breaking my parser.

I resolved my problem, but I use normal jquery ajax (no 0 added), but i have to load wordpress core files to my script, and i still search for a better solution.

Reply
Author’s gravatar author

Hi Clp,

I’ve never tried splitting the code into separate files, but I can’t see a reason for it not working as long as you include your separate PHP files into your functions.php file (if it’s for a theme).

I’m not sure what the 0 is. I have tried a few times to get that returned, but unusually it has never ever happened for me. I’m going to look into it more soon & I might even do a video tutorial.

Author’s gravatar

I tested on Snow Leopard(Firefox), but I will test also on Windows 7 later on my Pc to see about zero.Ye is for a theme. I don’t understand why to include,but I will explain the situation.

I need to split because I don’t want to clutter functions.php

I want to use $WPDB class outside WordPress context in an external Ajax handling script.

For this is needed to load the core Worpdress files to work.The file, let’s say mass_edit.php, it is located in the template directory.It contain the script that interrogate,change,create some customfields in the database.But maybe I can use wp_ajax.

In the wordpress file functions.php exist a function, change_field ,let’s say mass_edit which
create a dynamic form with custom fields. There are 3 types of custom fields.I use a select input to choose.When the select input is modified to another type, this information is served using jquery $.post to the script from mass_edit.php.The mass_edit uses the data, and return an Json array with the new values of custom fields.

Also if one or more custom_fields are modified the function change field send by ajax, as json , data to mass_edit.php which returns also json.

My mass_edit.php doesn’t recognize $wpdb php without loading WordPress core files.
If the code from mass_edit.php exist inside functions.php, no problem, But it is a lot of code and makes functions.php cluttered, and I want to load only in some situations.

The problem is on action in your solution is ajaxurl,admin-ajax.php and I need to go,redirect to mass_edit.php

Reply
Author’s gravatar

“in the wordpress file functions.php exist a function, change_field ,let’s say mass_edit” is “in the wordpress file functions.php exist a function, change_field”

Sorry for the error, but I didn’t see a option to correct the comment after submit.

Author’s gravatar author

Well it’s generally best to avoid loading files outside of WordPress & loading the core separately. The best solution is to write your code into separate files & them include them into functions.php. I only placed all my code in one file because it was easier to explain the tutorial in that manner.

To include your files you would just split up you code into different files & then write the following in functions.php:

There are quite a few theme creators that use that method to make a complex theme with admins, etc. WooThemes is one of them I believe. TEMPATEPATH is a constant defined by WordPress that holds the UNIX/WIN full path to the current theme folder. I assumed that your files to be included were in the current themes folder in a folder called ‘functions’.

I hope that helps you. Oh and unfortunately there isn’t a way to edit your own comments. I need to try and sort something out with that.

Author’s gravatar

I thought about that, but is not wordpress loading functions.php as core at initialization, or perhaps I didn’t understood correctly. It is not overwhelming ?

Reply
Author’s gravatar author

When WordPress initializes the core it starts all of the files needed to make WP work. That includes the theme files & includes running functions.php should it be found inside your theme, any files you have included into functions.php will also be ran as part of the core initialization.

If you are running any functions globalizing $wpdb should allow you access to WP’s database class.

I hope that helps. Let me know if I haven’t understood what you meant properly. Also sorry for any delays in answering, we’ve had problems with our outgoing emails all day so I haven’t been receiving ‘new comment received’ emails.

Author’s gravatar

“When WordPress initializes the core it starts all of the files needed to make WP work. That includes the theme files & includes running functions.php should it be found inside your theme, any files you have included into functions.php will also be ran as part of the core initialization.” This is exact what myself said. Is this not overwhelming ? , in the sense that loading everything will use more resources from the server, also this will not increase the loading of every page,post ?

Reply
Author’s gravatar

using a separate file that is call when you need it, and not all the time, like what I try to do, will not reduce the resources that the server use and will not increase the speed of rendering the pages,posts ?

Author’s gravatar author

Without the core being loaded there is no posts/pages, they exist only because the WordPress core functions create them from the information held in the database combined with the files from your theme.

It can be expensive in a server usage sense, but that is why plugins like WP-Super-Cache exist. Without Super Cache my site would fail quite a lot due to memory usage. Unfortunately that’s just the way WP is.

Using separate files has no benefits other than helping you organize your code into easy to find files. Not increasing server load is down to how you write your code. If you write code that runs all the time (even when it isn’t needed) then it will increase the load on the server. However if you alter the code to only run when it is called, or when it is needed it will only increase load then.

Author’s gravatar

This is pretty close to what i am trying to achieve. Any way you could look at my issues over at wp?

Any help would be much appreciated.

Reply
Author’s gravatar author

Hey there,

I’ve had a peek at your forum post and it’s most likely resolving to a 404 because WP doesn’t resolve file paths normally. Despite the template loading single.php it is actually being loaded from index.php (root of the WordPress blog) so relative paths will not work unless taken from where ever index.php is.

Your best bet is to replace it with an absolute path like /wp-content/themes/themename/single.php. Hope that helps. 🙂

Author’s gravatar

That seemed to be the issue. You are good!
And thanks so much for the VERY fast response.

I noticed that i needed to also define the call as a GET.

Looks like it creates the GET now all i am going to have to do now is get the GET vars and set them to PHP vars.

PS Just subscribed to your RSS, looking forward to other great posts from your team.

Author’s gravatar author

No problem & thank you. Posts have been a little sparse lately due to personal commitments but I’m planning to do some screen cast type tutorials soon. 🙂

By the way if the information you are sending could be sensitive or vulnerable to injection (like variables to be inserted into a database) I would advise sending & receiving the data via POST instead of GET. 😉

Author’s gravatar

Yah i am now using post. I was grabbing some code from another place that was using GET. changed it to POST.

Thanks for pointing that out to me. Always nice to have another set of eyes on something.

Spent like 3 hours messing with that simple ajax call last night… shows how good i am with jquery.

Every day is a school day.

Look forward to the new posts and screen casts

Author’s gravatar author

No worries. 😉

I know enough jQuery to get by but I’m learning all the time too. It’s a never ending cycle. 😆

Should be getting them done soon, just got to get a decent mic. 🙂

Author’s gravatar

Ok got another related issue:

After the ajax post submits it is getting a:

I am submitting the ajax request to the same page.

Any ideas why after the post that the php is not php?

Author’s gravatar

The reason for this is that you are calling single.php which is a template file without any of WordPress loading. That means that any WordPress functions won’t be available, bloginfo included.

Author’s gravatar

So does that mean that i need to have a php file outside of the main wordpress folder?

Author’s gravatar author

Not really. It just means that when you call single.php none of the normal WordPress functions such as bloginfo() will be available since WordPress has not been loaded.

It just means you will have to figure out path’s manually. You could always do it the way this tutorial does by passing your AJAX request through admin-ajax.php but you would have to deal with nonce generation etc. It would however allow you access to all the normal WordPress functions. It’s up to you.

Author’s gravatar

I returned, to clarify the 0, added at the end of the response, is because the wordpress use die(0), after the ajax call, so it is necessary to use die() in the function, before wordpress does.

Reply
Author’s gravatar

Ahhh. I’d completely forgotten about that. I’d just naturally assumed you had a die or exit in your code. Sorry about that.

Author’s gravatar

Great article. Just have a question. How can i use ajax in admin to be use with the

register_setting() method.

I am using register_settings(‘mysetting-group’,’option1′);

The form is like

How can i update these options using using ajax?

Thanks.

Reply
Author’s gravatar

Sorry the forms tag wasnt printed.
My form is like this

i tried to point it to wp-admin/wp-ajax.php , but doesnt works.

Thanks.

Reply
Author’s gravatar author

I’m going to assume you’ve been here:

http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/

Unfortunately I have no idea, from first glance I’d say that because WordPress takes care of everything when using register_setting() it takes away some the customization abilities such as using AJAX.

However as I’m not familiar with using that method to save plugin info I’m afraid I can’t be of much help, sorry. 🙁

Older Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more