How can I access to my WordPress database and run SQL from the widget code? (I need to access to the settings of one of my plugins from the widget code)
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You can check this http://codex.wordpress.org/Class_Reference/wpdb for full documentation. In resume you can do something like the follow from any part of your widget code:
just include
wp-blog-header.php
in the fileIf you want to retrieve some information from the database, you can use one of four helper functions to query the database and retrieve the data.
get_results():
This is the function that we looked at earlier. It is best for when you need two-dimensional data (multiple rows and columns). It converts the data into an array that contains separate objects for each row.
get_row():
When you need to find only one particular row in the database (for example, the post with the most comments), you can use get_row(). It pulls the data into a one-dimensional object.
get_col():
This method is much the same as get_row(), but instead of grabbing a single row of results, it gets a single column. This is helpful if you would like to retrieve the IDs of only the top 10 most commented posts. Like get_row(), it stores your results in a one-dimensional object.
get_var():
In many cases, you will need only one value from the database; for example, the email address of one of your users. In this case, you can use get_var to retrieve it as a simple value. The value’s data type will be the same as its type in the database
An example:
Documentation: http://codex.wordpress.org/Class_Reference/wpdb
Source: http://wp.smashingmagazine.com/?p=98071
Hope this helps.