{"id":2295,"date":"2019-10-30T14:16:31","date_gmt":"2019-10-30T14:16:31","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2295"},"modified":"2025-08-10T14:32:21","modified_gmt":"2025-08-10T13:32:21","slug":"ajax-background-post","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/ajax-wordpress\/ajax-background-post","title":{"rendered":"AJAX Background Post"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Ensure jquery is activated<\/h4>\n\n\n\n<p>In your function that hooks \u201cwp_enqueue_scripts\u201d, add this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  wp_enqueue_script('jquery');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Simple Working Example<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">In functions.php<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action ( 'wp_ajax_my_ajax_action_callback', 'my_ajax_action_callback' );        \/\/For AJAX calls made for logged in users\nadd_action ( 'wp_ajax_nopriv_my_ajax_action_callback', 'my_ajax_action_callback' ); \/\/For AJAX calls made from non logged in users (include both if both logged in and not logged in users will trigger this)\nfunction my_ajax_action_callback()\n{\n  \/\/Check the nonce\n  check_ajax_referer('my-nonce-special-string', 'security');      \/\/This will die(); if the nonce check fails\n  \n  \/\/if (!is_user_logged_in())\n  \/\/  die();\n\n  \/\/$user_id = get_current_user_id();                             \/\/Use this if your function wants the wordpress user_id (secure method, don't pass it as an ajax argument)\n\n  if ($my_value_1 == 9876)\n  {\n    \n    \/\/Do something\n    \n  }\n  \n    wp_die();     \/\/Terminate immediately and return a proper response\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">In your php (html) page<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $AjaxUrlHtml = \"var ajaxurl = '\" . admin_url('admin-ajax.php') . \"';\";\n  $AjaxNonce = wp_create_nonce( 'my-nonce-special-string' );\n  \n  $HtmlOutput .= &lt;&lt;&lt;_END\n  \n  &lt;div onclick=\"DoAjaxPost();\" &gt;&lt;\/div&gt;\n  \n  &lt;script type=\"text\/javascript\" &gt;\n    $AjaxUrlHtml\n\n    function DoAjaxPost() {\n        var post_data = {\n                   'action': 'my_ajax_action_callback',   \/\/The name of the ajax callback action in functions.php\n                   'security': '$AjaxNonce',\n                   'my_value_1': 9876\n        };\n\n        jQuery.post(ajaxurl, post_data);\n    }\n  &lt;\/script&gt;\n\n_END;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ensure jquery is activated In your function that hooks \u201cwp_enqueue_scripts\u201d, add this: Simple Working Example In functions.php In your php (html) page<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[],"class_list":["post-2295","post","type-post","status-publish","format-standard","hentry","category-ajax-wordpress"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2295"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2295\/revisions"}],"predecessor-version":[{"id":5101,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2295\/revisions\/5101"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}