プラグインに頼らないWordPress運用へ。セキュリティリスクを排除し、SEO・AI検索に強いFAQ&参考資料を独自実装する方法

WordPressサイトの改ざんや不正アクセスといったセキュリティインシデントの多くは、コア本体ではなく「サードパーティ製プラグインの脆弱性」を突かれて発生しています。
手軽に機能を追加できるプラグインは便利である反面、導入数が増えるほど「攻撃対象領域(アタックサーフェス)」が広がり、管理コストや表示速度の低下を招きます。
今回は、プラグイン依存からの脱却をテーマに、プラグインを一切使わずに「FAQ(よくある質問)の動的追加枠」と「参考資料枠」をWordPress標準機能だけで独自実装し、構造化データ(JSON-LD)を自動出力する手法を解説します。
なぜ今「脱プラグイン」が求められるのか?
プラグインが最大のセキュリティホールになる
WordPressにおけるインシデントの大部分は、長期間メンテナンスが放置されたプラグインや、アップデート直後のゼロデイ脆弱性が侵入経路となっています。 「ちょっとしたカスタムフィールドを追加したいから」「FAQアコーディオンを設置したいから」と安易にプラグインを増やし続ける運用は、サイトの乗っ取りやバックドア設置のリスクを自ら抱え込むことと同義です。
プラグインを減らす3つの実利
攻撃対象領域の最小化
コードを自社管理下に置くことで、外部プラグインのサプライチェーン攻撃リスクを遮断。
Core Web Vitals(表示速度)の向上
不要な共通CSSやJavaScriptの外部リクエストを削ぎ落とし、スコアの悪化を防ぐ。
互換性トラブルの撲滅
WordPressコアやPHPのメジャーバージョンアップ時に発生するプラグイン起因のエラー・画面崩れを回避。
実装の狙い:SEO・AI検索(AIO)への対応
単にプラグインを削るだけでなく、マーケティング視点でも強力な成果を出す設計にしています。
FAQPage 構造化データによる機械可読性の担保
HTML上のテキストだけでなく、JSON-LDとして「問い」と「答え」を明示的にマークアップすることで、検索クローラーやAI(Google AI Overviews、LLM等)がコンテンツの文脈を100%正確に解釈できます。
E-E-A-T(信頼性)を高める参考資料枠
記事末尾に出典元や参照文献を提示することは、Googleの品質評価(E-E-A-T)における「信頼性(Trust)」を補強し、AIが回答の引用元として採用する際の決定打となります。
運用の平準化
本文エディタとは別に固定の入力枠を設けることで、ライターによる表記ブレやマークアップミスを防ぎ、サイト全体で均一なデータ構造を維持します。
実装手順(コード解説)
Step 1: 管理画面に動的入力メタボックスを作成(functions.php)
行の追加・削除が可能なFAQ欄と、HTML記述に対応した参考資料欄を functions.php だけで構築します。
/**
* メタボックスの登録
*/
function custom_register_article_addons_metabox() {
// FAQ用メタボックス
add_meta_box(
'custom_faq_metabox',
'よくある質問(FAQ)管理',
'custom_render_faq_metabox',
'post',
'normal',
'default'
);
// 参考資料用メタボックス
add_meta_box(
'custom_references_metabox',
'参考資料・参考文献',
'custom_render_references_metabox',
'post',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'custom_register_article_addons_metabox');
/**
* FAQ入力フォーム描画(Vanilla JSで動的追加)
*/
function custom_render_faq_metabox($post) {
wp_nonce_field('custom_faq_save_nonce', 'custom_faq_nonce');
$faq_data = get_post_meta($post->ID, '_custom_faq_data', true);
if (!is_array($faq_data)) {
$faq_data = [];
}
?>
<div id="custom-faq-wrapper">
<div id="custom-faq-items">
<?php foreach ($faq_data as $index => $item) : ?>
<div class="custom-faq-item" style="background:#fff;border:1px solid #ccd0d4;padding:12px;margin-bottom:12px;border-radius:4px;">
<span class="faq-item-num" style="font-weight:bold;color:#555;">#<?php echo esc_html($index + 1); ?></span>
<div style="margin:8px 0 4px;"><label style="font-weight:600;">質問 (Q):</label></div>
<input type="text" name="custom_faq[<?php echo esc_attr($index); ?>][question]" value="<?php echo esc_attr($item['question'] ?? ''); ?>" style="width:100%;">
<div style="margin:8px 0 4px;"><label style="font-weight:600;">回答 (A):</label></div>
<textarea name="custom_faq[<?php echo esc_attr($index); ?>][answer]" rows="3" style="width:100%;"><?php echo esc_textarea($item['answer'] ?? ''); ?></textarea>
<button type="button" class="button button-link-delete remove-faq-btn" style="margin-top:6px;">行を削除</button>
</div>
<?php endforeach; ?>
</div>
<button type="button" class="button button-primary" id="add-faq-btn">+ FAQ項目を追加</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const wrapper = document.getElementById('custom-faq-items');
const addBtn = document.getElementById('add-faq-btn');
if (!addBtn) return;
addBtn.addEventListener('click', function () {
const count = wrapper.querySelectorAll('.custom-faq-item').length;
const itemDiv = document.createElement('div');
itemDiv.className = 'custom-faq-item';
itemDiv.style.cssText = 'background:#fff;border:1px solid #ccd0d4;padding:12px;margin-bottom:12px;border-radius:4px;';
itemDiv.innerHTML = `
<span class="faq-item-num" style="font-weight:bold;color:#555;">#${count + 1}</span>
<div style="margin:8px 0 4px;"><label style="font-weight:600;">質問 (Q):</label></div>
<input type="text" name="custom_faq[${count}][question]" value="" style="width:100%;">
<div style="margin:8px 0 4px;"><label style="font-weight:600;">回答 (A):</label></div>
<textarea name="custom_faq[${count}][answer]" rows="3" style="width:100%;"></textarea>
<button type="button" class="button button-link-delete remove-faq-btn" style="margin-top:6px;">行を削除</button>
`;
wrapper.appendChild(itemDiv);
});
wrapper.addEventListener('click', function (e) {
if (e.target && e.target.classList.contains('remove-faq-btn')) {
e.target.closest('.custom-faq-item').remove();
wrapper.querySelectorAll('.custom-faq-item').forEach((item, index) => {
item.querySelector('.faq-item-num').textContent = '#' + (index + 1);
});
}
});
});
</script>
<?php
}
/**
* 参考資料入力フォーム描画
*/
function custom_render_references_metabox($post) {
wp_nonce_field('custom_references_save_nonce', 'custom_references_nonce');
$references_html = get_post_meta($post->ID, '_custom_references_html', true);
?>
<p style="margin-bottom:8px;color:#666;font-size:13px;">※ <code><ul><li>...</li></ul></code> 形式で直接入力してください。</p>
<textarea name="custom_references_html" rows="5" style="width:100%;font-family:monospace;"><?php echo esc_textarea($references_html); ?></textarea>
<?php
}
/**
* データの安全な保存処理
*/
function custom_save_article_addons($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
// FAQの保存
if (isset($_POST['custom_faq_nonce']) && wp_verify_nonce($_POST['custom_faq_nonce'], 'custom_faq_save_nonce')) {
if (isset($_POST['custom_faq']) && is_array($_POST['custom_faq'])) {
$clean_faq = [];
foreach ($_POST['custom_faq'] as $item) {
$q = sanitize_text_field($item['question'] ?? '');
$a = sanitize_textarea_field($item['answer'] ?? '');
if ($q !== '' && $a !== '') {
$clean_faq[] = ['question' => $q, 'answer' => $a];
}
}
!empty($clean_faq) ? update_post_meta($post_id, '_custom_faq_data', $clean_faq) : delete_post_meta($post_id, '_custom_faq_data');
} else {
delete_post_meta($post_id, '_custom_faq_data');
}
}
// 参考資料の保存
if (isset($_POST['custom_references_nonce']) && wp_verify_nonce($_POST['custom_references_nonce'], 'custom_references_save_nonce')) {
if (isset($_POST['custom_references_html'])) {
$raw_ref = trim($_POST['custom_references_html']);
$raw_ref !== '' ? update_post_meta($post_id, '_custom_references_html', wp_kses_post($raw_ref)) : delete_post_meta($post_id, '_custom_references_html');
}
}
}
add_action('save_post', 'custom_save_article_addons');
Step 2: 構造化データ(JSON-LD)の自動出力(functions.php)
データが存在する記事のみ、<head> 内へ FAQPage スキーマを自動生成します。
/**
* FAQ構造化データの自動出力
*/
function custom_output_faq_schema() {
if (!is_single()) return;
$faq_list = get_post_meta(get_the_ID(), '_custom_faq_data', true);
if (empty($faq_list) || !is_array($faq_list)) return;
$entities = [];
foreach ($faq_list as $faq) {
$entities[] = [
'@type' => 'Question',
'name' => wp_strip_all_tags($faq['question']),
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => wp_strip_all_tags($faq['answer']),
],
];
}
$schema = [
'@context' => 'https://schema.org',
'@type' => 'FAQPage',
'mainEntity' => $entities,
];
echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . '</script>' . "\n";
}
add_action('wp_head', 'custom_output_faq_schema');
Step 3: フロント表示テンプレート(single.php)
本文末尾(the_content() の直後など)に配置します。入力データがある場合のみセクションが描画されます。
<?php
// FAQセクション
$faq_list = get_post_meta(get_the_ID(), '_custom_faq_data', true);
if (!empty($faq_list) && is_array($faq_list)) : ?>
<section class="c-article-faq">
<h2 class="c-article-faq__title">よくある質問(FAQ)</h2>
<dl class="c-article-faq__list">
<?php foreach ($faq_list as $faq) : ?>
<dt class="c-article-faq__q"><?php echo esc_html($faq['question']); ?></dt>
<dd class="c-article-faq__a"><?php echo nl2br(esc_html($faq['answer'])); ?></dd>
<?php endforeach; ?>
</dl>
</section>
<?php endif; ?>
<?php
// 参考資料セクション
$references_content = get_post_meta(get_the_ID(), '_custom_references_html', true);
if (!empty($references_content)) : ?>
<section class="c-article-ref">
<h2 class="c-article-ref__title">参考資料・文献</h2>
<div class="c-article-ref__body">
<?php echo $references_content; ?>
</div>
</section>
<?php endif; ?>
Step 4: デザインCSS(style.css)
テーマ既存の見出しスタイル(下線や左側の装飾バー)が混ざらないよう、リセット処理を組み込んだスタイルのサンプルです。こちらはサイトデザインに合わせて調整してください。
/* --- FAQセクション --- */
.c-article-faq,
.c-article-ref {
margin: 3rem 0;
padding: 2rem 1.5rem;
background-color: #f7f9fb;
border-radius: 8px;
border: 1px solid #e2e8f0;
}
/* 既存テーマの見出し装飾をリセットし、統一感ある見出しに上書き */
.c-article-faq h2.c-article-faq__title,
.c-article-ref h2.c-article-ref__title {
border: none !important;
background: transparent !important;
padding: 0 0 0.85rem 0 !important;
margin: 0 0 2rem 0 !important;
font-size: 1.35rem !important;
font-weight: 700 !important;
color: #1e3a8a !important;
text-align: center !important;
position: relative !important;
display: block !important;
}
.c-article-faq h2.c-article-faq__title::before,
.c-article-ref h2.c-article-ref__title::before {
content: none !important;
display: none !important;
}
.c-article-faq h2.c-article-faq__title::after,
.c-article-ref h2.c-article-ref__title::after {
content: "" !important;
position: absolute !important;
bottom: 0 !important;
left: 50% !important;
transform: translateX(-50%) !important;
width: 48px !important;
height: 3px !important;
background-color: #2563eb !important;
border-radius: 2px !important;
display: block !important;
}
/* FAQ カードスタイル */
.c-article-faq__list { margin: 0; padding: 0; }
.c-article-faq__q {
position: relative;
font-size: 1.05rem;
font-weight: 700;
line-height: 1.6;
color: #1f2937;
background-color: #fff;
padding: 1rem 1rem 1rem 3.25rem;
margin: 1rem 0 0 0;
border-radius: 6px 6px 0 0;
border: 1px solid #e5e7eb;
border-bottom: none;
}
.c-article-faq__q:first-of-type { margin-top: 0; }
.c-article-faq__q::before {
content: "Q";
position: absolute;
left: 1rem;
top: 50%;
transform: translateY(-50%);
width: 1.75rem;
height: 1.75rem;
background-color: #1e3a8a;
color: #fff;
font-size: 0.85rem;
font-weight: 700;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
.c-article-faq__a {
position: relative;
font-size: 0.95rem;
line-height: 1.8;
color: #374151;
background-color: #fff;
padding: 1.25rem 1rem 1.25rem 3.25rem;
margin: 0 0 1rem 0;
border-radius: 0 0 6px 6px;
border: 1px solid #e5e7eb;
}
.c-article-faq__a::before {
content: "A";
position: absolute;
left: 1rem;
top: 1.25rem;
width: 1.75rem;
height: 1.75rem;
background-color: #e02424;
color: #fff;
font-size: 0.85rem;
font-weight: 700;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
/* --- 参考資料スタイル --- */
.c-article-ref__body ul { margin: 0; padding-left: 1.5rem; }
.c-article-ref__body li { font-size: 0.95rem; line-height: 1.8; color: #374151; margin-bottom: 0.5rem; }
.c-article-ref__body a { color: #1e3a8a; text-decoration: underline; }
セキュリティ実装上のポイント
独自実装にあたっては、WordPress本体のセキュリティ規約に準拠した防御策を必ず組み込んでいます。
- NonceによるCSRF対策:
wp_nonce_field()とwp_verify_nonce()を用いて、外部からの不正な自動ポストリクエストを遮断。 - 権限チェック:
current_user_can('edit_post', $post_id)で、投稿権限を持たないユーザーによるデータ改変を防止。 - 入力・出力のサニタイズ:
- テキスト入力は
sanitize_text_field() - 改行ありテキストは
sanitize_textarea_field() - 参考資料のHTMLは危険なスクリプト(XSS)を排除する
wp_kses_post()で無害化。 - 出力時は
esc_html()、esc_attr()でエスケープを徹底。
- テキスト入力は
プラグインの「引き算」がサイトの資産価値を高める
「とりあえずプラグインを入れる」という選択は、開発初期の工数を減らせる反面、将来的なセキュリティインシデントのリスクやバージョンアップ時の保守コストを増大させます。
今回紹介したように、動的な入力欄や構造化データの出力は、WordPressの標準APIを組み合わせるだけで数行のコードで実装可能です。 外部依存を排除した堅牢な土台を作りつつ、検索エンジンと生成AIの双方に評価されるコンテンツ基盤を構築していくことが、これからのWordPress運用における重要なスタンダードとなります。
投稿者プロフィール
- 代表者
-
東海大学工学部を卒業後、東芝情報システム株式会社(旧グループ会社含む)に入社。半導体(SRAM)の開発チームにて、Unix環境でのPerlを用いた業務自動化プログラムの開発など、エンジニアとしての確かな基礎と緻密な論理的思考力を培う。
その後、東証上場企業である株式会社ザッパラスへ。Web・モバイルの最前線で、数多くのユーザーに愛されるコンテンツ制作業務に従事。ここで「ユーザー目線に立った魅力的なWebコンテンツの企画・制作ノウハウ」を深く学ぶ。
開発・システム側から見た「堅牢なロジック」と、制作・ユーザー側から見た「伝わるコンテンツ」の双方を実務で経験した強みを活かし、フリーランスとして独立。
現在は、ウイングアーク1st株式会社の「データのじかん」運営チームに参画するなど、大手・中小企業のWeb運営・開発パートナーとして活動。完全在宅でありながら、固定IPの完備や厳格なセキュリティポリシーの遵守を徹底し、企業のインフラや本番環境を安全に支える「チームの一員」として高い信頼を得ています。
見た目の美しさはもちろんのこと、内部の構造やWordPressのカスタマイズ、運用・セキュリティまでを見据えた、技術的バックボーンのある高品質なWebサイト・システム制作をワンストップでご提供しています。
