Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Created January 21, 2021 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarreros/78dc31ac71aa382f1a3568a8ec34b6c4 to your computer and use it in GitHub Desktop.
Save jmarreros/78dc31ac71aa382f1a3568a8ec34b6c4 to your computer and use it in GitHub Desktop.
<? // no copiar esta línea
add_filter( 'the_content', 'dcms_list_data' );
function dcms_list_data( $content ) {
$database_name = 'employees'; // nombre de la base de datos
$database_user = 'root';
$database_pass = 'root';
$database_server = 'localhost';
$table_name = 'tbl_employees'; // nombre de la tabla
$slug_page = 'empleados'; //slug de la página en donde se mostrará la tabla
if (is_page($slug_page)){
$mydb = new wpdb($database_user, $database_pass, $database_name, $database_server);
$items = $mydb->get_results("SELECT * FROM `$table_name`");
// nombre de los campos de la tabla
foreach ($items as $item) {
$result .= '<tr>
<td>'.$item->emp_no.'</td>
<td>'.$item->first_name.'</td>
<td>'.$item->birth_date.'</td>
</tr>';
}
$template = '<table class="table-data">
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Fecha</th>
</tr>
{data}
</table>';
return $content.str_replace('{data}', $result, $template);
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment