0

I'm wondering if someone can help with a code snippet for the functions.php.

I am using a plugin called Connections Pro with the extension Link, which connects the entries to the wordpress user. It has a shortcode that is very simple: [connections id=1] where after the number is the Entry ID ($entryID) that can be manually set for the single user that should be displayed.

I'd like it to instead be the Entry ID for the logged in user.

Any chance someone has an idea?

1 Answer 1

0

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');

Here is some error correction:

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];
        if ( ! empty( $entryid ) ) {
            $newout = do_shortcode(' [connections id='.$entryid.'] ');
        } else {
            $newout = do_shortcode(' [connections_link_view text_add="Add My Entry"] ');
        }
    } else {
        $newout = '';
    }
    return $newout;
}

add_shortcode('current-user-connections', 'rt_connections_id');
9
  • That is almost perfect. You not only pulled the current user id but paired it with the shortcode to make the new shortcode. I just figure out about an hour ago that the User ID and Entry ID aren't the same but I also figure out that the Entry ID is in the User Meta as an array: connections_entry_id. Can this be altered to insert the value from the array instead of the user id? Dec 29, 2021 at 5:51
  • @KillianConsulting.. I updated my answer for you.
    – rudtek
    Dec 29, 2021 at 6:01
  • I appreciate your help. Especially when you don't know the plugin. Well I'm not sure if it's pulling the meta data because now it is showing all data as though there is no specific ID. The meta data shows as an array like this: array ( 0 => '421', ) Dec 29, 2021 at 6:09
  • @KillianConsulting I posted a method that may work for that.
    – rudtek
    Dec 29, 2021 at 6:23
  • It works! I don't know how you figured that out without even knowing the plugin. The only thing it doesn't know what to do is what to display if the meta data isn't there. If I could bother you for help a last time. If there is no connections_entry_id meta data can it instead do_shortcode(' [connections_link_view text_add="Add My Entry"] ') ? Dec 29, 2021 at 16:18

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.