Title: WordPress: using get_option inside ajax function
Tags: javascript;ajax;wordpress;plugins
Question: I have created a simple plugin for personal use which I’m trying to extend with an AJAX function. I have added the following:
function cancel_notification() {
wp_die(get_option('some_name'));
add_action('wp_ajax_cancel_notification', 'cancel_notification');
add_action('wp_ajax_nopriv_cancel_notification', 'cancel_notification');
Inside the function I want to use the function “get_option()” to use settings set using the plugin, but all values returned using the function are empty. The settings are set, I’m using them in other places in the plugin.
This is my basic setup:
function cancel_notification() {
wp_die(get_option('some_name'));
add_action('wp_ajax_cancel_notification', 'cancel_notification');
add_action('wp_ajax_nopriv_cancel_notification', 'cancel_notification');
if(is_admin()) {
//Call the html code
add_action('admin_menu', 'foobar_admin_menu');
function foobar_admin_menu() {
add_menu_page();
function foobar_html_page() {
//here a form is shown and post actions are done to save data
Comment: When I use wp_die(get_option('some_name')); inside the AJAX function, it doesn't show anything, that is basically my problem.

Comment: @KulikovSergey the ajax call is performed from within foobar_html_page() via a jQuery AJAX click function. I have tried adding "wp_ajax_nopriv_" but it shows the same result.

Comment: @Opolo Webdesign , your piece of code should works correctly. The problem is not in **cancel_notification** function or in ajax call. But make sure that you're performing ajax call, when you have administrator preveligies. Otherwise you should define your ajax call in such way: `add_action('wp_ajax_cancel_notification', 'cancel_notification'); add_action('wp_ajax_nopriv_cancel_notification', 'cancel_notification');`

Comment: Question is not clear. I have done this many times and it works.


Here is another answer: Found the answer! Inside "foobar_html_page()" I was using '/wp-admin/admin-ajax.php' as AJAX url, this should be ajaxurl.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:05/05/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><ajax><wordpress><plugins>', 'Title': 'WordPress: using get_option inside ajax function', 'ViewCount': 508, 'id': 43798392, 'user_AboutMe': '', 'user_AccountId': 448269.0, 'user_DisplayName': 'vespino', 'user_Reputation': 1455.0, 'user_Views': 159.0, 'user_id': 842381.0}}