Skip Navigation

[Resolved] Page views counter with User custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a custom Page View counting system that increments a custom field in a User's profile when someone visits their content.

Solution: I can show you how to save some value in a User profile custom field using PHP, and how to retrieve that value from a User profile custom field using PHP.
Your code:

$meta = get_user_meta( $post->ID, 'wcpf-count', TRUE );

Is $post->ID accurate here? I think not. You need an author's User ID, not a post ID.

Example:

$meta = get_user_meta( 12345, 'wcpf-count', TRUE );

The variable $meta will contain the value of the wpcf-count field in the profile of User ID 12345. Replace 12345 with the correct User ID, or a variable representing the correct User ID. Try hard-coding a value first, then replace that with a variable and test again.

Your code:

update_user_meta( $authordata->ID, wcpf-count', implode(',', $meta) );
You have a syntax error here. The second parameter, wpcf-count, is not correctly surrounded by quotes.

Example:
[php]
update_user_meta( 12345, 'wcpf-count', 'some value' );

Replace 12345 with the correct author's ID, and replace 'some-value' with the correct value to store in the custom field. Try hard-coding the values first, then once things work as expected replace the hard-coded values with variables.

How you trigger this code is up to you. That part of the application falls outside the scope of support we provide here in the forums.

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_user_meta
https://codex.wordpress.org/Function_Reference/update_user_meta
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#618147

Hi

I have about 300 Authors on the site now.
However I am trying to make a script where the Authors can see how many times their profile has been viewed.

Now, obviously because author.php isnt a page or post with a meta, I figured the view count would need to be saved in the users_meta instead.

So I created a field called wcpf-count for the users.

How would I go about this?

I thought about something like this

add_action('wp', function() {

if( is_author() ) {

global $authordata;

$user_ip = $_SERVER['REMOTE_ADDR'];
$meta = get_user_meta( $post->ID, 'wcpf-count', TRUE );
$meta = '' !== $meta ? explode( ',', $meta ) : array();
$meta = array_filter( array_unique( $meta ) );

if( ! in_array( $user_ip, $meta ) ) {

array_push( $meta, $user_ip );
update_user_meta( $authordata->ID, wcpf-count', implode(',', $meta) );

}

}

});

Can you advise please

#618407

Hi, I can show you how to save some value in a User profile custom field using PHP, and how to retrieve that value from a User profile custom field using PHP.
Your code:

$meta = get_user_meta( $post->ID, 'wcpf-count', TRUE );

Is $post->ID accurate here? I think not. You need an author's User ID, not a post ID.

Example:

$meta = get_user_meta( 12345, 'wcpf-count', TRUE );

The variable $meta will contain the value of the wpcf-count field in the profile of User ID 12345. Replace 12345 with the correct User ID, or a variable representing the correct User ID. Try hard-coding a value first, then replace that with a variable and test again.

Your code:

update_user_meta( $authordata->ID, wcpf-count', implode(',', $meta) );

You have a syntax error here. The second parameter, wpcf-count, is not correctly surrounded by quotes.

Example:

update_user_meta( 12345, 'wcpf-count', 'some value' );

Replace 12345 with the correct author's ID, and replace 'some-value' with the correct value to store in the custom field. Try hard-coding the values first, then once things work as expected replace the hard-coded values with variables.

#618495

Can I put this in my Author.php?

Im already called $curauth->ID at the very top of the page using

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_billing_company) : get_userdata(intval($author));

Here is what I now have

add_action('wp', function() {

if( is_author() ) {

global $authordata;

$user_ip = $_SERVER['REMOTE_ADDR'];
$meta = get_user_meta( $curauth->ID, 'wcpf-count', TRUE );
$meta = '' !== $meta ? explode( ',', $meta ) : array();
$meta = array_filter( array_unique( $meta ) );

if( ! in_array( $user_ip, $meta ) ) {

array_push( $meta, $user_ip );
update_user_meta( $curauth->ID, 'wcpf-count', '1' );

}

}

});

I have added it to my author.php But the script does not do anything

#618760

Can I put this in my Author.php?
Again, I can tell you how to use the methods get_user_meta and update_user_meta to modify User custom fields.