Skip to content

Instantly share code, notes, and snippets.

@sigwyg
Last active June 4, 2018 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigwyg/e8077be9292026c1c9d4545ef336b28f to your computer and use it in GitHub Desktop.
Save sigwyg/e8077be9292026c1c9d4545ef336b28f to your computer and use it in GitHub Desktop.
WordPress: Contact Form 7の送信データをGoogle Sheetに追記する
vendor/
client_secret.json
composer.phar

TL;DR

  1. Contact Form 7 のメール送信完了フックから送信情報を取得
  2. 取得した送信情報をGoogle Sheetに投げる

Required

  • PHP 5.4以上。CLIとJSON extensionを含む。
  • Composer
    • $ php composer.phar require google/apiclient:^2.0
  • Googleアカウント
    • Sheetに書き込み可能な client_secret.json が必要。
    • 予めGoogle Developerコンソールにてサービスアカウントキーを取得しておくこと

参考: PHP Quickstart | Sheets API | Google Developpers

<?php
/**
* Google Sheetへリクエストを投げる
* - パス指定だけどうにかしたい
*/
require_once dirname(__FILE__).'/vendor/autoload.php';
class AppendGoogleSheet {
protected $service;
protected $spreadsheetId;
public function __construct() {
// 環境によって更新するシートを変える
$home_url = get_home_url();
switch ($home_url) {
case 'https://example.com':
$this->spreadsheetId = '[SheetID_for_production]';
break;
case 'https://stg.example.com':
$this->spreadsheetId = '[SheetID_for_staging]';
break;
default:
$this->spreadsheetId = '[SheetID_for_development]';
}
// Get the API client and construct the service object.
$client = $this->getClient();
$this->service = new Google_Service_Sheets($client);
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
public function getClient() {
$credentialsPath = dirname(__FILE__).'/client_secret.json';
$accessToken = json_decode(file_get_contents($credentialsPath), true);
$client = new Google_Client();
$client->setApplicationName('Pontact PHP');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig($accessToken);
return $client;
}
/**
* Appends values to a spreadsheet.
* @param array $values
*/
public function appendSheet(array $values) {
$value = new Google_Service_Sheets_ValueRange();
$value->setValues(array( 'values' => $values));
$range = "シート1";
$params = array("valueInputOption" => "USER_ENTERED");
$result = $this->service->spreadsheets_values->append($this->spreadsheetId, $range, $value, $params);
}
}
?>
<?php
require_once dirname(__FILE__).'/class-append_google_sheet.php';
/**
* Contact Form 7 の送信完了イベント
*/
add_action('wpcf7_mail_sent', 'hooked_contactform_sent', 10, 1);
function hooked_contactform_sent(){
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$formdata = $submission->get_posted_data();
/**
* Google Sheetへリクエストを投げる
*/
date_default_timezone_set('Asia/Tokyo');
$values = array(
'example.com', // プロジェクト
$formdata['your-name'],
$formdata['your-message'],
'', // 所属
$formdata['your-email'],
'', // 対象名
'', // 対象URL
date("Y/m/d H:i:s"), // 日時
$_SERVER['REMOTE_ADDR'] // IP
);
$sheet = new AppendGoogleSheet();
$sheet->appendSheet($values);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment