admin管理员组

文章数量:1122832

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.

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.

Share Improve this question asked Jan 9, 2020 at 16:11 Todd LTodd L 11 3
  • 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 Commented Jan 9, 2020 at 17:23
  • 1 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 Commented Jan 9, 2020 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 Commented Jan 9, 2020 at 18:06
Add a comment  | 

1 Answer 1

Reset to default 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>

本文标签: Append USER ID to an outbound link