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’);
-
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.ww.wp.xz.cn/reference/classes/wpdb/
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.ww.wp.xz.cn/Shortcode_API
-
This reply was modified 6 years, 5 months ago by
Steven Stern (sterndata).
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’);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.
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’);You’re getting syntax errors by using “curly” quotes instead of single “straight” quotes.
add_shortcode('team_preview', 'team_players');Yes…that’s it @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.
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'; }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?
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.
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. Reason: expose curly quotes
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!
@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');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.
-
This reply was modified 6 years, 5 months ago by
The topic ‘Creating shortcode with a database query in it…’ is closed to new replies.