WordPress関数一覧【テーマ・プラグイン開発向け使用例付きリファレンス】

WordPressのテーマ・プラグイン開発で使用する関数を、カテゴリ別に整理した一覧表です。テンプレートタグ・ループ・条件分岐から、フック・REST API・セキュリティまで網羅しています。

各関数の使用例付きで、実務で必要な関数をすぐに探せるリファレンスとしてお使いください。詳細はWordPress Developer Resourcesで確認できます。


テーマテンプレート

テーマファイルの読み込みやページ構造に関する関数です。

関数名用途使用例
get_header()header.phpを読み込む<?php get_header(); ?>
get_footer()footer.phpを読み込む<?php get_footer(); ?>
get_sidebar()sidebar.phpを読み込む<?php get_sidebar(); ?>
get_template_part()テンプレートパーツを読み込む<?php get_template_part('template-parts/content', 'single'); ?>
get_search_form()検索フォームを表示<?php get_search_form(); ?>
body_class()bodyタグにクラスを追加<body <?php body_class(); ?>>
post_class()投稿要素にクラスを追加<article <?php post_class(); ?>>
wp_head()head内にフック出力を挿入<?php wp_head(); ?>(</head>直前に必須)
wp_footer()フッターにフック出力を挿入<?php wp_footer(); ?>(</body>直前に必須)
wp_body_open()bodyタグ直後にフック出力を挿入<?php wp_body_open(); ?>
language_attributes()htmlタグにlang属性を出力<html <?php language_attributes(); ?>>
bloginfo()サイト情報を表示<?php bloginfo('name'); ?> / bloginfo('charset')
get_bloginfo()サイト情報を取得(返り値)<?php echo get_bloginfo('description'); ?>
comments_template()コメントテンプレートを読み込む<?php comments_template(); ?>
wp_link_pages()ページ分割ナビゲーションを表示<?php wp_link_pages(); ?>

ループ・投稿取得

WordPressのメインループやカスタムクエリで投稿を取得する関数です。

関数名用途使用例
have_posts()投稿が存在するか確認<?php if (have_posts()) : ?>
the_post()次の投稿データをセットアップ<?php while (have_posts()) : the_post(); ?>
rewind_posts()ループを最初に巻き戻す<?php rewind_posts(); ?>
WP_Query(クラス)カスタムクエリで投稿を取得$q = new WP_Query(['post_type' => 'post', 'posts_per_page' => 5]);
get_posts()投稿を配列で取得$posts = get_posts(['category' => 3, 'numberposts' => 10]);
wp_reset_postdata()カスタムクエリ後にグローバル$postを復元<?php wp_reset_postdata(); ?>(WP_Query使用後に必須)
setup_postdata()ループ外で投稿データをセットアップ<?php setup_postdata($post); ?>
get_queried_object()現在のクエリ対象オブジェクトを取得$obj = get_queried_object();
query_posts()(非推奨)メインクエリを上書き(pre_get_postsを使用すべき)⚠️ 使用は避け、pre_get_postsフックを推奨

投稿データ表示

ループ内で投稿のタイトル・本文・日付・画像などを出力する関数です。

