0

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

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();
}
  • Thank you very much Jacob! Worked perfectly and I feel like I learned something :) Thank you! – akdream Jun 26 at 10:52
0

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.

  • Thank you Camille for contributing to my knowledge. – akdream Jun 26 at 10:56

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.