admin管理员组文章数量:1130349
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 02 Answers
Reset to default 1Your 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...
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 02 Answers
Reset to default 1Your 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...
本文标签: hooksInserting a functions output after the content
版权声明:本文标题:hooks - Inserting a functions output after the content 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749057467a2309576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论