Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 5 author levels in my WordPress site. Levels are calculated using points

 - level 1
 - level 2
 - level 3
 - level 4
 - level 5

Authors getting impressions based on author levels like this.

 - Level l => 50%
 - Level 2 => 55%
 - Level 3 => 60%
 - Level 4 => 65%
 - Level 5 => 70%

As of now I'm using code like this..

$random_number = mt_rand(1,100);
if ($author == 'level1' && $random_number < 50) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level2' && $random_number < 55) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level3' && $random_number < 60) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level4' && $random_number < 65) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level5' && $random_number < 70) {
    $adsenseid = 'author adsense id';
} else {
    $adsenseid = 'admin adsense id';
}

The above code depends on random number. So I think it's not fool proof way.

Can someone help me to do it in a perfect way? Maybe using usermeta table.

share|improve this question
1  
I don't have Adsense knowledge, but just to understand the logic behind this: what's the relation between the percentage and the random number? :: And for clarity, what do you consider fool proof? :: Oh, and Google search doesn't tell me what "sharing modal" is... This Q is already as first result there (impressive, btw). –  brasofilo Dec 24 '12 at 0:22
    
For your first question please check this question. Consider my webpage getting 10 page views. Since i'm using random number, there is a possibility 8 out of 10 views use my aff id instead of user's aff id. So I guess that is not fool proof. –  Giri Dec 24 '12 at 0:33
    
I was working with bootstrap modal today. Little confused. Fixed it –  Giri Dec 24 '12 at 0:36
    
What is $user? Is that the user currently viewing the page? If so, I am confused about the relationship between your different components, and about how you want this revenue sharing to work. Actually, I am confused about those things independently of the question abotu $user. :) –  s_ha_dum Dec 24 '12 at 0:38
    
@s_ha_dum changed $user to $author. I hope it does make sense now. –  Giri Dec 24 '12 at 0:43

2 Answers 2

You question is really "how do I make my way work?" but "what is a good way to do this?" so, maybe I am missing something, but this seems overly complex to me. Assuming that "50%", "55%", etc. means "xx% of the revenue for those particular page loads", why not just track single (I assume) page loads per author and apply the percentage at the end of the day when you cut the checks? All you really need is an author hit counter.

If an author has 50 "hits" and that author is level 1, the author gets paid for 25 impressions. A level 3 author would get paid for 30. What is left over goes to you, or so I assume. That kind-of seems like the breakdown you are trying to get with the random number and the if/elses, but, again, maybe I am just really confused.

That is certainly how I would approach this, unless given reason for something more complicated. Can't help but feel like I am missing something. Are you sending this data to AdSense immediately on page load or something like that?

share|improve this answer
    
Hello mate, I have points system. So I'm using those points to calculate author level. Sorry.. I forgot to mention it.. Thanks for your answer. –  Giri Dec 24 '12 at 1:52
    
Ahh... changing the rules on me... –  s_ha_dum Dec 24 '12 at 5:16
up vote 1 down vote accepted
function get_current_number($author_id) {
    $current_number = (int) get_user_meta( $author_id, 'revshare_current_number', true );
    if( in_array( $current_number, range( 1, 99 ) ) ) {
        $current_number++;
    }
    else {
        $current_number = 1;
    }
    update_user_meta($author_id, 'revshare_current_number', $current_number);
    return $current_number;
}

$current_number = get_current_number($author_id);

if ($author == 'level1' && $current_number < 50) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level2' && $current_number < 55) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level3' && $current_number < 60) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level4' && $current_number < 65) {
    $adsenseid = 'author adsense id';
} elseif ($author == 'level5' && $current_number < 70) {
    $adsenseid = 'author adsense id';
} else {
    $adsenseid = 'admin adsense id';
}
share|improve this answer
    
Are you sure it's written like if ($author = level1 && and not like: if ($author == 'level1' && ? ::: I see you removed the randomness from the equation, seems legit now, especially after reading @s_ha_dum Answer. –  brasofilo Dec 24 '12 at 2:25
    
Actually $author = level1 is not my original code. I used code like that for better understanding. Anyway let me fix that too –  Giri Dec 24 '12 at 2:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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