Can't Register Shortcode w/Database Call
-
The following circumstances are predicated by this being a client site that I have to deal with and can’t just do things in a more sensible way. So I’m trying to make this as sensible as possible.
THE PROBLEM: I want to register a shortcode. I can set up a simple one that works, but if I try to make a database call in the function that returns the shortcode value, something is not happy. From what I can see/test, things should work. But it’s not.
EXPLANATION:
I’m calling the shortcode from a page that uses a custom template. The template dumps the content out throughthe_content()
. A simple shortcode function works (static string value type of stuff) but the other one does not.THE NON-WORKING CODE, IN functions.php:
function idxtoday() { // Air Quality Index Info global $index_today; $index_today = $wpdb->get_var( "SELECT aqi FROM $wpdb->datapoints WHERE date = '" . date( 'Y-m-d' ) . "';" ); $index_today = ( is_null( $index_today ) ) ? '--' : $index_today; return "<b>$index_today</b>"; } function register_shortcodes () { add_shortcode('idxtoday','idxtoday'); } add_action( 'init', 'register_shortcodes');
Now, if I remove all the database stuff and just make
$index_today
some string like “Hi there, sucker!” then the shortcode works.So… I’m guessing this is either a database thing, or a database thing combined with a functions.php thing combined with a shortcode thing. Or something.
The page I need to implement this on is in draft, as I can’t publish an unfinished page and they have no staging site. But the site is: https://littlestepsbigimpact.com/
Any ideas?
- The topic ‘Can't Register Shortcode w/Database Call’ is closed to new replies.