Title: Creating shortcode with a database query in it&#8230;
Last modified: December 16, 2019

---

# Creating shortcode with a database query in it…

 *  Resolved [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/)
 * I get the basics of creating a shortcode and have the query part down, but if
   I just use the query as I normally would in a function. I’m also not sure how
   to pass a variable (or an attribute?) for the query to use.
 * The plan would be for me and other writers to use this shortcode when creating
   a Post. [team_preview team=######]
 * How do I get the function/query to read the ‘team’ part? Is it as easy as using
   the $atts or $content variables where I have $team in the query below?
 * function team_players {
 * select * from data-table
    where team = . ‘ $team ‘ .
 * }
    add_shortcode(‘team_preview’, ‘team_players’);

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/page/2/?output_format=md)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12243609)
 * Yes, you’d do it via $atts
 * [team_preview team=”xxxx”]
 * and “xxxx” would be the value for the attribute “team”.
 * Be sure to validate and sanitize the value before issuing your SQL statement.
   Use the wp_db abstraction layer to access your database. [https://developer.wordpress.org/reference/classes/wpdb/](https://developer.wordpress.org/reference/classes/wpdb/)
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12243621)
 *     ```
       where team = . ‘ $atts ‘ .
       ```
   
 * Is that how it would be used in the query?
 * FYI – I’m not using any information from wp_ tables for this query.
    -  This reply was modified 6 years, 5 months ago by [jwrbloom](https://wordpress.org/support/users/jwrbloom/).
    -  This reply was modified 6 years, 5 months ago by [jwrbloom](https://wordpress.org/support/users/jwrbloom/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12243643)
 *     ```
       function team_players ( $atts, $content ) {
          $a = shortcode_atts( array(
       	'team' => '',
       	), $atts );
           if ( empty ( $a['team'] ) {
              return '';
           }
   
       $team = $a['team']';
       // Your SQL statements go here to open the database,
       // sanitize inputs, and issue the select
       // in order to do SELECT staff FROM table WHERE team=$team
   
       // and then return the results as processed
       return $results;
       }
       ```
   
 * See [https://codex.wordpress.org/Shortcode_API](https://codex.wordpress.org/Shortcode_API)
    -  This reply was modified 6 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244583)
 * I created a plugin, have it uploaded and activated. This is what I have, which
   is essentially when you put, but before digging into the query, I just wanted
   to see if my site is reading the function.
 * It’s not.
 * In a post, I put [team_preview], and it didn’t print Hello. It just printed [
   team_preview]. What am I missing?
 *     ```
       function team_players( $atts, $content ) {
          $a = shortcode_atts( array(
       	'team' => '',
       	), $atts );
           if ( empty ( $a['team'] )) {
              return '';
           }
   
       $team = $a['team'];
   
       	echo 'Hello';
   
       return $results;
       }
       add_shortcode(‘team_preview’, ‘team_players’);
       ```
   
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244678)
 * shortcodes do not echo. They must return a string. You have no value in $results.
   You’re taking my example literally without attempting to understand it.
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244729)
 * I’m just not able to wrap my head around it. I’d like to just see how a basic
   shortcode produces before I get too deeply into building the query. Otherwise,
   I won’t know what’s working and what isn’t.
 * I put [team_preview] in a post, and it just showed [team_preview].
 *     ```
       function team_players( $atts, $content ) {
   
       	return 'Hello';
   
       }
       add_shortcode(‘team_preview’, ‘team_players’);
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244801)
 * You’re getting syntax errors by using “curly” quotes instead of single “straight”
   quotes.
    `add_shortcode('team_preview', 'team_players');`
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244841)
 * Yes…that’s it [@bcworkz](https://wordpress.org/support/users/bcworkz/). Thank
   you!
 * I may have an output question when I get back to my desk and really dig in, but
   at least now it’s working.
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12244971)
 * This is what I have now. I’m not sure it’s getting the team value from the shortcode
 * [team_preview team=“Carmel”]
 * It’s returning blank. It should have one row of data and return Test. I was able
   to get it to return Test before the actual query.
 *     ```
       function team_players( $atts, $content ) {
          $a = shortcode_atts( array(
        	'team' => '',
        	), $atts );
            if ( empty ( $a['team'] )) {
               return '';
            }
   
        $team = $a['team'];
   
       include(ABSPATH ."resources/con.php");
   
        $query = "SELECT * FROM a_game_preview_players
        where school = '" . $atts . "'";
   
       $results = mysqli_query($con,$query);
       echo mysqli_error($con);
       while($row = mysqli_fetch_assoc($results)) {
   
       	return 'Test';
   
       	}
       ```
   
    -  This reply was modified 6 years, 5 months ago by [jwrbloom](https://wordpress.org/support/users/jwrbloom/).
    -  This reply was modified 6 years, 5 months ago by [jwrbloom](https://wordpress.org/support/users/jwrbloom/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12247406)
 * It appears that the query is failing to return anything, thus the while loop 
   never runs. I don’t think you want to use $atts in a query, it’s not a scalar
   value. Wouldn’t you want to use $team for that?
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12247494)
 * I did try $team, but it didn’t change anything. When I coded the team name in
   the query, it worked, but it’s not getting the team value from the shortcode.
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12247975)
 * Turns out it was a syntax issue with the shortcode
 * Should be…
    `[team_preview team='Carmel']` …single quotes.
 * Instead of…
    `[team_preview team=“Carmel”]` …double quotes.
 * Thank you for the input!
    -  This reply was modified 6 years, 5 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: expose curly quotes
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12251427)
 * It’s actually the use of `“curly”` quotes that was the problem. They don’t work
   regardless of single or double. In shortcodes you should be able to use either
   double or single as long as they are the `"straight"` variant.
 * The forum’s parser normally obscures the distinction. I edited your last reply
   to code format your examples so that the use of the `“curly”` variants remain
   visible.
 * Anyway, I’m glad you resolved this!
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12262603)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) it is working great,
   but I wanted to alter something.
 * $row[‘players’] creates a list of 3-4 players for a given team. It’s inline. 
   I want it produced in an unordered list. I know how to explode and sort the list,
   but I’m not sure who to make it work within the ‘return’.
 * Echoing doesn’t work. (Is that a WordPress thing? Not fully sure why I have to
   return vs echo, when I see examples other functions doing that, but that’s not
   important right now.) I’ve tried return array, likely incorrectly.
 *     ```
       function team_players( $atts, $content ) {
          $a = shortcode_atts( array(
        	'team' => '',
        	), $atts );
            if ( empty ( $a['team'] )) {
               return 'Empty';
            }
   
        $team = $a['team'];
   
       include(ABSPATH ."resources/con.php");
   
        $query = "SELECT * FROM a_game_preview_players
        where teamID = '" . $team . "'";
   
       $results = mysqli_query($con,$query);
       echo mysqli_error($con);
       while($row = mysqli_fetch_assoc($results)) {
   
       	return '<b>' . $row['school'] . '</b><br/>'
       		. $row['players'];
   
       	}
   
       }
       add_shortcode('players', 'team_players');
       ```
   
 *  Thread Starter [jwrbloom](https://wordpress.org/support/users/jwrbloom/)
 * (@jwrbloom)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/#post-12263283)
 * For anyone else who comes across this problem:
 * I figured it out using the ob_start and ob_get_clean functions.
    (see more on
   php output buffers)
 * Replaced what is in the while loop above with…
 *     ```
        	ob_start();
   
        		echo '<b>' . $row['school'] . '</b><br/>
        			<ul style="list-style-type:none;">';
   
        		$players = $row['players'];
        		$players = explode("; ",$players);
   
        			foreach ($players as $player) {
        				echo '<li>' . $player . '</li>';
   
        			}
        			echo '</ul>';
   
        	$player_list = ob_get_clean();
   
        	return $player_list;
   
       	}
   
       }
       add_shortcode('players', 'team_players');
       ```
   
 * The ob_start starts the buffer.
    The ob_get_clean gets what’s in the buffer and
   empties for its next use.
    -  This reply was modified 6 years, 5 months ago by [jwrbloom](https://wordpress.org/support/users/jwrbloom/).

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/page/2/?output_format=md)

The topic ‘Creating shortcode with a database query in it…’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 16 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/creating-shortcode-with-a-database-query-in-it/page/2/#post-12265339)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
