How To Fetch Data From Custom Table Functions.php

prefix . “students”; // wp_students table $message = “”; // Handle form submission if ( isset($_POST[‘adc_submit_student’]) ) { $firstname = sanitize_text_field($_POST[‘firstname’]); $lastname = sanitize_text_field($_POST[‘lastname’]); $class = sanitize_text_field($_POST[‘class’]); $roll_number = sanitize_text_field($_POST[‘roll_number’]); $email = sanitize_email($_POST[’email’]); $phone = sanitize_text_field($_POST[‘phone’]); // Insert into DB $wpdb->insert( $table_name, array( ‘firstname’ => $firstname, ‘lastname’ => $lastname, ‘class’ => $class, ‘roll_number’ => $roll_number, ’email’ => $email, ‘phone’ => $phone, ), array(‘%s’,’%s’,’%s’,’%s’,’%s’,’%s’) ); $message = “

✅ Student record added successfully!”; } // Build form ob_start(); ?>

<div class="adc-student-form">
    <h2>Add New Student</h2>
    <?php echo $message; ?>
    <form method="post">
        <p>
            <label>First Name</label><br>
            <input type="text" name="firstname" required>
        </p>
        <p>
            <label>Last Name</label><br>
            <input type="text" name="lastname" required>
        </p>
        <p>
            <label>Class</label><br>
            <input type="text" name="class" required>
        </p>
        <p>
            <label>Roll Number</label><br>
            <input type="text" name="roll_number" required>
        </p>
        <p>
            <label>Email</label><br>
            <input type="email" name="email" required>
        </p>
        <p>
            <label>Phone</label><br>
            <input type="text" name="phone" required>
        </p>
        <p>
            <input type="submit" name="adc_submit_student" value="Save Student">
        </p>
    </form>
</div>

<?php
return ob_get_clean();

}
add_shortcode(‘student_form’, ‘adc_student_form_shortcode’);

Fetch by roll number

// Shortcode: Search Student by Roll Number [search_student]
// Everything (PHP + CSS) in one snippet
// ============================
function adc_search_student_form() {
global $wpdb;
$table_name = $wpdb->prefix . “students”; // change if your table name differs

// --- CSS (printed once) ---
static $css_printed = false;
if ( ! $css_printed ) {
    $css_printed = true;
    echo '<style>

/* ===== Card container (form) ===== / form.adc-student-form{ max-width:520px; margin:24px auto; padding:24px; background:#fff; border-radius:16px; box-shadow:0 10px 24px rgba(0,0,0,.06); font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif; } form.adc-student-form p{ margin:0 0 18px; } form.adc-student-form label{ display:block; margin:0 0 8px; font-size:14px; font-weight:600; color:#374151; } form.adc-student-form input[type=”text”], form.adc-student-form input[type=”email”], form.adc-student-form input[type=”number”]{ width:100%; height:46px; padding:10px 14px; background:#f9fafb; border:1px solid #e5e7eb; border-radius:10px; font-size:14px; transition:border-color .2s, box-shadow .2s, background .2s; } form.adc-student-form input[type=”text”]:focus, form.adc-student-form input[type=”email”]:focus, form.adc-student-form input[type=”number”]:focus{ outline:none; background:#fff; border-color:#3b82f6; box-shadow:0 0 0 3px rgba(59,130,246,.2); } / Button */
form.adc-student-form input[type=”submit”]{
display:inline-block;
padding:12px 20px;
border:0;
border-radius:10px;
background:linear-gradient(180deg,#3b82f6,#2563eb);
color:#fff;
font-weight:700;
font-size:14px;
cursor:pointer;
box-shadow:0 6px 14px rgba(37,99,235,.28);
transition:transform .08s ease, filter .2s ease;
}
form.adc-student-form input[type=”submit”]:hover{ filter:brightness(.98); }
form.adc-student-form input[type=”submit”]:active{ transform:translateY(1px) scale(.99); }

/* ===== Result card (same look as form) ===== / .student-result{ max-width:520px; margin:25px auto; padding:22px 24px; background:#fff; border-radius:16px; box-shadow:0 10px 24px rgba(0,0,0,.06); font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif; } .student-result h3{ margin:0 0 14px; font-size:18px; color:#2563eb; } / Make list look like stacked inputs */
.student-result ul{ list-style:none; padding:0; margin:0; }
.student-result li{ margin:0 0 14px; }
.student-result li strong{
display:block;
margin:0 0 6px;
font-size:13px;
font-weight:600;
color:#374151;
}
.student-result li .value{
display:block;
padding:12px 14px;
background:#f9fafb;
border:1px solid #e5e7eb;
border-radius:10px;
font-size:14px;
color:#111827;
line-height:1.2;
word-break:break-word;
}
‘;
}

// Keep last searched roll in the field after submit
$entered_roll = isset($_POST['roll_number']) ? sanitize_text_field($_POST['roll_number']) : '';

// --- Form markup ---
$output  = '<form method="post" class="adc-student-form" autocomplete="off">';
$output .= wp_nonce_field('adc_search_student_action', 'adc_search_student_nonce', true, false);
$output .= '<p>
        <label for="roll_number">Enter Roll Number</label>
        <input type="text" id="roll_number" name="roll_number" value="' . esc_attr($entered_roll) . '" inputmode="numeric" pattern="[A-Za-z0-9\-]+" required>
    </p>
    <p><input type="submit" name="adc_search_student" value="Search"></p>
</form>';

// --- Handle submit ---
if ( isset($_POST['adc_search_student']) ) {
    if ( ! isset($_POST['adc_search_student_nonce']) || ! wp_verify_nonce($_POST['adc_search_student_nonce'], 'adc_search_student_action') ) {
        $output .= '<p style="color:#b91c1c;max-width:520px;margin:10px auto;">Security check failed. Please retry.</p>';
    } else {
        $roll_number = trim($entered_roll);

        // Fetch record
        $student = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$table_name} WHERE roll_number = %s", $roll_number)
        );

        if ( $student ) {
            $output .= '<div class="student-result">';
            $output .= '<h3>Student Details</h3>';
            $output .= '<ul>';
            $output .= '<li><strong>First Name</strong><span class="value">' . esc_html($student->firstname) . '</span></li>';
            $output .= '<li><strong>Last Name</strong><span class="value">' . esc_html($student->lastname) . '</span></li>';
            $output .= '<li><strong>Class</strong><span class="value">' . esc_html($student->class) . '</span></li>';
            $output .= '<li><strong>Roll Number</strong><span class="value">' . esc_html($student->roll_number) . '</span></li>';
            $output .= '<li><strong>Email</strong><span class="value">' . esc_html($student->email) . '</span></li>';
            $output .= '<li><strong>Phone</strong><span class="value">' . esc_html($student->phone) . '</span></li>';
            $output .= '</ul>';
            $output .= '</div>';
        } else {
            $output .= '<p style="color:#b45309;background:#fff7ed;border:1px solid #fde68a;padding:10px;border-radius:8px;max-width:520px;margin:10px auto;">No student found with that roll number.</p>';
        }
    }
}

return $output;

}
add_shortcode(‘search_student’, ‘adc_search_student_form’);

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED – Do not modify or remove comment markers above or below:

if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) )
$uri = get_template_directory_uri() . ‘/rtl.css’;
return $uri;
}
endif;
add_filter( ‘locale_stylesheet_uri’, ‘chld_thm_cfg_locale_css’ );

Share on Facebook Share on Twitter