Put those add_action
functions in your functions.php
file too. If they’re in header.php
, WordPress never registers them since header isn’t loaded in AJAX. Also, you don’t need that is_admin()
check. The theme’s header will never load in admin. So, your functions file should look like this:
add_action('wp_ajax_my_special_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');
function my_action_callback() {
$whatever = $_POST['whatever'];
$whatever += 10;
echo 'whatever now equals: ' . $whatever;
die();
}
And the beginning of that part of your theme’s header file should look like this:
<?php
$_ajax = admin_url('admin-ajax.php');
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
Other than that, your code looks like it’s good to go!
Related Posts:
- Adding admin-ajax.php to the frontend. Good or bad idea?
- How to Link External jQuery/Javascript files with WordPress
- Jquery in Child Theme
- WordPress AJAX in header.php
- Question about the way that wp_register_script works
- WooThemes PremiumNews Theme jQuery Conflict with WordPress 3.2
- Using JQuery to check for Rel Attribute of Image before Overriding It
- Getting a ‘slide down’ js panel implemented within WP
- Calling external Libraries in WordPress
- Adding the jQuery to my theme that already exists in WordPress?
- Knowing blog name using blog id
- Jquery Theme Roller to WordPress
- jQuery is not defined – some theme features do not work!
- Ajax Call not Working in Plugin
- WordPress ajax insert and delete if already inserted delete the post id stored in custom table
- Adding admin-ajax.php to the frontend. Good or bad idea?
- Theme making direct ajax calls to theme folder
- Workaround AJAX calling without directly calling WordPress backend (Admin dashboard) because it is restricted to the end user (the public)
- How to change background color in the Notepad++ text editor?
- JSON parsing error syntax error unexpected end of input
- Cross-Origin Read Blocking (CORB)
- Cross-Origin Read Blocking (CORB)
- syntax error: unexpected token <
- Pass request headers in a jQuery AJAX GET call
- jQuery Ajax error handling, show custom exception messages
- How to send multiple data fields via Ajax?
- Infinite Scrolling in Asp.Net MVC with jQuery / AJAX Issues
- My javascript is returning this error: $.ajax is not a function
- ajax jquery simple get request
- jQuery get textarea text
- jQuery AJAX cross domain
- Set timeout for ajax (jQuery)
- How to generate a simple popup using jQuery
- What does on() in JavaScript do?
- jQuery AJAX submit form
- jQuery Ajax File Upload
- How can I upload files asynchronously with jQuery?
- Adding images to a Bootstrap Carousel with AJAX
- Jquery Ajax Posting JSON to webservice
- AJAX response error: net::ERR_EMPTY_RESPONSE
- Ajax TypeError: $.POST is not a function
- TypeError: $.ajax(…) is not a function?
- XML Parsing Error: not well-formed in FireFox but good in Chrome
- Access Control Request Headers, is added to header in AJAX request with jQuery
- Ajax Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
- How to fix the ‘Missing required request header. Must specify one of: origin,x-requested-with’ Ajax Error
- What’s the point of the X-Requested-With header?
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- jQuery Ajax POST example with PHP
- Refresh/reload the content in Div using jquery/ajax
- How to get a cross-origin resource sharing (CORS) post request working
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- jQuery Form Validation before Ajax submit
- jQuery – Illegal invocation
- How to manage a redirect request after a jQuery Ajax call
- jQuery: Performing synchronous AJAX requests
- How to change or add theme to Android Studio?
- Call PHP function from jQuery?
- FormData append not working
- Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- Download a file by jQuery.Ajax
- jQuery’s .on() method combined with the submit event
- JQuery: ‘Uncaught TypeError: Illegal invocation’ at ajax request – several elements
- jquery ajax function not working
- Ajax success function
- jQuery ajax error function
- “The Events Calendar” plugin by Modern Tribe in WordPress not working
- Jquery Slider for profile template
- WordPress frameworks and parent themes [closed]
- Opinions and recommendations on the best barebones base theme [closed]
- Remove JSON API links in header html
- Using OOP in themes
- ajaxurl not defined on front end
- Where can I download WordPress themes from? [closed]
- How can I allow the Editor Role to change Theme Settings?
- What are all the available parameters for query_posts?
- Customizing a WordPress theme without changing it?
- Where can I learn to create my own theme?
- How do I turn off self-closing tags for markup in WordPress (for HTML5, or HTML4, for example)?
- how to add version of style.css in wordpress
- Does WordPress work without a theme?
- What is the role and history of the $content_width global variable?
- How to move the sidebar in TwentyFifteen to the right?
- Installation failed: Download failed. No working transports found
- What is theme-compat?
- Override parent theme translation on child theme
- How to manage ajax calls and JSON in wordpress
- WordPress Theme activation hook?
- Can wordpress theme folder name be changed freely and nothing technically happens
- Link to specific Customizer section
- What does exactly GPL license mean for my WordPress theme?
- Am I allowed to license my WordPress theme under the aGPL
- How do I change the header image height in Twenty Seventeen?
- Starter Theme vs Parent Theme? Pros and cons
- How to change the language for the front-end only?
- W3 Total Cache, CDN and theme files [closed]
- Best way to include Bootstrap in WordPress
- ajax stopped working when not logged in?
- Using jQuery to delete data stored in wp_options