最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Shortcodes output in the wrong order

matteradmin9PV0评论

I have two shortcodes that i'm using for copyright information.

// Year shortcode
function year_shortcode() {
    $year = the_time('Y');
    return $year;
}
add_shortcode( 'year', 'year_shortcode' );

// Copyright shortcode
function copyright_shortcode() {
    $copyright = '©';
    return $copyright;
}
add_shortcode( 'c', 'copyright_shortcode' );

[c][year] Copyright info stuff here...

Returns 2018 © Copyright info stuff here...

I'm trying to add the © symbol as the first text element.

I've also tested with ob_start() and ob_get_clean() but they still appear in the wrong order.

// Copyright shortcode
function copyright_shortcode() {
    ob_start();
    echo '©';
    return ob_get_clean();
}

I have two shortcodes that i'm using for copyright information.

// Year shortcode
function year_shortcode() {
    $year = the_time('Y');
    return $year;
}
add_shortcode( 'year', 'year_shortcode' );

// Copyright shortcode
function copyright_shortcode() {
    $copyright = '©';
    return $copyright;
}
add_shortcode( 'c', 'copyright_shortcode' );

[c][year] Copyright info stuff here...

Returns 2018 © Copyright info stuff here...

I'm trying to add the © symbol as the first text element.

I've also tested with ob_start() and ob_get_clean() but they still appear in the wrong order.

// Copyright shortcode
function copyright_shortcode() {
    ob_start();
    echo '©';
    return ob_get_clean();
}
Share Improve this question asked Oct 22, 2018 at 9:58 user1676224user1676224 1961 silver badge13 bronze badges 1
  • Possible duplicate of Shortcode always displaying at the top of the page – Jacob Peattie Commented Oct 22, 2018 at 10:16
Add a comment  | 

1 Answer 1

Reset to default 4

the_time() is the problem here. It echoes its output. Shortcodes, effectively, happen in this order:

  1. The text containing shortcodes is parsed to find shortcodes.
  2. Each shortcode is executed and its output stored.
  3. Each shortcode is replaced with its output.

The problem is that if you echo inside a shortcode, its output will be printed to the screen at step 2, rather than at the proper place in step 3.

So what's happening is:

  1. The text, [c][year] is parsed. The [c] and [year] shortcodes are found.
  2. [c] is executed, and © is stored for later replacement.
  3. [year] is executed, the output of the_time() is printed to the screen, and then null is stored for later replacement.
  4. © is printed to the screen where [c] was.
  5. [year] is replaced with nothing, because the shortcode callback returned nothing.

So you can see that the year is output before the copyright symbol because it was output as part of executing the shortcode in step 3, before the copyright symbol was output in step 4.

You need to replace the_time() with get_the_time() so that the year is returned to the $year variable, not printed.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far