Ajax for subscibers not working

If an AJAX function does not work properly, in most cases you forgot to allow Users with no or little privileges to access the function. Adding the Callback Function is quite simple: add_action(‘wp_ajax_my_action’, ‘my_action_callback’); Now you need to do the same thing for the “not-so-privileged” users: add_action(‘wp_ajax_nopriv_my_action’, ‘my_action_callback’); With these two lines everything should work … Read more

custom autocomplete search

I found my problem. It was just a problem from my search php script! Without trim( $_REQUEST[‘term’] ) it will not correctly filter results. Now like this it works perfectly: function search_autocomplete(){ $posts = get_posts( array( ‘s’ => trim( $_REQUEST[‘term’] ), ‘post_type’ => array(‘post’, ‘portfolio’) ) ); $suggestions=array(); global $post; $args = array(‘post_type’ => array(‘post’, … Read more

Serve a different theme based on window size

You shouldn’t need to use a different theme. A good responsive theme should handle all sorts of device window sizes. The new default WP theme, Twenty Thirteen, is responsive mobile first and changes for different device screen sizes. http://wordpress.org/themes/twentythirteen

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;

Check ajax triggered from front-end or from dashboard

Ajax request in WordPress is always performed in admin side, so is_admin() returns always true. If you need to know where the ajax request come from you need to check where the script is executed, not where the script send the ajax request. For example: add_action(‘wp_enqueue_scripts’, ‘cyb_enqueue_scripts’); function cyb_enqueue_scripts() { //Register the script. wp_register_script(‘my-script’, get_stylesheet_directory_uri(). … Read more