関数名用途使用例
the_title()投稿タイトルを表示<h1><?php the_title(); ?></h1>
get_the_title()投稿タイトルを取得(返り値)<?php echo get_the_title($post_id); ?>
the_content()投稿本文を表示<div><?php the_content(); ?></div>
the_excerpt()投稿の抜粋を表示<?php the_excerpt(); ?>
get_the_excerpt()投稿の抜粋を取得(返り値)$excerpt = get_the_excerpt();
the_permalink()投稿のURLを表示<a href="<?php the_permalink(); ?>">詳細</a>
get_permalink()投稿のURLを取得(返り値)$url = get_permalink($post_id);
the_post_thumbnail()アイキャッチ画像を表示<?php the_post_thumbnail('large'); ?>
get_the_post_thumbnail_url()アイキャッチ画像のURLを取得$img = get_the_post_thumbnail_url(null, 'full');
has_post_thumbnail()アイキャッチ画像の有無を判定<?php if (has_post_thumbnail()) : ?>
get_the_date()投稿日を取得<?php echo get_the_date('Y年n月j日'); ?>
get_the_modified_date()更新日を取得<?php echo get_the_modified_date('Y-m-d'); ?>
the_author()著者名を表示<?php the_author(); ?>
get_the_author_meta()著者のメタ情報を取得<?php echo get_the_author_meta('display_name'); ?>
get_author_posts_url()著者アーカイブURLを取得<a href="<?php echo get_author_posts_url($author_id); ?>">
get_the_category()投稿のカテゴリを取得$cats = get_the_category();
get_the_tags()投稿のタグを取得$tags = get_the_tags();
the_category()カテゴリリンクを表示<?php the_category(', '); ?>
the_tags()タグリンクを表示<?php the_tags('タグ: ', ', '); ?>
get_post_meta()カスタムフィールド値を取得$val = get_post_meta($post->ID, 'key', true);
get_comments_number()コメント数を取得<?php echo get_comments_number(); ?>
next_post_link()次の投稿リンクを表示<?php next_post_link('%link', '次の記事 &raquo;'); ?>
previous_post_link()前の投稿リンクを表示<?php previous_post_link('%link', '&laquo; 前の記事'); ?>

条件分岐タグ

現在表示しているページの種類を判定する関数です。テンプレートやフック内で表示の出し分けに使います。

関数名用途使用例
is_home()ブログ投稿一覧ページか判定<?php if (is_home()) : ?>
is_front_page()サイトのフロントページか判定<?php if (is_front_page()) : ?>
is_single()個別投稿ページか判定<?php if (is_single()) : ?>
is_page()固定ページか判定<?php if (is_page('about')) : ?>
is_singular()個別投稿・固定ページ・添付ファイルか判定<?php if (is_singular('post')) : ?>
is_archive()アーカイブページか判定<?php if (is_archive()) : ?>
is_category()カテゴリアーカイブか判定<?php if (is_category('news')) : ?>
is_tag()タグアーカイブか判定<?php if (is_tag()) : ?>
is_author()著者アーカイブか判定<?php if (is_author()) : ?>
is_search()検索結果ページか判定<?php if (is_search()) : ?>
is_404()404エラーページか判定<?php if (is_404()) : ?>
is_admin()管理画面か判定<?php if (is_admin()) : ?>
is_sticky()先頭固定投稿か判定<?php if (is_sticky()) : ?>
in_category()指定カテゴリに属するか判定<?php if (in_category('news')) : ?>
has_tag()指定タグが付いているか判定<?php if (has_tag('featured')) : ?>
is_post_type_archive()カスタム投稿タイプのアーカイブか判定<?php if (is_post_type_archive('product')) : ?>
is_tax()カスタムタクソノミーアーカイブか判定<?php if (is_tax('genre')) : ?>

フック(アクション/フィルター)

WordPressの動作を拡張・変更するための仕組みです。テーマやプラグイン開発の根幹となる機能です。

関数名用途使用例
add_action()アクションフックにコールバックを登録add_action('wp_enqueue_scripts', 'my_scripts');
do_action()アクションフックを実行do_action('my_custom_hook');
remove_action()登録済みアクションを解除remove_action('wp_head', 'wp_generator');
add_filter()フィルターフックにコールバックを登録add_filter('the_content', 'my_filter');
apply_filters()フィルターフックを実行し値を返す$val = apply_filters('my_filter', $value);
remove_filter()登録済みフィルターを解除remove_filter('the_content', 'wpautop');
has_action()アクションが登録されているか確認if (has_action('init', 'my_func')) { ... }
has_filter()フィルターが登録されているか確認if (has_filter('the_title')) { ... }

よく使うフック一覧

テーマやプラグイン開発で頻繁に使用するアクションフック・フィルターフックの一覧です。

フック名種類用途・実行タイミング
initアクションWordPress初期化後に実行。カスタム投稿タイプの登録等に使用
after_setup_themeアクションテーマの初期設定。add_theme_support等を実行
wp_enqueue_scriptsアクションフロントエンドのCSS/JSを登録・読み込み
admin_enqueue_scriptsアクション管理画面のCSS/JSを登録・読み込み
widgets_initアクションウィジェットエリアの登録
pre_get_postsアクションメインクエリを実行前に変更(query_postsの代替)
save_postアクション投稿保存時に実行。カスタムフィールドの保存等に使用
wp_headアクション<head>内に出力を追加
wp_footerアクション</body>直前に出力を追加
template_redirectアクションテンプレート読み込み前にリダイレクト等を実行
rest_api_initアクションREST APIルートを登録
send_headersアクションHTTPヘッダー送信時に実行。セキュリティヘッダーの追加等
the_contentフィルター投稿本文を出力前に加工
the_titleフィルター投稿タイトルを出力前に加工
excerpt_lengthフィルター抜粋の文字数を変更
upload_mimesフィルターアップロード可能なファイルタイプを追加・制限
body_classフィルターbodyタグのクラスを追加・変更
document_title_partsフィルターページの<title>タグを加工
wp_robotsフィルターrobotsメタタグの内容を変更

CSS/JS読み込み

スタイルシートやJavaScriptを正しい方法で読み込む関数です。wp_enqueue_scriptsフック内で使用します。

関数名用途使用例
wp_enqueue_style()CSSファイルを読み込みwp_enqueue_style('main', get_stylesheet_uri());
wp_enqueue_script()JSファイルを読み込みwp_enqueue_script('app', get_theme_file_uri('/js/app.js'), [], '1.0', true);
wp_register_style()CSSを登録(読み込みは別途enqueue)wp_register_style('lightbox', $url);
wp_register_script()JSを登録(読み込みは別途enqueue)wp_register_script('chart', $url, [], '4.0', true);
wp_dequeue_style()登録済みCSSの読み込みを解除wp_dequeue_style('plugin-style');
wp_dequeue_script()登録済みJSの読み込みを解除wp_dequeue_script('jquery-migrate');
wp_localize_script()JSにPHPの値を渡すwp_localize_script('app', 'myData', ['ajaxUrl' => admin_url('admin-ajax.php')]);
wp_add_inline_style()インラインCSSを追加wp_add_inline_style('main', ':root{--primary:#333}');
wp_add_inline_script()インラインJSを追加wp_add_inline_script('app', 'const API="/wp-json"', 'before');
get_stylesheet_uri()style.cssのURLを取得wp_enqueue_style('theme', get_stylesheet_uri());
get_theme_file_uri()テーマ内ファイルのURLを取得get_theme_file_uri('/assets/css/main.min.css')

カスタム投稿タイプ・タクソノミー

独自の投稿タイプやカテゴリ構造を作成する関数です。initフック内で使用します。

関数名用途使用例
register_post_type()カスタム投稿タイプを登録register_post_type('product', ['public' => true, 'label' => '商品']);
register_taxonomy()カスタムタクソノミーを登録register_taxonomy('genre', 'product', ['label' => 'ジャンル']);
add_meta_box()編集画面にメタボックスを追加add_meta_box('seo', 'SEO設定', 'render_seo_box', 'post');
update_post_meta()カスタムフィールドを保存・更新update_post_meta($post_id, '_price', $value);
delete_post_meta()カスタムフィールドを削除delete_post_meta($post_id, '_old_key');
add_shortcode()ショートコードを登録add_shortcode('btn', function($atts) { return '<a>...</a>'; });
register_sidebar()ウィジェットエリアを登録register_sidebar(['name' => 'サイドバー', 'id' => 'sidebar-1']);
dynamic_sidebar()ウィジェットエリアを表示<?php dynamic_sidebar('sidebar-1'); ?>
is_active_sidebar()ウィジェットが登録されているか確認<?php if (is_active_sidebar('sidebar-1')) : ?>
wp_nav_menu()ナビゲーションメニューを表示wp_nav_menu(['theme_location' => 'primary']);
register_nav_menus()メニューの位置を登録register_nav_menus(['primary' => 'メインメニュー']);

セキュリティ・サニタイズ

出力のエスケープやnonce検証など、セキュリティ対策に関する関数です。テーマ・プラグイン開発で必須の知識です。

