How to include php-generated javascript?

You can either not use wp_enqueue_script (which loads a JS file, where you won’t be able to use PHP) and instead simply print the script out on the wp_head or wp_footer hooks…. or you can use wp_localize_script() wp_enqueue_script( ‘some_handle’ ); $instance = // not sure how you are fetching this value $array = array( ‘thumbs’ … Read more

Using admin-ajax prevents regular php form submission

You can change the form action to admin-ajax.php?action=your_action and this will work, but it’s not best practice. The ajax submition should be added via javascript, not by changing the form action (also your js function should prevent the form from submiting normally, returning false – see here an example: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/), and php handling of the … Read more

Localize script not working

You’re calling wp_localize_script before the action that enqueues the script has run. Move the localize into the enqueue action, after the script is enqueued. function my_scripts_method() { wp_enqueue_script( ‘ajax_script’, get_stylesheet_directory_uri() . ‘/js/ajax_script.js’, array( ‘jquery’ ) ); wp_localize_script(‘ajax_script’, ‘WPURLS’, array( ‘siteurl’ => get_option(‘siteurl’) )); } add_action( ‘wp_enqueue_scripts’, ‘my_scripts_method’ ); That said, you probably don’t want to … Read more

WordPress – admin-ajax.php returns 502 Bad Gateway [closed]

I had similar problem on wordpress site. add these lines inside http block of /etc/nginx/nginx.conf file. fastcgi_temp_file_write_size 10m; fastcgi_busy_buffers_size 512k; fastcgi_buffer_size 512k; fastcgi_buffers 16 512k; If it still not working also add this line client_max_body_size 50M;

Error with function in functions.php?

These two lines are wrong: add_action(‘wp_ajax_get_data’, mm_get_all_informations); add_action(‘wp_ajax_nopriv_get_data’, mm_get_all_informations); You need to quote the function name: add_action(‘wp_ajax_get_data’, ‘mm_get_all_informations’); add_action(‘wp_ajax_nopriv_get_data’, ‘mm_get_all_informations’); If you had debugging enabled, as you should when you are working, you would have spotted the problem immediately.

Function won’t run onclick using Ajax

1) Is your function working? First do a console.log( “it’s working” ); inside your function and check it on your browser’s console. If it’s not showing when you click, you must refactor your function. 2) Have you defined ajaxurl? Unlike on the admin side, the ajaxurl javascript global does not get automatically defined for you, … Read more

Creating a new div onclick wordpress+ajax

To add action for WordPress AJAX If you needed to create an AJAX handler for an “render_admin_charts_page” request, you would create a hook like this: add_action( ‘wp_ajax_render_admin_charts_page’, ‘yourfunction’ ); function yourfunction() { ?> <div class=”wrap”> <div class=”em-bookings-events”> <h2><?php esc_html_e(‘Event Booking Report’,’dbem’); ?></h2> <?php em_bookings_events_table(); ?> </div> </div> <?php // Handle request then generate response using … Read more