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

php - displaying content that includes shortcodes within an echo

programmeradmin0浏览0评论

I have created a custom comment system, when people enter in video shortcodes it doesn't display the video but just echo's out the shortcode.

I understand the do_shortcode(); command but the comments can contain comments as well as shortcodes.

Is there a best practice around having the shortscodes work?

Thanks

-Chalkie

edit this is my function:

function display_match_comments() {
    global $current_user; wp_get_current_user();
    global $match_id;
    global $wpdb;
    global $comment_success; 
    echo '<div class="latest_results" style="background:#ffffff; text-align:left !important;"">';
    echo '<h6 style="text-align:center !important;">User Comments and Pictures';
    if ( is_user_logged_in() ) {
        echo ' <a href="#add_comment">Add Comment</a>';
    }
    echo '</h6>';
    $content_size = 'one_half';
    $table_name = 'wp_match_comments';
    $row2 = $wpdb->get_results( "SELECT * FROM $table_name WHERE comment_gameid = $match_id AND comment_moderation = '1'");
    if(!$row2) { echo 'No Comments Found</span>'; }
    foreach($row2 as $row2) { 
            if(!$content_size){ $content_size = "one_half"; }
            echo '<div class="'.$content_size.'">';
            echo '<div class="latest_results" style="background:#ffffff;">';
            echo '<h6><a href="/forums/users/'.$row2->comment_name.'">'.$row2->comment_name.'</a></h6>';
            $comment_content = stripslashes_deep($row2->comment_content);
echo '<span>'.$comment_content.'<br>';
            $react_id = $row2->id. '_' .$match_id;
            echo do_shortcode('[reactions id="'.$react_id.'"]');
            echo '</span><br>';
            $text_to_be_wrapped_in_shortcode = '<span><a href="/wp-admin/admin.php?page=match_comments&id='.$row2->id.'" class="gamedetail">Manage Comment</a></span>';
            echo do_shortcode( '[UAS_role roles="administrator, moderator"]' . $text_to_be_wrapped_in_shortcode . '[/UAS_role]' );
            echo '</div>';
            echo '</div>';
            if ($content_size == "one_half") { $content_size = "one_half last_column"; goto skip2; }else{ $content_size = "one_half"; goto skip2; }
            skip2:
        }
    echo '<div id="add_comment" class="latest_results" style="background:#ffffff; text-align:left !important;"">';
    if ( is_user_logged_in() ) {
        echo '<h6 style="text-align:center !important;">Add your own comment and 
pictures below</h6>';
    if(get_query_var( 'success' ) != 'yes') { 
     ?>
<form method="POST"  action="<?php the_permalink(); echo $match_id; ?>?success=yes">
    <?php wp_editor($comment_content,"comment_content", array('textarea_rows'=>12, 'editor_class'=>'comment_content_class')); ?>
    <input type="hidden" name="comment_name" value="<?php echo $current_user->user_login; ?>"/>
    <input type="hidden" name="comment_gameid" value="<?php echo $match_id; ?>"/>
    <input type="submit" name="submitComment" value="submit" />
    </form>
<?php
    }
$default_comment = array(
            'comment_name' => '',
            'comment_content' => '',
            'comment_moderation' => '0',
            'comment_gameid' => '',
        );
$item = shortcode_atts( $default_comment, $_REQUEST );
if($_POST['submitComment']) { 
       $wpdb->prepare($wpdb->insert( $table_name, $item ));
}
if(get_query_var( 'success' ) == 'yes') { echo '<span>Your comment has been sent for approval</span><br>'; }
}else{
    echo '<h6 style="text-align:center !important;"> you must be <a href="/login-register/">logged in</a> 
    to comment</h6>';
}
    echo '</div>';
    echo '</div>';
}

This is the part that displays the comment

$comment_content = stripslashes_deep($row2->comment_content);
echo '<span>'.$comment_content.'<br>';

Within that someone could use the [video] shortcode but it displays the shortcode text not a video.

I have created a custom comment system, when people enter in video shortcodes it doesn't display the video but just echo's out the shortcode.

I understand the do_shortcode(); command but the comments can contain comments as well as shortcodes.

Is there a best practice around having the shortscodes work?

Thanks

-Chalkie

edit this is my function:

function display_match_comments() {
    global $current_user; wp_get_current_user();
    global $match_id;
    global $wpdb;
    global $comment_success; 
    echo '<div class="latest_results" style="background:#ffffff; text-align:left !important;"">';
    echo '<h6 style="text-align:center !important;">User Comments and Pictures';
    if ( is_user_logged_in() ) {
        echo ' <a href="#add_comment">Add Comment</a>';
    }
    echo '</h6>';
    $content_size = 'one_half';
    $table_name = 'wp_match_comments';
    $row2 = $wpdb->get_results( "SELECT * FROM $table_name WHERE comment_gameid = $match_id AND comment_moderation = '1'");
    if(!$row2) { echo 'No Comments Found</span>'; }
    foreach($row2 as $row2) { 
            if(!$content_size){ $content_size = "one_half"; }
            echo '<div class="'.$content_size.'">';
            echo '<div class="latest_results" style="background:#ffffff;">';
            echo '<h6><a href="https://www.husupporters.club/forums/users/'.$row2->comment_name.'">'.$row2->comment_name.'</a></h6>';
            $comment_content = stripslashes_deep($row2->comment_content);
echo '<span>'.$comment_content.'<br>';
            $react_id = $row2->id. '_' .$match_id;
            echo do_shortcode('[reactions id="'.$react_id.'"]');
            echo '</span><br>';
            $text_to_be_wrapped_in_shortcode = '<span><a href="https://www.husupporters.club/wp-admin/admin.php?page=match_comments&id='.$row2->id.'" class="gamedetail">Manage Comment</a></span>';
            echo do_shortcode( '[UAS_role roles="administrator, moderator"]' . $text_to_be_wrapped_in_shortcode . '[/UAS_role]' );
            echo '</div>';
            echo '</div>';
            if ($content_size == "one_half") { $content_size = "one_half last_column"; goto skip2; }else{ $content_size = "one_half"; goto skip2; }
            skip2:
        }
    echo '<div id="add_comment" class="latest_results" style="background:#ffffff; text-align:left !important;"">';
    if ( is_user_logged_in() ) {
        echo '<h6 style="text-align:center !important;">Add your own comment and 
pictures below</h6>';
    if(get_query_var( 'success' ) != 'yes') { 
     ?>
<form method="POST"  action="<?php the_permalink(); echo $match_id; ?>?success=yes">
    <?php wp_editor($comment_content,"comment_content", array('textarea_rows'=>12, 'editor_class'=>'comment_content_class')); ?>
    <input type="hidden" name="comment_name" value="<?php echo $current_user->user_login; ?>"/>
    <input type="hidden" name="comment_gameid" value="<?php echo $match_id; ?>"/>
    <input type="submit" name="submitComment" value="submit" />
    </form>
<?php
    }
$default_comment = array(
            'comment_name' => '',
            'comment_content' => '',
            'comment_moderation' => '0',
            'comment_gameid' => '',
        );
$item = shortcode_atts( $default_comment, $_REQUEST );
if($_POST['submitComment']) { 
       $wpdb->prepare($wpdb->insert( $table_name, $item ));
}
if(get_query_var( 'success' ) == 'yes') { echo '<span>Your comment has been sent for approval</span><br>'; }
}else{
    echo '<h6 style="text-align:center !important;"> you must be <a href="https://www.husupporters.club/login-register/">logged in</a> 
    to comment</h6>';
}
    echo '</div>';
    echo '</div>';
}

This is the part that displays the comment

$comment_content = stripslashes_deep($row2->comment_content);
echo '<span>'.$comment_content.'<br>';

Within that someone could use the [video] shortcode but it displays the shortcode text not a video.

Share Improve this question edited Jan 3, 2021 at 18:28 Chalkie asked Jan 3, 2021 at 18:14 ChalkieChalkie 92 bronze badges 3
  • Include the code, maybe someone will be able to help if they can see what's actually happening. – Tony Djukic Commented Jan 3, 2021 at 18:17
  • We can't help you without checking your code so add the code here. – Ravinder Kumar Commented Jan 3, 2021 at 18:22
  • Edited my question – Chalkie Commented Jan 3, 2021 at 18:29
Add a comment  | 

1 Answer 1

Reset to default 2

If some of your comments will contain shortcodes and some won't, there's no harm in running them all through do_shortcode(). If the content has no shortcodes in it, it will simply be returned, and if it does contain shortcodes, they'll be parsed and the content returned.

So you should be able to change

echo '<span>'.$comment_content.'<br>';

to

echo do_shortcode( '<span>'.$comment_content.'<br>' );
发布评论

评论列表(0)

  1. 暂无评论