How to insert current user ID/entry ID into a shortcode?

Question

I haven’t tested this, but you could try this:

Put this php in your functions.php.

function rt_connections_id () {
    if ( is_user_logged_in() ) {
        $usersid = get_current_user_id();
        $newout = do_shortcode(' [connections id='.$usersid.'] ');
    } else {
        $newout="";
    }
    return $newout;
}

add_shortcode('current-user-connections', 'rt_connections_id');

Now call it into action by using the shortcode [current-user-connections] INSTEAD of [connections id=1].

UDPATE based on comment…
I don’t know anything about the plugin you’re using, but if you need to query a user meta I would change my above code to this:

function rt_connections_id () {
    if ( is_user_logged_in() ) {
        $usersid = get_current_user_id();
        $entryid = get_user_meta( $usersid, 'connections_entry_id', true ); 
        $newout = do_shortcode(' [connections id='.$entryid.'] ');
    } else {
        $newout="";
    }
    return $newout;
}

add_shortcode('current-user-connections', 'rt_connections_id');

I didn’t do any error handling here to test a scenarios where connections_entry_id is empty, and I’m also assuming it is a single entry string.

Lastly, if it’s in an array, try this:

function rt_connections_id () {
    if ( is_user_logged_in() ) {
        $usersid = get_current_user_id();
        $usermeta = get_user_meta($usersid);
        $entryid = $usermeta['connections_entry_id'][0];
        $newout = do_shortcode(' [connections id='.$entryid.'] ');
    } else {
        $newout="";
    }
    return $newout;
}

add_shortcode('current-user-connections', 'rt_connections_id');

0
Killian Consulting 4 weeks 2021-12-28T17:52:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse