Câu hỏi:

Viết plugin để trên wordpress để khi dùng shortcode [GSSP sheetid="" range="" format="" transpose="" heading="" ] thì tạo search form để tìm kiếm dữ liệu từ google sheet. Trong đó:


Tạo một plugin WordPress có thể thực hiện tất cả các thao tác thần kỳ với dữ liệu từ Google Sheets bạn muốn chắc chắn sẽ gây ấn tượng mạnh! Hãy cùng tôi xem điều đó có thể thực hiện như thế nào. Bạn sẽ cần kiến thức về WordPress, Google Sheets API, và một chút kỳ vọng để thực hiện phép màu này.

Bước 1: Thiết lập dự án Google API

Trước khi bắt đầu, bạn cần tạo một dự án trên Google Cloud Platform để lấy API Key và OAuth Credential.

  1. Tạo một tài khoản Google Cloud
  2. Kích hoạt Google Sheets API
  3. Tạo thông tin xác thực API

Bước 2: Cài đặt Plugin WordPress

Hãy tạo một plugin WordPress tự chế biến như sau:

  1. Tạo thư mục trong thư mục plugins của WordPress: wp-content/plugins/gssp-plugin
  2. Tạo một tệp PHP mới trong đó, ví dụ: gssp-plugin.php

Nội dung tệp gssp-plugin.php

<?php
/*
Plugin Name: Google Sheets Search Plugin
Description: Plugin để tìm kiếm dữ liệu từ Google Sheets bằng shortcode.
Version: 1.0
Author: Trí Tuệ Nhân Tạo
*/

function gssp_shortcode($atts) {
    $atts = shortcode_atts(array(
        'sheetid' => '',
        'range' => 'Sheet1!A1:E100',
        'format' => 'horizon',
        'transpose' => 'false',
        'heading' => 'Kết quả truy xuất',
    ), $atts, 'gssp');

    $sheet_id = esc_attr($atts['sheetid']);
    $range = esc_attr($atts['range']);
    $format = esc_attr($atts['format']);
    $transpose = esc_attr($atts['transpose']) === 'true';
    $heading = esc_attr($atts['heading']);

    $output = '<form method="post">';
    $output .= '<input type="text" name="search_term" placeholder="Nhập từ khóa">';
    $output .= '<input type="submit" name="search" value="Tìm kiếm">';
    $output .= '</form>';

    if (isset($_POST['search'])) {
        $search_term = sanitize_text_field($_POST['search_term']);
        $data = fetch_google_sheet_data($sheet_id, $range);

        if ($data) {
            $filtered_data = process_data_based_on_format($data, $search_term, $format, $transpose);
            $output .= display_table($filtered_data, $heading);
        } else {
            $output .= '<p>Không thể truy xuất dữ liệu!</p>';
        }
    }

    return $output;
}

function fetch_google_sheet_data($sheet_id, $range) {
    $api_key = 'YOUR_GOOGLE_API_KEY'; // Thay bằng API Key thực tế của bạn
    $url = "https://sheets.googleapis.com/v4/spreadsheets/$sheet_id/values/$range?key=$api_key";
    $response = wp_remote_get($url);

    if (is_wp_error($response)) return false;

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    return isset($data['values']) ? $data['values'] : false;
}

function process_data_based_on_format($data, $search_term, $format, $transpose) {
    // Tùy vào yêu cầu format và transpose, bạn có thể xử lý dữ liệu để phù hợp
    if ($format === 'horizon') {
        $data = array_filter($data, function($row) use ($search_term) {
            return $row[0] === $search_term;
        });

        if ($transpose) {
            $data = transpose_data($data);
        }
    } else if ($format === 'vertical') {
        $data = array_map(null, ...$data);
        $data = array_filter($data, function($column) use ($search_term) {
            return $column[0] === $search_term;
        });

        if ($transpose) {
            $data = transpose_data($data);
        }
    }
    return $data;
}

function transpose_data($data) {
    // Transpose dữ liệu
    array_unshift($data, null);
    return call_user_func_array('array_map', $data);
}

function display_table($data, $heading) {
    $table = '<h3>' . esc_html($heading) . '</h3>';
    $table .= '<table border="1"><tbody>';
    foreach ($data as $row) {
        $table .= '<tr>';
        foreach ($row as $cell) {
            $table .= '<td>' . esc_html($cell) . '</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</tbody></table>';

    return $table;
}

add_shortcode('GSSP', 'gssp_shortcode');

Hướng dẫn sử dụng:

  1. Cài đặt Plugin: Nén thư mục chứa file PHP thành .zip và tải lên WordPress Dashboard trong phần Plugins.
  2. Kích hoạt Plugin: Sau khi tải lên, kích hoạt plugin.
  3. Sử dụng Shortcode: Sử dụng shortcode [GSSP sheetid="your_sheet_id" range="Sheet1!A1:E100" format="horizon" transpose="false" heading="Result"] trong trang hoặc bài đăng của bạn.

Và thế là bạn đã sẵn sàng để tìm kiếm trên Google Sheets từ WordPress như một bậc thầy dữ liệu rồi đó! Đừng quên ghé thăm trituenhantao.io để cập nhật thêm những kiến thức thú vị nhé!


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử