Urban Giraffe Support | Sniplets
can't get $wpdb access to work
could be a lot of potential issues, here, but here is what is going on - i basically just want to access the wordpress database (and possibly others in the future), and i read the $wpdb object/class/reference is a good place to start.
Below is my exact code, and it seems to blow up on attempting to recognize "$wpdb->get_var".
=====================
<?php include_once('wp-config.php'); ?>
<?php include_once('wp-includes/wp-db.php'); ?>
<?php global $wpdb; ?>
<?php $wpdb->get_var("SELECT COUNT(*) FROM wp_users;"); ?>
<h1>
<?php echo "Today's date is: ".date('Y'); ?>
</h1>
=====================
I tried this same stuff in Daiko's Text Widget and finally got it going, but not before I had to change the original "$wp->users" to the hard-coded "wp_users" - as seen above.
Also, it seems I have to use the opening and closing php tags on every line where PHP code exists, else I get errors. Meaning - it seems like it is not possible to have multiple lines of PHP code all within one set of enclosing "<?php ... ?>" tags. or is it?
On a side note, there's a small typo on the main documentation page at:
http://urbangiraffe.com/plugins/sniplets/
The 'Execute PHP' section has some sample code that is missing an 'echo' before the 'date()' call:
Today is the year <?php date ('Y'); ?>, and it's very lovely.
I checked out the Exec-PHP plugin, and it seems fine, except I am not crazy about having to give up the WYSIWYG editor.
I'm also interested to see any example uses of Sniplets. It looks like it can do a lot.
Thanks for any pointers.
Responses
Posted 8 months ago by Key Master
You can ignore these two lines:
<?php include_once('wp-config.php'); ?><?php include_once('wp-includes/wp-db.php'); ?>The functions provided by these files will already be included.
The correct table name is
$wpdb->users, not$wp->users. There is also no need to put <?php on every line.Reply
You must log in to post.