the_author()
filter hook can be used to modify the output of the_author()
. When using the hook, is it possible to know which PHP file generates which call to the_author()
?
For example, say I have this custom function attached to the_author()
filter hook, which modifies the_author()
output:
add_filter("the_author", "change_author");
function change_author($author) {
$author = "NEW AUTHOR!";
return $author;
}
When I load my page, I can see this change occur in 5 separate sections:
- Once in the header (from file header.php).
- Twice in the body (from file page.php).
- Twice in the footer (from file footer.php).
Inside my function change_author()
, is there any way to know which PHP file is currently making the call to the_author()
?
My motivation for asking is because I'd like to change the author for only certain sections, i.e. only when the_author()
is called from a specific file (my_file.php). For all other instances of the the_author()
I want to leave the output unmolested. I'm just wondering if that's possible to accomplish using the_author()
filter hook.
the_author()
filter hook can be used to modify the output of the_author()
. When using the hook, is it possible to know which PHP file generates which call to the_author()
?
For example, say I have this custom function attached to the_author()
filter hook, which modifies the_author()
output:
add_filter("the_author", "change_author");
function change_author($author) {
$author = "NEW AUTHOR!";
return $author;
}
When I load my page, I can see this change occur in 5 separate sections:
- Once in the header (from file header.php).
- Twice in the body (from file page.php).
- Twice in the footer (from file footer.php).
Inside my function change_author()
, is there any way to know which PHP file is currently making the call to the_author()
?
My motivation for asking is because I'd like to change the author for only certain sections, i.e. only when the_author()
is called from a specific file (my_file.php). For all other instances of the the_author()
I want to leave the output unmolested. I'm just wondering if that's possible to accomplish using the_author()
filter hook.
2 Answers
Reset to default 1You can determine the calling files using debug_backtrace
in PHP, which will give you the backtrace of the functions called and the file called from. WP core provides wp_debug_backtrace_summary, which makes doing things like this easier. Using the condition stated in your question of being called from my_file.php
you could do something like this:
add_filter( 'the_author', 'change_author' );
function change_author($author) {
if ( false !== strpos( wp_debug_backtrace_summary(), 'my_file.php' ) ) {
$author = "NEW AUTHOR!";
}
return $author;
}
By not passing any args to wp_debug_backtrace_summary
it will output the summary as a string. Then we just use strpos
to check if my_file.php
is included in the output as a calling file and do the changes.
As mentioned by others, this might not be the best approach, but it is easily doable.
From your comment:
Sure I can try to explain in terms you've described. My theme displays a 'featured posts' section at the bottom of each individual post. It does so via a theme file:
related_posts.php
. In it, it displays the author for a handful of posts, each time usingthe_author()
. I want to return a blank string forthe_authoer()
in each of these cases. Does that make sense?
You might be able to achieve this with normal template tags. We could implement it as the following logic:
- If we are viewing a single post.
- If the
the_author()
function is being used for any other post than the current individual post.
This could work because the author for the current main post isn't likely being displayed anywhere else on the page, because the related posts can't be the current post, by definition.
The reason we can do this is because get_the_ID()
will return the post ID of the post whose author is being displayed, while get_queried_object_id()
will get the ID of the full post that's being displayed, no matter where we use it. So we just need to compare them:
add_filter(
'the_author',
function( $author ) {
// We only want our filter to affect single post views.
if ( is_single() ) {
// If the author is being displayed for any post other than the current post.
if ( get_the_ID() !== get_queried_object_id() ) {
$author = '';
}
}
return $author
}
);
One thing you'll need to consider, though, is that if the theme is outputting markup around the author name, that will still be output. The only way to remove that would be to modify the template.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744797307a4594281.html
my_file.php
instead ofthe_author
? What you're asking for is not a trivial thing, and is beyond most developers capabilities, the result may not be exactly what you want either, specifically you'll need the reflection API to be available. I can guarantee there are better ways to solve your problem of displaying different things on your template, such as using a custom function – Tom J Nowell ♦ Commented Jan 28, 2020 at 12:38the_author()
. I want to return a blank string forthe_authoer()
in each of these cases. Does that make sense? – cag8f Commented Jan 28, 2020 at 15:06