php - button for resetting all the custom meta fields

admin2025-01-07  3

I’d like to have a button to reset all the custom fields for the prices, the button should operate from the author (agency) edit page because I’d like to clean the prices of all the apartments (posts) of the same author (agency) in “one shot”. I already have this button This is the button in the author edit page:

<button type="button" ID="target2" data-author-id="<?php echo get_current_user_id(); ?>">Cancella prezzi  2
</button>

this is the code I did in functions.php

add_action('admin_enqueue_scripts', 'reset_price_fields');
function reset_price_fields( $hook ) {
if ( 'user-edit.php' != $hook ) {
    return;
}
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/cancella_prezzi_2.js/', array('jquery'), '1.0' );

$title_nonce = wp_create_nonce( 'title_example' );
wp_localize_script(
'ajax-script',
'my_ajax_obj',
array(
    'ajax_url' => admin_url( 'admin-ajax.php' ),
    'nonce'    => $title_nonce,
)
);
}

and this one:

add_action('wp_ajax_reset_price_fields', 'reset_price_fields2');


function reset_price_fields2() {
check_ajax_referer( 'title_example' );

// Check for the required parameter
if (!isset($_POST['author_id'])) {
    wp_send_json_error(['message' => 'No author ID provided.']);
    exit;
}

$author_id = intval($_POST['author_id']);
$args = [
    'post_type' => 'post', // Change this to your custom post type
    'author' => $author_id,
    'posts_per_page' => -1 // Get all posts
];

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();

        // Get current custom fields
        $custom_fields = get_post_meta($post_id);

        // Prepare an array of custom fields to reset
        $fields_to_reset = ['1_per_hol', '2_periodo']; // Replace with your custom field keys

        foreach ($fields_to_reset as $field) {
            if (array_key_exists($field, $custom_fields)) {
                // Delete the specific custom field
                delete_post_meta($post_id, $field);
            }
        }
    }
    wp_send_json_success(['message' => 'Prices have been reset successfully.']);
} else {
    wp_send_json_error(['message' => 'No posts found for this author.']);
}

wp_reset_postdata();
exit;
}

this is my js file code placed in my theme js folder (named: cancella_prezzi_2):

jQuery(document).ready(function($) {
$('#target2').on('click', function() {
    if (confirm("Do you really want to delete prices?")) {
        $.ajax({
            url: my_ajax_obj.ajax_url, // WordPress AJAX URL
            type: 'POST',
dataType : 'json',
            data: {
                action: 'reset_price_fields', // Custom action to be processed in PHP
                author_id: $(this).data('author-id') // Pass the author ID
            },
            success: function(response) {
                alert(response.message); // Show success message
            },
            error: function() {
                alert('Error resetting prices.'); // Show error message
            }
        });
    }
});
});

add_action( 'admin_enqueue_scripts', 'reset_price_fields' );

But nothing happens when I click on the button...:(

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263033a859.html

最新回复(0)