filters - When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?

the_author() filter hook can be used to modify the output of the_author().When using the hook, is it possible to know

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.

Share Improve this question asked Jan 28, 2020 at 11:59 cag8fcag8f 1,9973 gold badges21 silver badges31 bronze badges 4
  • 1 You can't really based on the file, but there could be some logic you could use to do something similar. Can you describe the circumstances in more 'business-logic' terms? For example, you want to change filter the author "when displayed for the current main post, but not for other posts listed in a widget". – Jacob Peattie Commented Jan 28, 2020 at 12:24
  • 1 Have you considered calling a custom function from my_file.php instead of the_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:38
  • @TomJNowell Directly editing my_file.php is indeed my fall back solution. I would have to copy a theme file from parent to child to do it. So before I do that, i was wondering if I could achieve the same with custom code in my child theme. I realize this approach is probably the more advantageous solution. But one goal of my question is to at least learn if what I want is at all possible. If not for this exercise, but for the future. – cag8f Commented Jan 28, 2020 at 15:04
  • 1 @JacobPeattie 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 using the_author(). I want to return a blank string for the_authoer() in each of these cases. Does that make sense? – cag8f Commented Jan 28, 2020 at 15:06
Add a comment  | 

2 Answers 2

Reset to default 1

You 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 using the_author(). I want to return a blank string for the_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:

  1. If we are viewing a single post.
  2. 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信