Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Author Tracking Code WordPress

  • SOLVED

I need help with coding this piece. Basically i want with someone click on my author page. It stored a cookie of that author name and email. I want the cookie to stay in place untill the person close their browser, or however long. not a big deal. I should then be able to use this info in any gravityform on my site. Basically i am tracking leads generated by my authors. If you have a better way of doing this, i am happy for suggestions. Thanks . Heres is my current author.php code to get the author info

<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;
?>

Answers (3)

2010-11-09

enodekciw answers:

<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;

setcookie('cookiename', $curauth, 0);
?>


then you can get that cookie value, using
<?php $value = $_COOKIE['cookiename']; ?>


A-Tron Thomas comments:

Hmm i try something like that, but this definitly what i needed. But I get this error message! Any ideas?


Warning: setcookie() expects parameter 2 to be string, object given in /home/content/p/a/r/XXXX/html/wp-content/themes/SRL/author.php on line 23


enodekciw comments:


<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;

setcookie('cookiename', $curauth->display_name, 0);
?>


this will set cookie with authors display name. you can also use
$curauth->aim;
$curauth->description;
$curauth->display_name;
$curauth->first_name;
$curauth->ID;
$curauth->jabber;
$curauth->last_name;
$curauth->nickname;
$curauth->user_email;
$curauth->user_login;
$curauth->user_nicename;
$curauth->user_registered;
$curauth->user_url;
$curauth->yim;

for the second parameter if you don't want to set cookie with display_name ;)


A-Tron Thomas comments:

getting


Warning: Cannot modify header information - headers already sent by (output started at /home/content/p/a/r/paracucina/html/wp-content/themes/SRL/header.php:23) in /home/content/p/a/r/paracucina/html/wp-content/themes/SRL/author.php on line 25


enodekciw comments:

try to paste that piece of code just before your <?php get_header(); ?> function call.

it should look like this:


<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;

setcookie('cookiename', $curauth->display_name, 0);
?>
<?php get_header(); ?>


A-Tron Thomas comments:

you code is the one i end of using and you was first. Make you the fair winner. Thanks this have really just ended my 1 month project on this client site. This was the missing piece to 1000s lines of code i wrote of this amazing site. I would love to get your email, for when i get stuck on other small lines of code. Is this possible?


enodekciw comments:

I'm glad it works!
Sent you my email contacts in private ;)

2010-11-09

Neeraj Kumar answers:

You can not pass an array or an object to setcookie function in php.

The second parameter will only accept "strings" as parameter. Try converting value into string and then storing it into a cookie.

If you have an array then you can do following:

$converted_to_string = json_encode($array_name);

then:

setcookie('cookiename', $converted_to_string, 0);

for retrieving do:

$value = json_decode($_COOKIE['cookiename']);

If you have an object you can convert it to an array by typecasting it using '(array)' like:

$array = (array)$object;

Then convert it to json string by json_encode function


A-Tron Thomas comments:

getting
Warning: Cannot modify header information - headers already sent by (output started at /home/content/p/a/r/paracucina/html/wp-content/themes/SRL/header.php:23) in /home/content/p/a/r/paracucina/html/wp-content/themes/SRL/author.php on line 25


Neeraj Kumar comments:

This error occurs if you are sending some html content to browser before using functions like 'start_session', 'set_cookie' etc.

Solution: Use these functions before sending anything to browser i.e. before the starting of <html> tag.

If you could display the code of header.php then I'll be able to tell to exactly what is causing this error.

2010-11-11

Merne Asplund answers:

Doesn't Google Analytics handle this well? I'm pretty sure you can get a click amount of anything you want with analytics.

http://wordpress.org/extend/plugins/google-analytics-for-wordpress/

Or am I not understanding what type of data you're trying to collect?