jQuery Ajax PHP function call returning [object Object]

OK, so there are multiple problems with your code… 1. Incorrect localization of script Here’s your function that enqueues and localizes the scripts: function my_theme_enqueue_scripts() { wp_enqueue_script(‘jquery-ui’, ‘https://code.jquery.com/ui/1.12.1/jquery-ui.min.js’, array(‘jquery’), ‘1.12.1’); wp_register_script( ‘myjs’, get_stylesheet_directory_uri() . ‘/my.js’, array(‘jquery’),’1.0.0′); wp_enqueue_script(‘myjs’); wp_localize_script( ‘ajax-script’, ‘admin-ajax’, array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ) ) ); } add_action( ‘wp_enqueue_scripts’, ‘my_theme_enqueue_scripts’ ); So you … Read more

My jQuery Ajax form submit is still refreshing page?

You use wrong js event. You register your callback with this code: $(‘#form-pm’).on(‘send’, function(e) { But there is no event called send on form. It should be submit. Here’s fixed version of that line: $(‘#form-pm’).on(‘submit’, function(e) {

How to jQuery Ajax show new data from successful insert?

Modify your code as follows: add_action(‘wp_ajax_send_projectmessage’, ‘send_projectmessage’); function send_projectmessage() { global $wpdb; $wpdb->insert( //insert stuff to db table)); // Call your function to retrieve your data and send it to browser $messages = load_projectmessages($projectid); wp_send_json_success($messages); } Display data through JS by modifying this ajaxRequest.done(function(data) { console.log(data); }); to this ajaxRequest.done(function(data) { // Put markup for … Read more

When echo follow Ajax button inside Ajax pagination template not working?

I think, if you change your jQuery click action selector to handle event delegation the follow buttons should work after being added by ajax. // Change this $( ‘.follow-links a’ ).on(‘click’, function(e) { // to this $( ‘.some-parent-not-added-by-ajax’ ).on(‘click’, ‘.follow-links a’, function(e) { // or as last resort $( document ).on(‘click’, ‘.follow-links a’, function(e) {

Getting a GET error in console – 404 not found for wp-json

The URL you’re using is not the URL that you’ve registered. This code: register_rest_route( ‘news’, ‘/news’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘custom_api_get_news_callback’ )); Registers this URL: http://localhost:8888/una/wp-json/news/news But you’re sending requests to http://localhost:8888/una/wp-json/news/all-posts Nowhere in your code have you defined an all-posts endpoint. You either need to change /news in register_rest_route() to /all-posts, or … Read more

Preprocess submitted data

If I got your question right then maybe you could try using the update_option action, which according to the docs “Fires immediately before an option value is updated.”. It gets option, old value and new value as parameters. https://developer.wordpress.org/reference/hooks/update_option/ The accepted answer here, Hook if somebody saves plugin options?, might also be relevant for you. … Read more

Getting the post terms ‘wp_get_post_terms’ per post when within the functions.php file

(Revised answer) So I’m assuming that there are actually posts in the category (ID: 31), and based on the wp_get_post_terms() reference: $post_id (int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0. It’s likely that you’re not passing the proper post ID to the wp_get_post_terms() function: wp_get_post_terms( $post->ID, … Read more

No errors in the console but Ajax call doesn’t seem to be working

I cannot test everything since there are some dependencies on certain markup and functionality, but here’s what I found so far. Wrap your script in the following instead of $ = jQuery;. This is a better way to handle possible conflicts. (function($){ // your code here })(jQuery); While the way you’re enqueuing scripts works, it … Read more