Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Búsqueda en una tabla personalizada de WordPress usando un Shortcode
<?php // No copiar esta línea
// ShortCode
add_action( 'init', 'dcms_frm_shortcode' );
function dcms_frm_shortcode(){
add_shortcode('search-form', 'dcms_search_form');
function dcms_search_form( $atts , $content ){
global $wpdb;
$search = $_GET['search']??'';
$table_name = 'customers';
$field_search1 = 'customerNumber';
$field_search2 = 'customerName';
if ( $search ){
$sql = "SELECT * FROM {$table_name}
WHERE {$field_search1} = '{$search}' OR $field_search2 LIKE '%{$search}%'";
$items = $wpdb->get_results($sql);
<input type="search" name="search" id="name" placeholder="Buscar..." value="<?= $search ?>" required>
<input type="submit" value="Buscar">
<?php if ( $items ): ?>
<table class="custom-table">
<?php foreach($items as $item): ?>
<td><?= $item->customerNumber ?></td>
<td><?= $item->customerName ?></td>
<td><?= $item->phone ?></td>
<td><?= $item->addressLine1 ?></td>
<td><?= $item->city ?></td>
<td><?= $item->creditLimit ?></td>
<?php endforeach; ?>
<?php elseif ( $search ): ?>
<div><strong>No se encontraron resultados</strong></div>
<?php endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment