$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Inserting a functions output after the content|Programmer puzzle solving
最新消息: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)

hooks - Inserting a functions output after the content

matteradmin10PV0评论

So I am trying to add a button at the end of my blog posts, here is the code I have in the plugin so far:

<?php
/*
Plugin Name: etc....
*/

function shares_content() { ?>
  <div class='social-shares-container'>
    <a href='.php?u=<?php echo the_permalink(); ?>'>Share on Facebook</a>
  </div> <?php
}

function shares_add_buttons($content) {
  global $post;

  if (!is_page() && is_object($post)) {
    return $content . shares_content();
  }

  return $content;
}

add_filter('the_content', 'shares_add_buttons');
?>

This adds the link just before my content, but if I do something like this, it adds the new content in the desired place (after the_content):

function shares_add_buttons($content) {
  global $post;

  if (!is_page() && is_object($post)) {
    return $content . 'some random content';
  }

  return $content;
}

Could anyone tell me why this is please?

So I am trying to add a button at the end of my blog posts, here is the code I have in the plugin so far:

<?php
/*
Plugin Name: etc....
*/

function shares_content() { ?>
  <div class='social-shares-container'>
    <a href='https://www.facebook/sharer/sharer.php?u=<?php echo the_permalink(); ?>'>Share on Facebook</a>
  </div> <?php
}

function shares_add_buttons($content) {
  global $post;

  if (!is_page() && is_object($post)) {
    return $content . shares_content();
  }

  return $content;
}

add_filter('the_content', 'shares_add_buttons');
?>

This adds the link just before my content, but if I do something like this, it adds the new content in the desired place (after the_content):

function shares_add_buttons($content) {
  global $post;

  if (!is_page() && is_object($post)) {
    return $content . 'some random content';
  }

  return $content;
}

Could anyone tell me why this is please?

Share Improve this question asked Dec 30, 2018 at 19:58 D. WinningD. Winning 1235 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 1

Your shares_content function directly outputs content, which won't work if you're trying to assign the results to a variable or use it in a return statement within another function.

You can change it to return the string:

function shares_content() {
    $content = "<div class='social-shares-container'><a href='https://www.facebook/sharer/sharer.php?u=%s'>Share on Facebook</a></div>";
    return sprintf( $content, get_permalink() );
}

It's also worth pointing out here the use of get_permalink(). If you look at the source code, that function also returns its value. There is another API function, the_permalink(), which contains an echo instead of a return. That would also break your filter output. Most WordPress functions have two versions like this.

In this line:

return $content . shares_content();

You concatenate original content with result of shares_content function. So it looks correct, but then...

In that function:

function shares_content() { ?>
  <div class='social-shares-container'>
    <a href='https://www.facebook/sharer/sharer.php?u=<?php echo the_permalink(); ?>'>Share on Facebook</a>
  </div> <?php
}

you don’t return anything, so this function has no result, so nothing is appended to the content in your filter.

But also... This function echoes div with link, so that content is printed, when the function is ran, so it gets printed before the content...

Post a comment

comment list (0)

  1. No comments so far