最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Display a text message if the shortcode is not found?

programmeradmin1浏览0评论

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

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

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

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

Share Improve this question edited Apr 3, 2019 at 22:18 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Mar 30, 2019 at 14:36 Nicolas LogerotNicolas Logerot 676 bronze badges 1
  • 1 Have you tried the core function shortcode_exists( 'picu_list_collections' )? – bueltge Commented Mar 31, 2019 at 12:41
Add a comment  | 

2 Answers 2

Reset to default 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/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]

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;
发布评论

评论列表(0)

  1. 暂无评论