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

Nikola Nikolov nikolov.tmw at gmail.com
Wed Jan 8 12:33:17 UTC 2014


I still think that adding

global $wpdb;

is probably the best approach. If you really don't want to copy/paste a
single line of code, you can just use $GLOBALS['wpdb'] - same thing, just a
bit less readable(and you won't be able to use the nifty features of things
like code intel).

Anyway - whichever way you choose it usually won't be a big deal if you
decide to change your implementation in the future - you'd have to do just
a couple of search/replace's.


On Wed, Jan 8, 2014 at 2:26 PM, Abdussamad Abdurrazzaq <
abdussamad at abdussamad.com> wrote:

> Can't you stick it in a class variable instead?
>
>
> On 01/07/2014 07:43 PM, Dobri wrote:
>
>> How is
>>
>> $db = self::db();
>> //do stuff with $db
>>
>> different from
>>
>> global $wpdb;
>> //do stuff with $wpdb
>>
>> Unless you use it for one call only (self::db()->query() or something), I
>> don't see much benefit in this method?
>>
>> ~Dobri
>>
>> On Mon, 6 Jan 2014, at 5:53 PM, Ben May wrote:
>>
>>  I usually have a base class that all other classes extend, and have a
>>> self::db() which basically globals $wpdb once and returns it, saves
>>> having
>>> to write global $wpdb; each class.
>>>
>>>
>>> On 7 January 2014 01:58, Leo Baiano <ljunior2005 at gmail.com> 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
>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>>
>>>>  _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>>  _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list