関数名用途使用例
esc_html()HTML出力時のエスケープ<?php echo esc_html($user_input); ?>
esc_attr()HTML属性値のエスケープ<input value="<?php echo esc_attr($val); ?>">
esc_url()URLのエスケープ<a href="<?php echo esc_url($url); ?>">
esc_js()JavaScript出力のエスケープ<script>var x = '<?php echo esc_js($val); ?>';</script>
esc_textarea()textarea内容のエスケープ<textarea><?php echo esc_textarea($content); ?></textarea>
wp_kses_post()投稿で許可されたHTMLタグのみ通すecho wp_kses_post($html);
sanitize_text_field()テキスト入力のサニタイズ$clean = sanitize_text_field($_POST['name']);
sanitize_email()メールアドレスのサニタイズ$email = sanitize_email($_POST['email']);
absint()正の整数に変換$id = absint($_GET['id']);
wp_nonce_field()nonce隠しフィールドを出力(CSRF対策)wp_nonce_field('save_settings', '_my_nonce');
wp_verify_nonce()nonceを検証if (!wp_verify_nonce($_POST['_my_nonce'], 'save_settings')) { die(); }
check_admin_referer()管理画面でのnonce検証check_admin_referer('delete_item');
wp_check_password()パスワードのハッシュ照合wp_check_password($input, $hash);

オプション・テーマ設定

wp_optionsテーブルの値の取得・保存やテーマ機能の有効化に関する関数です。

関数名用途使用例
get_option()オプション値を取得$val = get_option('blogname');
update_option()オプション値を保存・更新update_option('my_setting', $value);
delete_option()オプション値を削除delete_option('old_setting');
add_theme_support()テーマ機能を有効化add_theme_support('post-thumbnails'); / 'title-tag' / 'html5'
get_theme_mod()テーマカスタマイザーの設定を取得$color = get_theme_mod('primary_color', '#333');
set_theme_mod()テーマカスタマイザーの設定を保存set_theme_mod('primary_color', '#3460fb');
get_custom_logo()カスタムロゴのHTML取得<?php echo get_custom_logo(); ?>
set_post_thumbnail_size()アイキャッチ画像のサイズを設定set_post_thumbnail_size(1200, 630, true);
add_image_size()カスタム画像サイズを追加add_image_size('card', 400, 300, true);

URL・パス取得

サイトやテーマのURL・ファイルパスを取得する関数です。ハードコーディングを避けるために使用します。

関数名用途使用例
home_url()サイトのトップページURLを取得echo home_url('/');https://example.com/
site_url()WordPressインストール先URLを取得echo site_url('/wp-login.php');
admin_url()管理画面のURLを取得echo admin_url('admin-ajax.php');
get_template_directory_uri()親テーマディレクトリのURLを取得echo get_template_directory_uri() . '/assets/img/logo.png';
get_stylesheet_directory_uri()子テーマ(現在のテーマ)ディレクトリのURLを取得echo get_stylesheet_directory_uri() . '/style.css';
get_template_directory()親テーマのサーバーパスを取得require get_template_directory() . '/inc/setup.php';
get_theme_file_uri()テーマ内ファイルのURLを取得(子テーマ優先)echo get_theme_file_uri('/assets/js/app.js');
get_theme_file_path()テーマ内ファイルのパスを取得(子テーマ優先)require get_theme_file_path('/inc/helpers.php');
wp_upload_dir()アップロードディレクトリの情報を取得$dir = wp_upload_dir(); echo $dir['baseurl'];
content_url()wp-contentディレクトリのURLを取得echo content_url('/plugins/');
plugins_url()プラグインディレクトリのURLを取得echo plugins_url('assets/style.css', __FILE__);

ユーザー・権限

ログイン状態の判定やユーザー情報の取得に関する関数です。

