0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

APIのヘッダーからセッションと部門コードを受け取る

falseだと記事は表示しない

コードとセッションが返ったら、コードで絞り込んだ記事をwordpress側で表示

wordpress側では表示の制御だけ

  1. functions.php に追加
<?php
/**
 * ヘッダーから認証情報を取得する関数
 */
function get_auth_from_headers() {
    // ヘッダーから取得
    $session = null;
    $affiliation_code = null;
    
    // Apache/Nginx対応
    if (function_exists('getallheaders')) {
        $headers = getallheaders();
        $session = $headers['X-Session'] ?? null;
        $affiliation_code = $headers['X-Affiliation-Code'] ?? null;
    } else {
        // $_SERVERから取得(サーバー環境による)
        $session = $_SERVER['HTTP_X_SESSION'] ?? null;
        $affiliation_code = $_SERVER['HTTP_X_AFFILIATION_CODE'] ?? null;
    }
    
    return [
        'session' => $session,
        'affiliation_code' => $affiliation_code,
        'is_valid' => !empty($session) && !empty($affiliation_code)
    ];
}

/**
 * 認証済みの所属カテゴリー投稿を取得
 */
function get_affiliation_posts($affiliation_code) {
    // カテゴリーを取得
    $category = get_category_by_slug($affiliation_code);
    
    if (!$category) {
        return [];
    }
    
    // 投稿を取得
    $posts = get_posts(array(
        'category' => $category->term_id,
        'post_type' => 'post',
        'posts_per_page' => 10,
        'orderby' => 'date',
        'order' => 'DESC'
    ));
    
    return $posts;
}
  1. page-affiliation.php(テンプレート)
<?php
/**
 * Template Name: Affiliation Content Page
 * 
 * 所属別コンテンツを表示するテンプレート
 */
get_header();

// ヘッダーから認証情報を取得
$auth = get_auth_from_headers();
?>

<main id="main" class="site-main">
    <?php if (!$auth['is_valid']) : ?>
        <!-- 認証失敗: 記事を表示しない -->
        <div class="auth-error">
            <h3>⚠️ アクセスが制限されています</h3>
            <p>認証に失敗しました。アクセス権限を確認してください。</p>
        </div>
        
    <?php else : ?>
        <!-- 認証成功: 記事を表示 -->
        <?php
        $affiliation_code = $auth['affiliation_code'];
        $category = get_category_by_slug($affiliation_code);
        $posts = get_affiliation_posts($affiliation_code);
        ?>
        
        <?php if ($category && !empty($posts)) : ?>
            <div class="affiliation-content">
                <div class="affiliation-header">
                    <h2><?php echo esc_html($category->name); ?></h2>
                    <p>所属コード: <?php echo esc_html($affiliation_code); ?></p>
                </div>
                
                <div class="posts-list">
                    <?php foreach ($posts as $post) : setup_postdata($post); ?>
                        <article class="post-item">
                            <h3>
                                <a href="<?php echo get_permalink($post->ID); ?>">
                                    <?php echo esc_html($post->post_title); ?>
                                </a>
                            </h3>
                            <time><?php echo get_the_date('Y年m月d日', $post->ID); ?></time>
                            <?php if ($post->post_excerpt) : ?>
                                <p><?php echo esc_html($post->post_excerpt); ?></p>
                            <?php endif; ?>
                        </article>
                    <?php endforeach; wp_reset_postdata(); ?>
                </div>
            </div>
            
        <?php else : ?>
            <div class="no-content">
                <p>該当する所属のコンテンツが見つかりません。</p>
            </div>
        <?php endif; ?>
        
    <?php endif; ?>
</main>

<?php get_footer(); ?>
  1. スタイル(optional)
