php - Need help transforming echo to return for use with shortcode

I'm not so good at coding but managed to write a little piece of code to have a shortcode to display a co-authors (

I'm not so good at coding but managed to write a little piece of code to have a shortcode to display a co-authors (co-authors plus plugin) bio/description in posts.

The code I came up with is

<?php function torque_hello_world_shortcode() { ?>
<?php if ( function_exists( 'get_coauthors' ) ) ?>
<?php $coauthors = get_coauthors(); ?>
<?php foreach ( $coauthors as $coauthor ) { ?>
    <div><span class="authorboxsinglename"><?php echo ( $coauthor->display_name ); ?></span></div>
    <div><span class="authorboxsinglebio"><?php echo ( $coauthor->description ); ?></span></div>
<?php } ?>
<?php } ?>

It works, except: the content appears at the very top of the post, not where I put the shortcode. Somewhere I've read it might be because I used echo instead of return.

Since I'm not good at coding I have no clue how to change my code to return. Could anyone do that for me? It would be really appreciated.

Kind regards, Cédric

I'm not so good at coding but managed to write a little piece of code to have a shortcode to display a co-authors (co-authors plus plugin) bio/description in posts.

The code I came up with is

<?php function torque_hello_world_shortcode() { ?>
<?php if ( function_exists( 'get_coauthors' ) ) ?>
<?php $coauthors = get_coauthors(); ?>
<?php foreach ( $coauthors as $coauthor ) { ?>
    <div><span class="authorboxsinglename"><?php echo ( $coauthor->display_name ); ?></span></div>
    <div><span class="authorboxsinglebio"><?php echo ( $coauthor->description ); ?></span></div>
<?php } ?>
<?php } ?>

It works, except: the content appears at the very top of the post, not where I put the shortcode. Somewhere I've read it might be because I used echo instead of return.

Since I'm not good at coding I have no clue how to change my code to return. Could anyone do that for me? It would be really appreciated.

Kind regards, Cédric

Share Improve this question edited Jun 26, 2019 at 0:55 akdream asked Jun 26, 2019 at 0:18 akdreamakdream 32 bronze badges 1
  • 1 Possible duplicate of Return vs Echo Shortcode – ngearing Commented Jun 26, 2019 at 3:07
Add a comment  | 

2 Answers 2

Reset to default 0

There's two ways to do it.

First, you could put any HTML into a string, and concatenate it with ., and .= to add a string to an existing value. This way you can build up a large string and return it at the end:

function torque_hello_world_shortcode() {
    $html = '';

    if ( function_exists( 'get_coauthors' ) ) {
        $coauthors = get_coauthors();

        foreach ( $coauthors as $coauthor ) {
            $html .= '<div><span class="authorboxsinglename">' . $coauthor->display_name . '<span></div>';
            $html .= '<div><span class="authorboxsinglebio">' . $coauthor->description . '</span></div>';0
        }
    }

    return $html;
}

(You don't need to have opening PHP tags on every line.)

Or, you could use 'output buffering' to capture all the output, and then return it at the end. This is more useful when dealing with large amounts of HTML. To start capturing use ob_start() and to get the captured output use ob_get_clean():

function torque_hello_world_shortcode() {
    ob_start();

    if ( function_exists( 'get_coauthors' ) ) {
        $coauthors = get_coauthors();

        foreach ( $coauthors as $coauthor ) {
            ?>

            <div><span class="authorboxsinglename"> <?php echo $coauthor->display_name; ?> <span></div>
            <div><span class="authorboxsinglebio"> <?php echo $coauthor->description; ?> </span></div>

            <?php
        }
    }

    return ob_get_clean();
}

To return something, you need to create a string variable like this :

$output = '<div><span class="authorboxsinglename">'.$coauthor->display_name.' </span></div>';
$output .= ' <div><span class="authorboxsinglebio">'.$coauthor->description.'</span></div>';

return $output;

The main things to know here :

  • The PHP concatenation operator for strings is ..
  • For readability, you can use .= to append the argument on the right side to the argument on the left side.
  • We don't need to echo the $coauthor variables because they are part of the $output string that is returned (and then echoed by the Wordpress shortcode function).

And yes you are right about why your Echo is not working, as seen on the add_shortcode Wordpress Codex page :

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

Also, I removed the last closing </div> because it wasn't closing anything.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745369680a4624742.html

相关推荐

  • php - Need help transforming echo to return for use with shortcode

    I'm not so good at coding but managed to write a little piece of code to have a shortcode to display a co-authors (

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信