⚠️ บทความนี้มีลิงก์อ้างอิง AsiaGB.com — เราได้รับค่าตอบแทนเมื่อคุณคลิกผ่าน Disclosure: นโยบายเปิดเผย
WordPress REST API ตัวอักษร API ขาด

คู่มือ WordPress REST API ฉบับสมบูรณ์ 2026

Complete WordPress REST API Guide — How to use endpoints, authentication, and build headless CMS

WordPress REST API คืออะไร

WordPress REST API เป็นวิธีสำหรับให้แอปพลิเคชันภายนอก (เช่น Mobile App, React Frontend, หรือบริการอื่น) สามารถเข้าถึง WordPress ได้โดยไม่ต้องสัมผัส Admin Dashboard หรือเขียนผ่าน HTML Forms เหมือนแบบเดิม แทนที่จะใช้หน้า HTML ปกติ REST API ใช้ JSON (JavaScript Object Notation) เพื่อส่งข้อมูลไป-มาระหว่างเซิร์ฟเวอร์และไคลเอนต์

REST API ถูกเพิ่มเข้ามาใน WordPress 4.7 (ปี 2017) แล้ว ตั้งแต่นั้นมาจนถึงปัจจุบัน ได้เป็นส่วนสำคัญของ WordPress core ที่ได้รับการอัปเดตและปรับปรุงอย่างต่อเนื่อง การใช้ REST API ช่วยให้สามารถสร้าง Headless CMS ได้ — นั่นคือการแยก Content Management System (สำหรับผู้ดูแลระบบ) ออกจาก Frontend Presentation (สิ่งที่เห็นบนจอ)

ก่อน WordPress REST API มีอยู่ หากต้องการให้ Mobile App อ่านข้อมูลจาก WordPress จะต้องใช้ XML-RPC ซึ่งมีความยุ่งยากและช้ากว่า REST API มากมาย ตอนนี้สามารถดึงข้อมูลด้วย HTTP GET requests ที่ตรงไปตรงมา ทำให้การพัฒนา Mobile App, React/Vue Frontend, หรือ Next.js sites จาก WordPress ทำได้ง่ายขึ้นอย่างมาก

Endpoint หลักของ REST API

WordPress REST API จัดเรียง URL endpoints ตามแบบ RESTful standard ทั้งหมดมีรูปแบบเดียวกันคือ /wp-json/ ตามด้วย namespace และ route หลัก endpoint ของ WordPress ก่อตั้งขึ้นที่ /wp-json/wp/v2/ ตัวอักษร v2 หมายถึง version 2 ของ WordPress REST API

หลัก endpoints ของ WordPress REST API ได้แก่:

นอกจากนี้ plugins อื่น ๆ สามารถเพิ่ม Custom Endpoints ได้ เช่น WooCommerce เพิ่ม /wp-json/wc/v3/ สำหรับ e-commerce functions ตัวอย่างเช่น /wp-json/wc/v3/products เพื่อดึงข้อมูลสินค้า

ทั้งหมด endpoints นี้ยอมรับ HTTP methods ต่างกัน:

การดึงข้อมูล Posts และ Pages

การดึงข้อมูล Blog Posts เป็นการใช้งาน REST API ที่ง่ายที่สุด ไม่ต้องพิสูจน์ตัวตน และสามารถดำเนินการได้จาก JavaScript ที่รันใน Browser หรือจาก Server-side code

ตัวอย่าง: ดึงข้อมูลสูงสุด 10 posts ที่เผยแพร่ (published):

GET https://example.com/wp-json/wp/v2/posts?per_page=10&status=publish

Response จะเป็น JSON array ที่มีข้อมูล posts แต่ละตัวมี:

{
  "id": 123,
  "date": "2026-06-29T10:30:00",
  "title": {
    "rendered": "WordPress REST API Guide"
  },
  "content": {
    "rendered": "<p>...</p>"
  },
  "excerpt": {
    "rendered": "Learn how to use..."
  },
  "featured_media": 456,
  "categories": [1, 2],
  "author": 1,
  "status": "publish"
}

สามารถใช้ query parameters ต่างๆ เพื่อกำหนด filter:

หากต้องการดึงข้อมูล post เดียว (by ID):

GET https://example.com/wp-json/wp/v2/posts/123

