Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we reset counter ? #26

Open
houpela opened this issue Oct 17, 2023 · 4 comments
Open

Can we reset counter ? #26

houpela opened this issue Oct 17, 2023 · 4 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@houpela
Copy link

houpela commented Oct 17, 2023

Hi there !
Thanks for your work. it's just perfect for my use.
Could you tell me if counter coul'd be reset (All counter or maybe by post)? and how i can reset it ?
Could you add this function to your work ?
Thanks

@houpela
Copy link
Author

houpela commented Oct 19, 2023

Well, i find in database in the postmeta table the metakey 'entry_views' a counter to reset, i reset it from database...

Could you think it's was possible to put a button to set entry_views to '0' from your Option Page ?

i'm lookking for it besause i made qr-code with the url of product-post, and your plugin help me to make inventory...

@houpela
Copy link
Author

houpela commented Oct 19, 2023

ChatGpt help me :

Certainly! You can add a button to reset the counter directly in your plugin's settings page. Here's how you can do it:

Add the following function to your Wp_post_view_settings class to display a reset button on the settings

public static function wppv_reset_counter_button() {
    ?>
    <form method="post" action="">
        <input type="hidden" name="wppv_reset_counter" value="1">
        <?php submit_button('Reset View Counter', 'primary', 'reset_counter_button'); ?>
    </form>
    <?php
}

Next, add a hook to check if the button has been clicked and reset the counter if necessary. Add this part to your wppv_api_settings_init function:

// ...
// Add this line to display the reset button
add_settings_field(
    'wppv_api_reset_counter',
    __( 'Reset View Counter', 'wppv' ),
    array( 'Wp_post_view_settings','wppv_reset_counter_button'),
    'wppvPlugin',
    'wppv_api_wppvPlugin_section'
);

// ...
// Add this condition to check if the button was clicked
if (isset($_POST['reset_counter_button']) && $_POST['wppv_reset_counter'] == 1) {
    // Reset the counter here
    global $wpdb;
    $table_name = $wpdb->prefix . 'postmeta';
    $updated = $wpdb->update($table_name, array('meta_value' => '0'), array('meta_key' => 'entry_views'));
    if ($updated) {
        echo 'The view counter has been reset successfully!';
    } else {
        echo 'An error occurred while resetting the view counter.';
    }
}

With these additions, you'll have a "Reset View Counter" button on your plugin's settings page. When you click this button, the view counter will be reset. Be sure to adjust the code to fit your specific needs. 😊

@houpela houpela closed this as completed Oct 19, 2023
@vanpariyar
Copy link
Owner

vanpariyar commented Oct 23, 2023 via email

@vanpariyar
Copy link
Owner

Added this to working sprint thanks.

@vanpariyar vanpariyar reopened this Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Status: 🔖 Ready
Development

No branches or pull requests

2 participants