Display a text message if the shortcode is not found?

I’m using the do_shortcode function to add a shortcode in a page. But I would like to check if that shortcode exists before displaying it. If the user has no gallery, I would like to display a message like this: “Sorry no gallery here”.

This is my PHP:

<?php
/**
* galeries content
*/

function iconic_galeries_endpoint_content() {
echo /* Template Name: Client Area */

get_header();
?>

<div id="primary" class="content-area">
    <main id="main" class="site-main">
        <article>
            <header class="entry-header">
                <h1><?php _e( 'Vos galeries photos.', 'my-theme' ); ?></h1>
            </header>
            <div class="entry-content">
            <?php
            $current_user = wp_get_current_user();

            if ( isset( $current_user->user_email ) ) {
                echo '<p>' . sprintf( __( '%s, this is your galleries', 'my-theme' ), $current_user->display_name ) . ':</p>';
                echo do_shortcode( '[picu_list_collections email="' . $current_user->user_email . '"]' );
            }
            else {
                echo '<p>' . sprintf( __( 'Please <a href="https://wordpress.stackexchange.com/questions/333009/%s">log in</a> to see this page', 'my-theme' ), wp_login_url( get_permalink() ) ) . '.</p>';
            }
            ?>
            </div>
        </article>
    </main>
</div>

<?php
}

I haven’t found a way to return a message like: “Sorry no gallery here”.

Does anyone have a solution?


When client has a gallery to approve
Client has no galerie to approve

And when client has no gallery to approve or he has already approve his photos
Client has a gallery to approve

2 Answers
2

do_shortcode() returns content with shortcodes filtered out. We can check the return value and display appropriate message accordingly.

$output = do_shortcode( '[picu_list_collections email="' . $current_user->user_email . '"]' );

// the shortcode returns an empty <ul> tag, if there is no gallery 
// https://plugins.trac.wordpress.org/browser/picu/trunk/frontend/includes/picu-template-functions.php#L464

if($output == '<ul class="picu-collection-list"></ul>') echo "Nothing here";
else echo $output;

I hope this may help!

References:

  1. do_shortcode()
  2. [picu_list_collections]

Leave a Comment