ข้อมูลจะละเอียดกว่า GET /posts มี full content, meta fields, และข้อมูลอื่นๆ ที่ไม่ส่งออกมาในรายการ (list) view

การพิสูจน์ตัวตนกับ REST API

สำหรับการดำเนินการที่เปลี่ยนแปลงข้อมูล (POST, PUT, DELETE) หรือการเข้าถึง private posts/pages จำเป็นต้องพิสูจน์ตัวตนว่าเป็นผู้ใช้ WordPress ที่มีสิทธิ์ WordPress รองรับหลายวิธี:

1. Application Passwords (ที่แนะนำ)

WordPress 5.6+ มี Application Passwords ซึ่งเป็นรหัสผ่านพิเศษสำหรับการเข้าถึง API เท่านั้น ผู้ใช้สามารถสร้างได้ใน WordPress Admin > Users > Your Profile > Application Passwords

การใช้งาน: ส่ง Authorization header ด้วย Basic Authentication:

curl -X POST https://example.com/wp-json/wp/v2/posts \
  -H "Content-Type: application/json" \
  -H "Authorization: Basic $(echo -n 'username:app-password' | base64)" \
  -d '{
    "title": "My New Post",
    "content": "Post content here",
    "status": "publish"
  }'

2. OAuth 2.0 (สำหรับ Third-party Apps)

WordPress ที่ enable OAuth2 plugin (เช่น OAuth2 Server) สามารถให้ third-party apps ขอ access token โดยไม่เปิดเผย password ของผู้ใช้ ผลงานกำลังสร้างการรองรับ OAuth2 native ในไป WordPress core

3. JWT (JSON Web Tokens)

JWT plugins เช่น Simple JWT Authentication ใช้ token-based authentication ใช้งานได้ดีสำหรับ Mobile Apps และ Single Page Applications (SPAs)

สร้าง แก้ไข และลบข้อมูล

เมื่อได้รับการพิสูจน์ตัวตนแล้ว สามารถสร้าง posts, pages, comments และข้อมูลอื่นๆ ได้

สร้าง Post ใหม่:

POST https://example.com/wp-json/wp/v2/posts
Authorization: Basic base64(username:app-password)
Content-Type: application/json

{
  "title": "My First REST API Post",
  "content": "<p>This post was created via REST API</p>",
  "excerpt": "A short excerpt",
  "featured_media": 789,
  "categories": [1, 2],
  "tags": [5, 6],
  "status": "draft"
}

ข้อมูลที่ส่งได้แก่:

แก้ไข Post ที่มีอยู่:

PUT https://example.com/wp-json/wp/v2/posts/123
Authorization: Basic base64(username:app-password)
Content-Type: application/json

{
  "title": "Updated Title",
  "content": "<p>Updated content</p>",
  "status": "publish"
}

สำหรับการแก้ไข บาง fields ที่ไม่ต้องการแก้อาจละไว้ได้ — มีเพียง fields ที่ต้องการเปลี่ยนแปลง

ลบ Post:

DELETE https://example.com/wp-json/wp/v2/posts/123?force=true
Authorization: Basic base64(username:app-password)

flag ?force=true หมายถึงการลบอย่างถาวร หากไม่ใส่ post จะถูกย้ายไปถังขยะแทน

สร้าง Custom Endpoints

นอกเหนือจาก built-in endpoints WordPress ยอมให้ plugins และ theme developers สร้าง Custom Endpoints เพื่อให้ functionality ที่เฉพาะเจาะจง

ตัวอย่างโค้ด PHP สำหรับสร้าง custom endpoint:

add_action( 'rest_api_init', function() {
  register_rest_route( 'myapp/v1', '/greet', array(
    'methods'  => 'GET',
    'callback' => 'my_greet_callback',
    'permission_callback' => '__return_true'
  ) );
} );

function my_greet_callback( $request ) {
  $name = $request->get_param( 'name' );
  return new WP_REST_Response( array(
    'greeting' => 'Hello, ' . $name . '!'
  ), 200 );
}

จากนั้น สามารถเรียกใช้:

GET https://example.com/wp-json/myapp/v1/greet?name=John

Custom endpoints มีประโยชน์สำหรับ:

WordPress Headless CMS Architecture

