0

I need some help, I am learning php through wordpress, so I don't really know the php basic and don't really know wordpress either. so I have this shortcode function:

function links( $atts, $content = null ){
    $args = shortcode_atts( array(
        's' => '',
        'l' => '',
    ), $atts);
    
    $api = file_get_contents('API URL'.$args['l']);
    $result1 = '<a href="'. $api .'">'. $args['s'] .'</a>';
    $result2 = '<a href="'. $args['l'] .'">'. $args['s'] .'</a>';

    return THIS IS I NEED THE IF LOGGED IN SHOWS $result2 AND THE OTHERWISE
}
add_shortcode ('link', 'links');

the shortcode is already written in custom meta box group text area from foreach.

[link s="Google" l="https://google.com"]

I write this in single.php

echo do_shortcode($linkx['link_box']);

thank you in advance.

1

Use is_user_logged_in() in the logic to decide what to return. See https://developer.wordpress.org/reference/functions/is_user_logged_in/

I assume $linkx['link_box'] contains the shortcode.

2
  • when I tried to put is_user_logged_in() in functions.php as I input the shortcode there, it doesn't work, I don't know why. what if I put it on single.php ? how does it work? I mean how I write it? – Tiyo Nov 15 '20 at 16:36
  • nevermind, got it now. my mistake wrong code that it didn't work in the first place. Thank you so much. – Tiyo Nov 16 '20 at 11:45

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.