• I have shortcode which I can see only logged in, no public. Everything else works fine with public. Can someone advice me how to enable this in public?

    My Child-theme funtions.php file and shortcode is [my-shortcode]

    <?php
    
    function wpb_postsbycategory() {
    // the query
    $the_query = new WP_Query( array( 'category_name' => 'yhteystiedot', 'posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC' ) ); 
     
    // The Loop
    if ( $the_query->have_posts() ) {
        $string .= '<div class="postsbycategory widget_recent_entries">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            
    		
                $string .= '<div>'.'' .get_the_content() .'</div>';
                $string = apply_filters('the_content', $string);
    	$string = str_replace(']]>', ']]>', $string);
                }
        } else {
        // no posts found
    }
    $string .= '</div>';
     
    return $string;
     
    /* Restore original Post Data 
    wp_reset_postdata();*/
    }
    // Add a shortcode
    add_shortcode('my-shortcode', 'wpb_postsbycategory');
    
    add_filter( 'widget_text', 'shortcode_unautop');
    add_filter( 'widget_text', 'do_shortcode');
    • This topic was modified 6 years, 11 months ago by bcworkz. Reason: code fixed
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi

    if(is_user_logged_in()){
      return $string;
    }else{
      return '';  
    }

    Please try this code.

    Thread Starter m1rzu

    (@m1rzu)

    I tried and didn’t work. I tried also

    if(!is_user_logged_in()){
      return $string;
    }else{
      return '';
    }

    I think this shortcode should be public/registered somehow? I see it work properly only if I’m logged in WordPress.

    • This reply was modified 6 years, 11 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Your shortcode works for me when added to my site, logged in or not. I did make some minor changes. Naturally I used a category that exists on my site in the query. I removed the concatenation dot from the first $string assignment
    $string = '<div class="postsbycategory....
    The code generates a PHP warning with the dot in place.

    The shortcode tag in your add_shortcode() function call was demarcated with “curly” quotes. All quotes in PHP code must be the "straight" kind. I updated the quotes in your OP in order to be able to test your code.

    Be sure your query is returning something, otherwise it may appear that the shortcode is not working. Try replacing the // no posts found comment with an echo statement so you can see that the shortcode is working even if the query fails.

    Be sure adjacent text within the same container as the shortcode is visible to all users. Add some adjacent text if none exists to verify.

    You may have your theme or a plugin interfering. Temporarily add your handler code to a default theme’s functions.php. Use the health check plugin’s troubleshooting tab to switch the active theme to the default one of which you added the shortcode handler and selectively deactivate plugins to narrow down the source.

    In the future when you post code in these forums, please be sure to demarcate with backticks or use the code button. Failure to do so makes testing your code by others very difficult because the forum’s parser corrupts PHP code, rendering it unusable.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘My own shortcode show only for logged in user not public’ is closed to new replies.