0

i am toying with myCred and would like to reward logged in users for sharing content bringing in visitors. myCred offers affiliate linking by user-id, so I figure, I "only" need to achieve appending the currently logged-in user's user ID to whatever permalink wordpress renders (be they posts, pages, listings, comments, buddypress actions, etc. - wherever a permalink is issued by my site), like so

?mref=Current_User_ID

I've spent the last hours sifting thru many similar Q&As here + trying to adapt what I found to my scenario; alas, I'm rather unskilled at any real programming and hence had no success so far.

I figured out how to check if myCred is active, [if( class_exists( 'myCRED_Core' ) )], how to limit execution to logged in users, how to get the current user's ID [$current_user_id = get_current_user_id();] and that I'll probably have to do something like

$affiliate_url = add_query_arg('mref', $current_user_id, get_permalink());

but I don't know how to make it apply for all links rendered for my site.

Is there a way? Thank you for what help you can provide. Cheers - LX

0

You can use the_permalink filter.

function wpse_289182_permalink($url) {
    if(is_user_logged_in())
    {
        return add_query_arg('mref', get_current_user_id(), get_permalink());
    }
    return $url;
}
add_filter('the_permalink', 'wpse_289182_permalink');
1
  • Hello, Jean-Rémi; this already works like a charm, thank you very much. is there a way to extend the scope of this to also encompass permalinks of comments? I have checked and so far it looks like permalinks of comments don't have the mref appended. Is that possible as well? Cheers :) – eLeXeM Dec 26 '17 at 8:37

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.