0

I am new with wordpress and trying to create button counter also saving click into database. This form is only for wordpress login users.

Function.php

function getcount($userID){         
    $click_key = 'click_count';
    $count = get_user_meta($userID, $click_key, true);
    if($count==''){
        delete_user_meta($userID, $click_key);
        add_user_meta($userID, $click_key, '0');
        return "0 click";
    }
    return $count.'click';
}
function setcount($userID){
    $click_key = 'click_count';
    $count = get_user_meta($userID, $click_key, true);
    $count++;
    update_user_meta($userID, $click_key, $count);
}

HTML:

<?php
if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if( isset($_POST['clicks']) ) { 
        setcount($userID);
    }
?>

<form action="<?php echo get_bloginfo('home')?>" method="post">
    <input type="submit" value="click me!" name="clicks">
</form>
    <p>Click Count: <?php echo get_user_meta('click_count', $current_user, true); ?></p>
<?php } ?>

How to count button clicks for each users?

  • Can you ellaborate a little further, you've stated what you want it to do, but you haven't described what it's currently doing. What's wrong with the code you posted? Additionally, when you say PHP or HTML, which files are these located in? Is this a theme or a plugin? – Tom J Nowell Dec 22 '18 at 21:16
  • @TomJNowell Both file are in theme, First part in function.php and second in single.php. For logged in users i want to track how many times they click on this button. Actually this is a project for college students. – F.A Dec 22 '18 at 21:31
  • @TomJNowell Simple tracking and record how many times any a click this button. – F.A Dec 22 '18 at 21:33
  • Yes but what does it currently do? If it's broken, how is it broken? Are there error messages? Warnings? Does it work but the counter never increases? WSOD? – Tom J Nowell Dec 22 '18 at 22:12
  • @TomJNowell Code not submit value to database, only From action done after submit – F.A Dec 22 '18 at 22:15
0

The First Problem

The cause of this is not a WP problem, but a fundamental misunderstanding of how HTML forms work.

To be specific, the problem is here:

if( isset($_POST['clicks']) ) { 

There is no input named clicks, so this value is never set ( because you never set it ).

Lets look at your form:

<form action="<?php echo get_bloginfo('home')?>" method="post">
    <input type="submit" value="click me!" name="clicks">
</form>
    <p>Click Count: <?php echo get_user_meta('click_count', $current_user, true); ?></p>
<?php } ?>

Here, it submits a form to the homepage using POST, but it never sends any information. The only input tag is the submission button. A hidden input is needed with the name of clicks

The Second Problem

$current_user = wp_get_current_user();
if( isset($_POST['clicks']) ) { 
    setcount($userID);

Here, the code fetches the current user, but then passes $userID to setcount. $userID is not defined anywhere though, it has no value, so PHP defaults it to null or an empty value. This is why even if you submit the form succesfully, it fails to update the database

A Final Note

If you double click the submit button, the code will enter a race condition as multiple requests try to increment the value. Depending on how fast the server is, the server load, and how quickly the user double clicks, the count will increment, or it will increment by 2 instead of 1.

So disable the submit button after it's clicked via javascript

  • Not work for me when I add <input type="hidden" value="" name="clicks"> also I try <input type="hidden" value="<?php echo get_user_meta('click_count', $current_user, true); ?>" name="clicks"> – F.A Dec 24 '18 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.