Skip to content

Instantly share code, notes, and snippets.

@rsoury
Created October 24, 2018 04:30
Show Gist options
  • Save rsoury/7c9a17080bd3e7dee19e1b76e7295222 to your computer and use it in GitHub Desktop.
Save rsoury/7c9a17080bd3e7dee19e1b76e7295222 to your computer and use it in GitHub Desktop.
Wordpress shortcode to Access DB Items
function your_shortcode_function( $atts ){
// $atts in case you want to receive attributes
// This lets you set default results for attributes that aren't provided.
$attributes = shortcode_atts(array(
"text" => "some text",
"link" => "Some link"
), $atts);
$value = "";
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) {
// each column in your row will be accessible like this
$value = $row->column_name;
// So imagine this loops over each row, and each value in that row is accessed by referencing the column as a property.
}
return $value
}
add_shortcode("your_shortcode_name", "your_shortcode_function");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment