3

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="%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

  • 1
    Have you tried the core function shortcode_exists( 'picu_list_collections' )? – bueltge Mar 31 at 12:41
1

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]
  • Thanks Qaisar, your code display no message but work when gallery exist, it seems may be a parameter I forgot – Nicolas Logerot Mar 30 at 15:31
  • @NicolasLogerot , I, in my answer, assumed that if there is no gallery, then the output of your shortcode is an empty string i.e. "" . Can you tell what is the output of shortcode when there is no gallery, or you can replace "" in my code with that output. – Qaisar Feroz Mar 30 at 15:35
  • Thank you very much but I don't know how to find the output of shot code when is no gallery – Nicolas Logerot Mar 30 at 15:40
  • What is output just after the this is your galleries USER_NAME when page is loaded? – Qaisar Feroz Mar 30 at 15:53
  • OK, Can you share screenshots in cases of a user has no galleries? – Qaisar Feroz Mar 30 at 16:12
1

This is a curious piece of code. Apparently users can upload a gallery, but your snippet does not show the upload code, so it's impossible to know how it is stored and hence to access to find out if it exists.

Presumably, the picu_list_collections shortcode knows how to access the gallery, but if there is no gallery you want it to return different output. That would mean modifying the shortcode function itself.

If you can only modify the current function you must not echo the result, but store it in a variable. Then you can perform a test on it to establish it the returned code reflects the existence of a gallery, for instance by testing it for an img-tag. Like this:

$gall = do_shortcode( '[picu_list_collections email="' . $current_user->user_email . '"]' );
if (false=strpos($gall,'<img')) echo 'Sorry no here' else echo $gall;

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.