New answers tagged

1

For the sake of documentation. I spent two days working out an ajax problem and this afternoon when I started testing, my PHP ajax handler wasn't getting called.... Extraordinarily frustrating. The solution to my problem (which might help others) is the priority of the add_action callback: add_action( "wp_ajax_{$my_handler}", array('class_name', '...


-1

I seem to have fixed the issue. It tuns out the inital data wasn't properly formatted.


1

Simply, you can not redirect the user from the sever in a Ajax callback. The redirect in the Ajax callback redirecta the Ajax request itself, it doesn't redirect the user to a new location. I hope you understand what I mean. Remember that Ajax, by definition, is asynchronous. So, you need to think in another workflow. For example (just an example, the ...


0

I had got the ans. First you have to add following code in your function.php to call ajax in your template** add_action( 'wp_ajax_demo-pagination-load-posts', 'cvf_demo_pagination_load_posts' ); add_action( 'wp_ajax_nopriv_demo-pagination-load-posts', 'cvf_demo_pagination_load_posts' ); function cvf_demo_pagination_load_posts() { global $wpdb; /...


0

I found out the issue. It was something like jQuery/AJAX code for it is not being able to hook itself when I reload the form with .load(), therefore the second time I submit a new comment, or when I clicked the reload button for the comment list/form (making the .load() run once), the new form would still submit the comment but since the jQuery/AJAX is not ...


0

I had the same problem, to fix it I used wp_die() at the end of my function just after an echo. Don't forget to pass your action on your script. To be sure, check if your function has to use wp_ajax_nopriv like wp_ajax.


1

See documentation here but don't miss the note 2 and the following points : AJAX on the front-end is more complicated, ajaxurl is not defined so you have to call it with wp_localize_script(). you have to use wp_ajax_nopriv_{action} for non logged in users there could be security issue !!! About the third point : Be careful because you give access to ...


1

A simple answer to your question is that you have the data type as JSON in your ajax request and you are passing a string in it. You need to pass a json object in the ajax request when you chose dataType equals to JSON. A JSON object is typically a key pair value inside curly braces. Here is a reference where you can see how ajax requests can be implemented ...


0

I think you may have a problem in the way you establish your data. If you read the WP Codex on AJAX, you will see the data in the examples is in key/value pairs. That is, the data should look something like this: var data = { 'action' : 'do_ajax', 'fn' : 'change_due_date', 'newDate' : newDate }; Also, note the Codex says that ...


0

Without knowing how you're populating your combo box, here's how I would do it: Use jQuery to capture the form selection: jQuery('#formElementID').change(function () {//this captures dropdown menu change event //call js function and pass form value - in my case a post id fetchTenantWoContactInfo(jQuery('#formElementID').val()); }); Use function to ...


2

Now I got it! WordPress is just awesome. Also Thank you knif3r, your suggestions help me lot. I am trying to explain it in my words. Please correct me, if I am wrong. What is nonce? Nonce is something like security hash to prevent attacks and mistakes. It creates unique identifiers to check if the ajax request is coming from website or not. In WordPress ...


0

For the future: Questions like this should be asked on Stack Overflow. As you expected, AJAX would be a good choice to use. Let's make a new file called send-email.php <?php $to = $_POST['email']; $subject = 'A teacher wants to know your relief work status'; $message = ' <img src="http://xyz.com.au/wp-content/uploads/xyz-logo-800.jpg" ...


0

You don't need to move your wp_localize_script anywhere else. Your problem lies within the fact that you try to use AJAX outside of WordPress. Let me refer to an earlier answer of me showing a simple version of how to use AJAX in WordPress here


0

You can apply your code on certain page by using is_page() function like this if(is_page('type_page_slug_here')){ /* Your script will go here */ } You can also use page ID instead of page slug. Use this code in functions.php.


0

Clouseau are not OOP and you should be able to replace them with any traditional functions in 99% of the cases. As for ajax, your problem is probably in understanding ajax more than with specific mechanics. Ajax is a context free request, the web server has no real reliable way to know from where and under what conditions it was sent, and therefor you have ...


2

You cannot use wp_localize_script in page template. You have to use it in functions.php or your custom plugin, after you register your script, which is usually in the wp_enqueue_scripts action. https://codex.wordpress.org/Function_Reference/wp_localize_script


0

Please follow the code: add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' ); add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' ); function prefix_ajax_add_myfunc() { // Handle request then generate response using WP_Ajax_Response } and in your ajax call do this: jQuery.post( ajaxurl, { 'action': 'add_myfunc',...


0

Please follow the code: add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' ); add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' ); function prefix_ajax_add_myfunc() { // Handle request then generate response using WP_Ajax_Response } and in your ajax call do this: jQuery.post( ajaxurl, { 'action': 'add_myfunc',...


-3

------------------ you can try this way, not 100% correct function.php - add admin ajax url ----------------- function enqueue_script(){ wp_register_script('custom_scripts',get_template_directory_uri() . '/js/scripts.js'); wp_localize_script( 'custom_scripts', 'ajax_url', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); wp_localize_script( '...


1

The following is the solution I came up with: <div id="selection1"> <div id="dropdown1"> <form id="choice1" action="<?php bloginfo('url'); ?>" method="get"> <?php $select = wp_dropdown_pages( array( 'post_type' => 'page', '...


-1

wp_send_json( $result ); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { wp_die(); } UPD The besat way to use ajax requests with wp is to use wp native ajax actions add_action( 'wp_ajax_my_action', 'my_action_callback' ); add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' ); https://codex.wordpress.org/AJAX_in_Plugins so you ...


2

How can i return a PHP value from a WordPress page to a JQuery function? Use wp_localize_script to create an object and pass it to the JavaScript.


0

To verify nonces in Ajax requests, check_ajax_referrer() should be used instead of wp_verify_nonce(): Crete the nonce: $nonce = wp_create_nonce( 'vote-nonce-' . get_the_ID() ); Include it in JavaScript: jQuery(document).ready(function(){ jQuery(".post-voting").click(function(){ vote_nonce = <?php echo $nonce; ?> vote_id = jQuery(this)....


0

If you want to update title and description you should not use wp_update_attachment_metadata attachment title and description is not a meta it's a main post detail (attachment is custom post), you can do this with following code. $attachment = array( 'ID' => $id, 'post_title' => $title, 'post_content' => $description ); // now update main ...



Top 50 recent answers are included