0

I would appreciate some help. I am trying to create a save bookmark plugin through wordpress functions file (all code in one place). The problem is I am unable get the ajax, jquery to work I am not sure what part of it is not working. You click on link and it'll call ajax and then php where it'll save the post_id, user_id into the database.

Here's the code (funtions.php):

echo '<a href="#bookmark" class="bookmark">Bookmark</a>';

add_action('admin_head', 'bookmark_add');

function bookmark_add() { ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
    $(document).on('click', '.bookmark', function(){
    console.log('bookmark_save_TEST');
    var data = {
        'action': 'bookmark_save',
        'user_id': 1234,
        'post_id': 3049291
    };
    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(bookmark_save.ajax_url, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});
</script> <?php
}

add_action( 'wp_ajax_bookmark_save', 'bookmark_save_callback' );

 function bookmark_save_callback() {
 global $wpdb;
 $user_id = intval( $_POST['user_id'] );
 $post_id = intval( $_POST['post_id'] );

 $post_id += 10;

 echo $post_id;
 
// once the code above works, will uncomment this to add into the database.
 //$sql = $wpdb->query("INSERT INTO {$wpdb->prefix}wp_bookmarks(`user_id`,`post_id`) VALUES (%s, %d)", $user_id, $post_id);

exit();
}

Can someone please tell me what is it that keeps going wrong?

3
  • Check you have any errors in browser console. Commented Feb 12, 2021 at 17:14
  • you also have to provide the ajax_url Commented Feb 12, 2021 at 17:18
  • @vanurag I am not getting any errors at all in browser console at all which is why its making it more difficult. How can I provide ajax_url? Also, if I use ajaxurl, is that better? All the code is in one file. Commented Feb 12, 2021 at 17:28

2 Answers 2

1

if you are using in admin area then use "add_action('admin_footer', 'bookmark_add');" for frontend "add_action('wp_footer', 'bookmark_add'); "

also check your action.

'action': 'bookmark_save_callback',
add_action( 'wp_ajax_bookmark_save_callback', 'bookmark_save_callback' );
add_action( 'wp_ajax_nopriv_bookmark_save_callback', 'bookmark_save_callback' );

you can check for more info https://codex.wordpress.org/AJAX_in_Plugins

Sign up to request clarification or add additional context in comments.

Comments

0

You have missed closing brackets and ajaxurl

});
</script>

 var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
jQuery.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
    });

also use "wp_ajax_nopriv_"

add_action( 'wp_ajax_nopriv_bookmark_save', 'bookmark_save' );

1 Comment

Thank you for pointing it out. I have made the changes and tried again and still nothing. No outcome or error or console, nothing at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.