Viết plugin để trên wordpress để khi dùng shortcode [GSSP sheetid="
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.
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.
Hãy tạo một plugin WordPress tự chế biến như sau:
wp-content/plugins/gssp-plugin
gssp-plugin.php
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');
.zip
và tải lên WordPress Dashboard trong phần Plugins.[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é!