<?php
// functions.php に追加
function enqueue_affiliation_styles() {
    if (is_page_template('page-affiliation.php')) {
        wp_add_inline_style('your-theme-style', '
            .auth-error {
                padding: 2rem;
                text-align: center;
                background: #fee;
                border: 2px solid #c33;
                border-radius: 8px;
                margin: 2rem auto;
                max-width: 600px;
            }
            .affiliation-header {
                margin-bottom: 2rem;
                padding-bottom: 1rem;
                border-bottom: 2px solid #ddd;
            }
            .post-item {
                margin-bottom: 2rem;
                padding-bottom: 1rem;
                border-bottom: 1px solid #eee;
            }
        ');
    }
}
add_action('wp_enqueue_scripts', 'enqueue_affiliation_styles');

実装コード

1. functions.php に追加

<?php
/**
 * ヘッダーから認証情報を取得する関数
 */
function get_auth_from_headers() {
    // ヘッダーから取得
    $session = null;
    $affiliation_code = null;
    
    // Apache/Nginx対応
    if (function_exists('getallheaders')) {
        $headers = getallheaders();
        $session = $headers['X-Session'] ?? null;
        $affiliation_code = $headers['X-Affiliation-Code'] ?? null;
    } else {
        // $_SERVERから取得(サーバー環境による)
        $session = $_SERVER['HTTP_X_SESSION'] ?? null;
        $affiliation_code = $_SERVER['HTTP_X_AFFILIATION_CODE'] ?? null;
    }
    
    return [
        'session' => $session,
        'affiliation_code' => $affiliation_code,
        'is_valid' => !empty($session) && !empty($affiliation_code)
    ];
}

/**
 * 認証済みの所属カテゴリー投稿を取得
 */
function get_affiliation_posts($affiliation_code) {
    // カテゴリーを取得
    $category = get_category_by_slug($affiliation_code);
    
    if (!$category) {
        return [];
    }
    
    // 投稿を取得
    $posts = get_posts(array(
        'category' => $category->term_id,
        'post_type' => 'post',
        'posts_per_page' => 10,
        'orderby' => 'date',
        'order' => 'DESC'
    ));
    
    return $posts;
}

2. page-affiliation.php(テンプレート)

<?php
/**
 * Template Name: Affiliation Content Page
 * 
 * 所属別コンテンツを表示するテンプレート
 */
get_header();

// ヘッダーから認証情報を取得
$auth = get_auth_from_headers();
?>

<main id="main" class="site-main">
    <?php if (!$auth['is_valid']) : ?>
        <!-- 認証失敗: 記事を表示しない -->
        <div class="auth-error">
            <h3>⚠️ アクセスが制限されています</h3>
            <p>認証に失敗しました。アクセス権限を確認してください。</p>
        </div>
        
    <?php else : ?>
        <!-- 認証成功: 記事を表示 -->
        <?php
        $affiliation_code = $auth['affiliation_code'];
        $category = get_category_by_slug($affiliation_code);
        $posts = get_affiliation_posts($affiliation_code);
        ?>
        
        <?php if ($category && !empty($posts)) : ?>
            <div class="affiliation-content">
                <div class="affiliation-header">
                    <h2><?php echo esc_html($category->name); ?></h2>
                    <p>所属コード: <?php echo esc_html($affiliation_code); ?></p>
                </div>
                
                <div class="posts-list">
                    <?php foreach ($posts as $post) : setup_postdata($post); ?>
                        <article class="post-item">
                            <h3>
                                <a href="<?php echo get_permalink($post->ID); ?>">
                                    <?php echo esc_html($post->post_title); ?>
                                </a>
                            </h3>
                            <time><?php echo get_the_date('Y年m月d日', $post->ID); ?></time>
                            <?php if ($post->post_excerpt) : ?>
                                <p><?php echo esc_html($post->post_excerpt); ?></p>
                            <?php endif; ?>
                        </article>
                    <?php endforeach; wp_reset_postdata(); ?>
                </div>
            </div>
            
        <?php else : ?>
            <div class="no-content">
                <p>該当する所属のコンテンツが見つかりません。</p>
            </div>
        <?php endif; ?>
        
    <?php endif; ?>
</main>

<?php get_footer(); ?>

3. スタイル(optional)

<?php
// functions.php に追加
function enqueue_affiliation_styles() {
    if (is_page_template('page-affiliation.php')) {
        wp_add_inline_style('your-theme-style', '
            .auth-error {
                padding: 2rem;
                text-align: center;
                background: #fee;
                border: 2px solid #c33;
                border-radius: 8px;
                margin: 2rem auto;
                max-width: 600px;
            }
            .affiliation-header {
                margin-bottom: 2rem;
                padding-bottom: 1rem;
                border-bottom: 2px solid #ddd;
            }
            .post-item {
                margin-bottom: 2rem;
                padding-bottom: 1rem;
                border-bottom: 1px solid #eee;
            }
        ');
    }
}
add_action('wp_enqueue_scripts', 'enqueue_affiliation_styles');

動作フロー

外部システム(Flutter等)
    ↓
    HTTPリクエスト
    ヘッダー: X-Session, X-Affiliation-Code
    ↓
WordPress(page-affiliation.php)
    ↓
get_auth_from_headers() でヘッダー読み取り
    ↓
    ├─ false → 「アクセス制限」画面表示
    └─ true  → 記事を取得して表示

テスト方法

cURLでテスト

# 認証なし(エラー画面が表示される)
curl https://your-site.com/affiliation-page/

# 認証あり(記事が表示される)
curl -H "X-Session: test123" \
     -H "X-Affiliation-Code: sales" \
     https://your-site.com/affiliation-page/

まとめ

要素 処理内容
ヘッダー取得 get_auth_from_headers()X-SessionX-Affiliation-Code を取得
false の場合 エラー画面を表示、記事は表示しない
true の場合 $affiliation_code で記事を絞り込んで表示
JavaScript 不要(PHP側で完結)
REST API 不要(テンプレートで処理)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?