plugin development - What is the proper method of using global $post?

I am using global $post multiple times in a class. These are my doubtsIs that a bad practice?Can it result in slow we

I am using global $post multiple times in a class. These are my doubts

  1. Is that a bad practice?
  2. Can it result in slow websites?
  3. What's the proper way to use it inside a class

    class Test{
        public function(){
            global $post;
        }
    
        public function2(){
            global $post;
        }
    
        public function3(){
            global $post;
        }
    }
    

I am using global $post multiple times in a class. These are my doubts

  1. Is that a bad practice?
  2. Can it result in slow websites?
  3. What's the proper way to use it inside a class

    class Test{
        public function(){
            global $post;
        }
    
        public function2(){
            global $post;
        }
    
        public function3(){
            global $post;
        }
    }
    
Share Improve this question edited Aug 3, 2019 at 23:16 Pat J 12.5k2 gold badges28 silver badges36 bronze badges asked Aug 3, 2019 at 18:45 user145078user145078 3
  • The issue I do see is that you're missing the function keyword: eg, public function() should be public function function_name(). – Pat J Commented Aug 3, 2019 at 23:18
  • 1 @PatJ sorry that was a typo I just wanted to show to the structure – user145078 Commented Aug 3, 2019 at 23:24
  • The correct answer depends entirely on how you're using it. The vast majority of the time there's better ways than using the global $post. Your question is not specific enough to answer though. – Jacob Peattie Commented Aug 4, 2019 at 6:04
Add a comment  | 

1 Answer 1

Reset to default 1

There's nothing wrong with using the global $post variable per se, although there are people who will say that using global variables is always bad. But since you're using it multiple times in the same object, it would be better to just get the post once and store it in a class property.

I prefer using the WordPress get_post() function because it looks cleaner and get_post() does some stuff if the global $post variable isn't a WP_Post object. Using the global $post variable multiple times in a class will not slow down your site though, if that's your main worry.

So my class might look something like this:

class Test {
  protected $post;
  public function __construct() {
    $this->post = \get_post();
  }
  public function fizzbuzz() {
    //* Use $this->post instead of global $post
  }
  ... and etc. Mainly etc.
}

If you're looking to modify the global $post object, a better method would be to use the the_post action hook to access the $post object immediately after it is setup.

class Test {
  public function the_post( $post_object ) {
    //* Do something useful with the post object
  }
}
add_action( 'the_post', [ new Test(), 'the_post' ] );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272419a4619830.html

相关推荐

  • plugin development - What is the proper method of using global $post?

    I am using global $post multiple times in a class. These are my doubtsIs that a bad practice?Can it result in slow we

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信