Wordpress Plugin Favorites, having issue with generating "unfavorite button"

103 Views Asked by At

I am trying to create new function within the existing plugin called Favorites in Wordpress.

The Favorites plugin is great, except when clearing user favorite list, it will clear the whole Favorite list instead of individual items. Thus, I am trying to create an extend of the function within the Favorite plugin, to remove an single item from the list.

https://favoriteposts.com/

According to the website Demo, it allows to remove an Single itme by clicking unfavorite to remove. I am trying to recreate something similar.

I clicked the button but nothing happened. Checked the console log it showed "POST https://craycrave.com/wp-admin/admin-ajax.php 403"

So I have tried everything to resolve this issue. It was a 404 problem, now it became 403, and it seems it's a security issue when accessing ajax.

Please help me to figure out what's going on with this code...

This is the code within my function.php

function enqueue_unfavorite_script() {
    // Enqueue the unfavorite script
    wp_enqueue_script( 'unfavorite-script', get_stylesheet_directory_uri() . '/js/unfavorite-script.js', array( 'jquery' ), '1.0', true );

    // Localize the script and pass the nonce value
    wp_localize_script( 'unfavorite-script', 'unfavoriteData', array(
        'nonce' => wp_create_nonce( 'remove_from_favorites' ),
    ) );
}
add_action( 'wp_enqueue_scripts', 'enqueue_unfavorite_script' );




// AJAX handler to remove item from favorites list
add_action('wp_ajax_remove_from_favorites', 'remove_from_favorites_ajax_handler');
add_action('wp_ajax_nopriv_remove_from_favorites', 'remove_from_favorites_ajax_handler');
function remove_from_favorites_ajax_handler()
{
    // Verify the AJAX request
    check_ajax_referer('remove_from_favorites', 'security');

      // Add a debugging statement
    error_log('AJAX handler called');
    
    // Get the post ID from the AJAX request
    $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;

    // Add your logic here to remove the item from the favorites list
    // For example, you can use a function provided by the Favorites plugin to remove the item:
    if (function_exists('favorites_remove_favorite')) {
        favorites_remove_favorite($post_id);
    }

    // Return a response
    wp_send_json_success();
}

This is the code for unfavorite-script.js


jQuery(document).ready(function($) {
    // Handle the unfavorite button click
    $(document).on('click', '.unfavorite-button', function(e) {
        e.preventDefault();

        var postID = $(this).data('post-id');
        var button = $(this);

        console.log(ajaxurl); // Log the value of ajaxurl

        // Make an AJAX request to remove the post from favorites
        $.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                action: 'remove_from_favorites',
                post_id: postID,
                security: unfavoriteData.nonce  // Pass the security nonce
            },
            success: function(response) {
                // Handle the success response
                if (response.success) {
                    // Update the button HTML and visual state
                    button.removeClass('unfavorite-button').addClass('favorite-button');
                    button.find('.text').text('Favorite');
                    button.find('i').removeClass('icon-star-full').addClass('icon-star-empty');
                }
            }
        });
    });
});

Is there something I didn't do?

0

There are 0 best solutions below