関数名用途使用例
is_user_logged_in()ユーザーがログイン中か判定<?php if (is_user_logged_in()) : ?>
current_user_can()現在のユーザーが権限を持っているか判定if (current_user_can('edit_posts')) { ... }
get_current_user_id()ログイン中ユーザーのIDを取得$uid = get_current_user_id();
wp_get_current_user()ログイン中ユーザーのオブジェクトを取得$user = wp_get_current_user(); echo $user->display_name;
get_userdata()ユーザーIDからユーザー情報を取得$user = get_userdata($id);
get_avatar()ユーザーのGravatarを取得echo get_avatar($user_id, 64);
wp_logout_url()ログアウトURLを取得<a href="<?php echo wp_logout_url(home_url()); ?>">ログアウト</a>
wp_login_url()ログインURLを取得<a href="<?php echo wp_login_url(); ?>">ログイン</a>

REST API

WordPressのREST APIを拡張するための関数です。ヘッドレスCMSやフロントエンド連携に使用します。

関数名用途使用例
register_rest_route()カスタムREST APIエンドポイントを登録register_rest_route('my/v1', '/items', ['methods' => 'GET', 'callback' => 'get_items']);
rest_api_initREST APIルート登録用のアクションフックadd_action('rest_api_init', 'register_my_routes');
wp_send_json_success()成功レスポンスをJSON形式で返すwp_send_json_success(['message' => '保存しました']);
wp_send_json_error()エラーレスポンスをJSON形式で返すwp_send_json_error(['message' => 'エラー'], 400);
rest_ensure_response()WP_REST_Responseオブジェクトを保証return rest_ensure_response($data);
wp_remote_get()外部APIにGETリクエスト$res = wp_remote_get('https://api.example.com/data');
wp_remote_post()外部APIにPOSTリクエスト$res = wp_remote_post($url, ['body' => $data]);
wp_remote_retrieve_body()リモートリクエストのレスポンスボディを取得$body = wp_remote_retrieve_body($res);

投稿の作成・更新・削除

プログラムから投稿を操作する関数です。プラグイン開発やデータ移行で使用します。

関数名用途使用例
wp_insert_post()新しい投稿を作成$id = wp_insert_post(['post_title' => 'タイトル', 'post_status' => 'publish']);
wp_update_post()既存の投稿を更新wp_update_post(['ID' => 123, 'post_title' => '新タイトル']);
wp_delete_post()投稿を削除wp_delete_post($post_id, true);(trueで完全削除)
wp_trash_post()投稿をゴミ箱に移動wp_trash_post($post_id);
wp_set_post_terms()投稿にカテゴリ・タグを設定wp_set_post_terms($post_id, [3, 5], 'category');
set_post_thumbnail()アイキャッチ画像を設定set_post_thumbnail($post_id, $attachment_id);
wp_get_attachment_image()メディア画像のHTMLを取得echo wp_get_attachment_image($id, 'medium');
wp_get_attachment_url()メディアファイルのURLを取得$url = wp_get_attachment_url($attachment_id);

その他の便利な関数

リダイレクト、メール送信、デバッグなど、実務で役立つ関数です。

関数名用途使用例
wp_redirect()リダイレクトwp_redirect(home_url('/')); exit;
wp_safe_redirect()安全なリダイレクト(同一サイト内のみ)wp_safe_redirect(admin_url()); exit;
wp_die()処理を中断してメッセージを表示wp_die('アクセスが拒否されました', 403);
wp_mail()メールを送信wp_mail('to@example.com', '件名', '本文');
wp_schedule_event()定期実行イベントを登録(WP-Cron)wp_schedule_event(time(), 'daily', 'my_daily_task');
wp_next_scheduled()次の定期実行予定時刻を取得if (!wp_next_scheduled('my_daily_task')) { ... }
set_transient()一時キャッシュを保存set_transient('api_data', $data, HOUR_IN_SECONDS);
get_transient()一時キャッシュを取得$data = get_transient('api_data');
delete_transient()一時キャッシュを削除delete_transient('api_data');
__() / _e()翻訳対応テキストの取得/表示echo __('保存しました', 'my-theme');
wp_parse_args()引数をデフォルト値とマージ$args = wp_parse_args($args, $defaults);
wp_is_mobile()モバイルデバイスか判定if (wp_is_mobile()) { ... }

関連記事

HTMLタグ一覧【フロントエンド向け使用例付き】

CSSプロパティ一覧【カテゴリ別・使用例付きリファレンス】

コードの差分を比較したいときは Diff Checker(コード比較ツール) もご活用ください。