0

Please bear with me as I am a newbie. Is there a way to append a USER ID aka a logged-in user's ID to any external link. For example, a logged-in user clicks a link, the link then appends that user's ID. I've tried WP current user, but have had issues getting that USER ID to get on my report.

  • We don't see any little piece of code you've used to append the user ID. We can't figure out the problem with the code since all our mediums are on holidays now. – Max Yudin Jan 9 at 17:23
  • Welcome to WPSE! Can you add some context? It would be good to know the problem this solves, are you using this for analytics purposes? – Tom J Nowell Jan 9 at 17:26
  • So i added the link manually on a wordpress page like this <a href="welcomehome.com&aff_sub=?user_id=<?php echo get_current_user_id(); ?"> ... the result ended on the reporting side: "?php echo get_current_user_id(); ?" so at least it read the brackets but it's intended use was not there. (a test user ID) – Todd L Jan 9 at 18:06
0

You should be able to use get_current_user_id(). Note that it will return '0' if the user isn't logged in.

<a href="https://example.com/?user_id=<?php echo get_current_user_id(); ?>">Link</a>


Update 2: Got bored, made a plugin: https://github.com/AndyMardell/append-user-id/releases/tag/v1.0.0-alpha

Download the zip and install :) Submitted to WordPress but it's still under review.

Installation and Usage: https://github.com/AndyMardell/append-user-id#installation


Update: you won't be able to do this without PHP. But hear me out...

1) Add the following PHP to your theme's functions.php file:

function prefix_get_current_user_id( $atts, $content, $tag ) {
    $atts        = array_change_key_case( (array) $atts, CASE_LOWER );
    $prefix_atts = shortcode_atts(
        array( 'url' => '' ),
        $atts,
        $tag
    );

    if ( is_user_logged_in() ) {
        $a_open = sprintf(
            '<a href="%s?user_id=%s">',
            esc_url( $prefix_atts['url'] ),
            get_current_user_id()
        );
        return $a_open . esc_html( $content ) . '</a>';
    }

    $a_open = sprintf(
        '<a href="%s">',
        esc_url( $prefix_atts['url'] )
    );

    return $a_open . esc_html( $content ) . '</a>';
}

function prefix_add_shortcodes() {
    add_shortcode( 'link_with_id', 'prefix_get_current_user_id' );
}
add_action( 'init', 'prefix_add_shortcodes' );

2) Edit your post/page content and add the following shortcode:

[link_with_id url="https://domain.com/link"]A link to a page[/link_with_id]

This will output the following html:

<a href="https://domain.com/link?user_id=1">A link to a page</a>

or if the user isn't logged in, it will output:

<a href="https://domain.com/link">A link to a page</a>

| improve this answer | |
  • Thank you. The end result seems to give me "?user_id=<?php echo get_current_user_id(); ?>", so it literally returns the same input you gave me. Now to add more info here, I am using a link shortener and am inputting this in a tag within the URL. Does that matter? – Todd L Jan 9 at 16:36
  • @ToddL are you trying to put this inside the post content? The answer above is intended for inside .php files – Tom J Nowell Jan 9 at 17:27
  • @Todd Learn some PHP (at least). That's the good point where to start from. There are a lot of FREE tutorials. – Max Yudin Jan 9 at 17:28
  • Correct, I'm simply trying to add it to links within a WordPress page. Not sure which PHP file to edit, or where to place if necessary. – Todd L Jan 9 at 17:56
  • If there's another way to do this that does not involve PHP, that would be welcomed to. – Todd L Jan 9 at 17:58

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.