Headless CMS หมายถึง WordPress ที่ไม่มี Frontend (ไม่มี theme UI) แต่ใช้เฉพาะเพื่อจัดการเนื้อหา (Content Management) ผ่าน Admin Dashboard หลังจากนั้น Frontend ถูกสร้างขึ้นโดย React, Vue, Next.js, หรือเทคโนโลยี web framework อื่นๆ

ประโยชน์ของ Headless WordPress:

สถาปัตยกรรม Headless WordPress:

┌─────────────────────────────┐
│  WordPress Admin Dashboard  │
│  (Content Management)       │
├─────────────────────────────┤
│   WordPress Database        │
│   REST API Endpoints        │
└──────────────────┬──────────┘
                   │
        ┌──────────┼──────────┬──────────┐
        │          │          │          │
     ┌──▼──┐   ┌──▼──┐   ┌──▼──┐   ┌──▼──┐
     │React│   │Next │   │Mobile│   │IOS  │
     │Web  │   │.js  │   │App   │   │App  │
     └─────┘   └─────┘   └──────┘   └─────┘

ตัวอย่าง: React Frontend ดึงข้อมูลจาก WordPress REST API

// React component
import { useEffect, useState } from 'react';

export default function PostList() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    fetch('https://example.com/wp-json/wp/v2/posts')
      .then(res => res.json())
      .then(data => setPosts(data));
  }, []);

  return (
    <div>
      {posts.map(post => (
        <article key={post.id}>
          <h2>{post.title.rendered}</h2>
          <p>{post.excerpt.rendered}</p>
        </article>
      ))}
    </div>
  );
}

ความปลอดภัย REST API

REST API เปิดให้ใครก็ได้เข้าถึง (โดยค่าเริ่มต้น สำหรับ read-only) ดังนั้นจึงจำเป็นต้องคำนึงถึงความปลอดภัย

1. ปิด REST API สำหรับข้อมูลที่ไม่จำเป็น

หาก WordPress ใช้เฉพาะเป็น Headless CMS หรือไม่ต้องการให้ใครเข้าถึง user information อาจสามารถปิด endpoints ที่ไม่ต้องการได้

// Hide user endpoints
add_filter( 'rest_endpoints', function( $endpoints ) {
  if ( ! is_user_logged_in() ) {
    unset( $endpoints['/wp/v2/users'] );
    unset( $endpoints['/wp/v2/users/(?P<id>[\\d]+)'] );
  }
  return $endpoints;
} );

2. ใช้ Application Passwords แล้วเปลี่ยนรหัสผ่าน

หากต้องการ write access ให้ใช้ Application Passwords ที่สร้างจากทีด UI แทนการใช้ password จริงของผู้ใช้ สามารถยกเลิกการเข้าถึงได้โดยการลบ app password นั้นโดยไม่ต้องเปลี่ยน password จริง

3. Rate Limiting

เซิร์ฟเวอร์ควรตั้ง rate limiting เพื่อป้องกัน brute-force attacks และ DDoS attacks ตัวอย่าง:

// Limit to 100 requests per minute per IP
add_filter( 'rest_throttle_check', function() {
  $ip = $_SERVER['REMOTE_ADDR'];
  $key = 'rest_limit_' . $ip;
  $count = get_transient( $key );
  if ( $count >= 100 ) {
    return new WP_Error( 'rest_throttled', 'Too many requests' );
  }
  set_transient( $key, $count + 1, 60 );
} );

4. HTTPS เท่านั้น

เลือก WordPress ที่หยุด HTTP ดับตรวจสอบแล้วว่า hosting plan ใช้ HTTPS (SSL/TLS) ไม่เช่นนั้น application passwords อาจถูกสกัดกั้นได้

5. ตรวจสอบ Permissions

endpoints ที่ต้องการสิทธิ์ (capability) ต้องตรวจสอบแล้วว่าผู้ใช้มีสิทธิ์นั้นจริง:

register_rest_route( 'myapp/v1', '/admin-only', array(
  'callback'             => 'my_admin_callback',
  'permission_callback'  => function() {
    return current_user_can( 'manage_options' );
  }
) );

ตัวอย่าง Use Case จริง

1. Mobile App สำหรับ Blog

Native iOS/Android app สามารถดึงข้อมูล posts จาก WordPress REST API แล้วแสดงในรูปแบบ app-native ผู้ใช้สามารถตั้งเพื่อการแจ้งเตือน (push notifications) เมื่อมีบทความใหม่ได้

2. Next.js Blog เร่งด้วย Static Generation

Next.js `getStaticProps` สามารถดึงข้อมูล posts จาก WordPress ตอน build time แล้วสร้าง static HTML pages ด้วย Incremental Static Regeneration (ISR) เวบไซต์จะรวดเร็วมากเพราะ HTML ถูกสร้างเสร็จแล้ว ขณะเดียวกัน page ก็ยังสามารถอัปเดตได้เมื่อมีเนื้อหาใหม่

3. Form Submission Logging

Contact form หรือ newsletter signup form บนเว็บไซต์ที่สร้างด้วย React สามารถส่งข้อมูลไปเก็บใน WordPress custom post type ผ่าน REST API แล้วแอดมินสามารถดูข้อมูล submissions ทั้งหมดใน WordPress Admin Dashboard

// React form submission
async function handleSubmit(e) {
  e.preventDefault();
  const response = await fetch(
    'https://example.com/wp-json/myapp/v1/submissions',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
      },
      body: JSON.stringify({
        name: form.name,
        email: form.email,
        message: form.message
      })
    }
  );
  const result = await response.json();
  alert('Thank you for your message!');
}

4. Multi-site Publishing dengan Jamstack

บริษัทที่มีหลายเว็บไซต์สามารถใช้ WordPress ตัวเดียวเป็น content hub แล้วให้เว็บไซต์แต่ละตัว (สร้างด้วย Hugo, Gatsby, 11ty, เป็นต้น) ดึงข้อมูลจาก REST API ทำให้สามารถจัดการเนื้อหาจากที่เดียวได้

คำถามที่ถามบ่อย

Q: WordPress REST API แตกต่างจาก XML-RPC อย่างไร?
REST API เป็นมาตรฐาน modern standard ที่ใช้ JSON แทน XML ทำให้เร็วกว่า เบากว่า และเข้าใจง่ายกว่า XML-RPC ถูกจำกัดเพราะใช้ HTTP POST เพียงอย่างเดียว แต่ REST API ใช้ HTTP verbs ที่เหมาะสม (GET, POST, PUT, DELETE)
Q: จะเพิ่ม custom fields/ACF เข้า REST API ได้อย่างไร?
สำหรับ built-in meta fields ใช้ register_meta() ด้วย `'show_in_rest' => true` สำหรับ Advanced Custom Fields (ACF) ให้เปิด ACF Settings > Custom Fields > Show in REST API
Q: เหมาะสมไหมที่จะปิด REST API ทั้งหมด?
ไม่แนะนำ REST API ถูกใช้โดย WordPress blocks editor เอง (Gutenberg) หากปิดตัวเต็มอาจทำให้ admin ใช้ block editor ไม่ได้ ดีกว่าปิดเฉพาะ endpoints ที่ไม่จำเป็น
Q: Application Passwords ปลอดภัยหรือไม่?
ปลอดภัยกว่าการใช้ password จริง เพราะสามารถยกเลิกได้โดยไม่ต้องเปลี่ยน password หลัก แม้จะส่งผ่าน HTTP Header ต้องใช้ HTTPS เสมอไป
Q: สามารถตั้งค่า CORS สำหรับ REST API ได้หรือไม่?
ได้ WordPress 5.9+ รองรับ CORS ตามค่าเริ่มต้น ในเวอร์ชั่นเก่า สามารถใช้ plugin เช่น CORS headers หรือแก้ไข .htaccess ด้วยตนเอง
แนะนำAsiaGB.com — Web Hosting & VPS ที่เราใช้และแนะนำ เซิร์ฟเวอร์ในไทยและสิงคโปร์ สตอเรจ SSD จัดการผ่าน DirectAdmin พร้อมทีม Support ภาษาไทย 24 ชั่วโมง uptime 99%

AsiaGB.com — hosting & VPS we use and recommend: TH/SG servers, SSD storage, DirectAdmin, 24h Thai support, 99% uptime.

เยี่ยมชม AsiaGB →
ที่มา & วิธีการ: บทความนี้เขียนจากการศึกษา WordPress Official REST API Documentation, Codex, และ Handbook ตรวจสอบกับ WordPress 6.4 LTS บนเซิร์ฟเวอร์ที่ใช้ AsiaGB.com (DirectAdmin, PHP 8.1, MySQL 8.0)