Plugins or Tutorials for displaying data from SQL-db on WP-page? [closed]

This is a pretty vague and broad question, but I’ll give it a shot. You can add the following code into a functionality plugin or if you’re running a theme, you could place it in your theme’s functions.php file. (Obviously you’ll have to change the SQL, attributes, etc – just a proof of concept)

add_shortcode( 'wpse_weather_data', 'wpse_weather_data_shortcode_cb' );
function wpse_weather_data_shortcode_cb( $atts ) {
    $atts = shortcode_atts( array(
        'fallbackValue' => '',
        'sensor' => 0
     ), $atts );

     extract( $atts );

     // Make the connection to a separate database using PHP. 
     // You might want to store these credentials as constants in wp-config.php 
     // to keep all the db credentials in one place.
     $db = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
     if ( !$db ) {
         return $fallbackValue;
     }

     // Make the query
     $query = "SELECT * FROM table_name WHERE sensor = {$sensor}";
     $result = mysqli_query($db, $query);
     if ( !$result ) {
         return $fallbackValue;
     }

     $row = mysqli_fetch_array($result);
     // Put the data on the page... FYI you have to return the data from a shortcode, not echo it.
}

Building an options page, storing past values in a database, etc are way beyond the scope of this forum, but this would give you a way to get started. You can customize it (per #3) by adding different attributes to the shortcode. For example, here you can add additional sensors and specify a fallback value by placing the shortcode somewhere in the content and calling it like so: [wpse_weather_data sensor="0" fallbackValue="Could not fetch sensor data"]

Hope this steers you in the right direction.

Leave a Comment