Wordpress Ajax

Ajax Wordpress

An article for plugin developers that describes how to add Ajax to a plugin. But there is another process of using Ajax in Wordpress. <font color="#ffff00">Introduction Addressed to plug-in programmers, this paper explains how to append Ajax to a plug-in.

... The following example uses JavaScript to enter our JavaScript in the bottom of the page. When the page is fully load, this will trigger the AJAX request:'action':'action':'my_action', }; alert('Got this from the server:' + response); }); }); }); NOTE: can be used if you want to split your JavaScript source into pure JavaScript file and more.

Next, create a PHP feature to process the AJAX query.

Failure to specify an operation will cause admin-ajax.php to terminate and returns 0. You' ll need to include some detail, such as debugging and whether the query came from the right place (with check_ajax_referer()), but hopefully the above example will be enough to launch you with your own Ajax administrative side plug-in.

Note the use of wp_die() instead of die() or exit(). In most cases you should use wp_die() in your Ajax call-back feature. Same example as the one before, except with the JavaScript on a seperate extern filename we will call js/my_query.js. Samples refer to a plug-in directory.

Aktion':'my_action','whatever': we_value < < // We transfer different pHs! alert ('Got this from the server:' + response); }); }); }); }); For JavaScript outside JavaScript we need wp_enqueue_script() first so they are contained on the page. In addition, we must use wp_localize_script() to transfer value to JavaScript objects property, since it is not possible for PHP to enter direct feedback into our JavaScript for this.

So if you want it to be triggered for both visitor and user logins on the frontend, you can do this: add_action('wp_ajax_my_action','my_action'); add_action('wp_ajax_nopriv_my_action','my_action'); hint 2: Both frontend and backend use admin-ajax queries. php thus is_admin() always returns real in your actions handler file.

If you load your Ajax scripts handler for the frontend and backend selective and use the is_admin() feature, your wp_ajax_(action) and wp_ajax_nopriv_(action) hook MUST be inside the is_admin() ====real part. If an Ajax request is linked to either wp_ajax_ or wp_ajax_nopriv_ action, it will be performed in the WP Admin environment. Check thoroughly the operations you perform in your source file because unauthorized or visitor access can cause increased privilege queries for which they may not be eligible.

add_action ('wp_ajax_my_backend_action','my_backend_action'); Here the Ajax actions set to call the PHP functions my_frontend_action_callback() for all user. Ajax' operation, named ma_backend_action, triggers the PHP callback operation ma_backend_action_callback() for registered user only. Plug-ins and designs that include contents via Ajax must initiate the post-load events on document.body after contents have been included.

Others depending on JavaScript interactions after inserting contents, such as AdToAny or a jQuery Masonry jQuerycript, are waiting for the post-load events to initiate the necessary JavaScript. For example, when the post-load is triggered from Jetpack's Infinite Scroll, the AddToAny shows the release keys for each posting, and jQuery Masonry places items on the page.

The JavaScript triggers the post-load events after the contents have been pasted via Ajax: jQuery( document. body). triggers( 'post-load'); JavaScript listens to the post-load event: When the Ajax query in wp-admin/admin-ajax.php failed, the answer was -1 or 0, dependent on the cause of the problem. Enabling bug_reporting will return them to the issue cache, which will burden your AJAX answer with errors.

For this reason, be careful when debugging Ajax, as any PHP message or message you return may cause the results to be confusingly parsed, or may cause your script to fail.

Mehr zum Thema