admin管理员组文章数量:1024424
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 |2 Answers
Reset to default 1do_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:
- do_shortcode()
- [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;
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
2 Answers
Reset to default 1do_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:
- do_shortcode()
- [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;
本文标签:
版权声明:本文标题:php - Display a text message if the shortcode is not found? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745537660a2155033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
shortcode_exists( 'picu_list_collections' )
? – bueltge Commented Mar 31, 2019 at 12:41