[wp-hackers] How to use $wpdb in plugins

Eric Andrew Lewis eric.andrew.lewis at gmail.com
Mon Jan 6 16:35:43 UTC 2014

I think the best practice here is to define and use the $wpdb global in every method you need it. You could perhaps store it by reference in a class variable, or create a singleton wrapper for accessing $wpdb, but both of these are stepping out of the “WordPress” way, and add a level of abstraction that isn’t adding much benefit, and perhaps confusing other programmers that touch your code.

Eric Andrew Lewis

On Monday, January 6, 2014 at 9:58 AM, Leo Baiano wrote:

> What better way to use the global $ wpdb in building a plugin? I'm working
> on a plugin and some methods need to access data in the database tables,
> including creating, and I was in doubt about the best way, like, do not
> want to quit repeating global $ wpdb in all methods of my class.
> How do you do?
> --  
> Amplexos,
> Leo Baiano
> Web Developer
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com (mailto:wp-hackers at lists.automattic.com)
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list