{"id":170,"date":"2026-05-16T08:00:23","date_gmt":"2026-05-15T23:00:23","guid":{"rendered":"https:\/\/shimaken.com\/schnauzer\/170\/"},"modified":"2026-05-18T00:34:31","modified_gmt":"2026-05-17T15:34:31","slug":"wordpress-rest-api-x-api","status":"publish","type":"post","link":"https:\/\/shimaken.com\/schnauzer\/170\/","title":{"rendered":"WordPress REST API \u00d7 X API \u9023\u643a\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f"},"content":{"rendered":"<h1>WordPress REST API \u00d7 X API \u9023\u643a\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f<\/h1>\n<h2>\u5c0e\u5165<\/h2>\n<p>WordPress \u30d6\u30ed\u30b0\u306e\u6295\u7a3f\u3092\u81ea\u52d5\u7684\u306b X (\u65e7 Twitter) \u306b\u30b7\u30a7\u30a2\u3057\u305f\u3044\u3002\u305d\u3046\u8003\u3048\u308b\u7ba1\u7406\u8005\u306f\u591a\u3044\u3067\u3059\u304c\u3001\u5b9f\u88c5\u65b9\u6cd5\u3068\u306a\u308b\u3068\u9078\u629e\u80a2\u304c\u5206\u304b\u308c\u307e\u3059\u3002\u30d7\u30e9\u30b0\u30a4\u30f3\u983c\u307f\u306b\u3059\u308b\u304b\u3001API \u3092\u76f4\u63a5\u53e9\u304f\u304b\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001WordPress REST API \u3068 X API \u3092\u76f4\u7d50\u3055\u305b\u3001\u30d6\u30ed\u30b0\u66f4\u65b0\u6642\u306b\u81ea\u52d5\u6295\u7a3f\u3059\u308b\u4ed5\u7d44\u307f\u3092\u5b9f\u969b\u306b\u69cb\u7bc9\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u305d\u306e\u904e\u7a0b\u3067\u898b\u3048\u305f\u8ab2\u984c\u3001\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u3001\u305d\u3057\u3066\u610f\u5916\u306a\u843d\u3068\u3057\u7a74\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<h2>\u7d50\u8ad6\u304b\u3089\u66f8\u304d\u307e\u3059<\/h2>\n<p>WordPress \u306e REST API \u3068 X API \u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30d7\u30e9\u30b0\u30a4\u30f3\u4e0d\u8981\u306a\u81ea\u52d5\u30b7\u30a7\u30a2\u6a5f\u80fd\u304c\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u8a8d\u8a3c\u5468\u308a\u3068 API \u30ec\u30fc\u30c8\u5236\u9650\u3078\u306e\u5bfe\u7b56\u304c\u5fc5\u9808\u3067\u3059\u3002\u672c\u8a18\u4e8b\u3067\u306f\u691c\u8a3c\u74b0\u5883\u3067\u306e\u5b9f\u88c5\u30d5\u30ed\u30fc\u3001\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u3001\u305d\u3057\u3066\u672c\u756a\u904b\u7528\u3078\u306e\u6ce8\u610f\u70b9\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002<\/p>\n<h2>\u4eee\u8aac\u3068\u76ee\u7684<\/h2>\n<p><strong>\u4eee\u8aac<\/strong><\/p>\n<p>WordPress \u306e REST API \u30d5\u30c3\u30af\u3068 X API \u306e OAuth 2.0 \u8a8d\u8a3c\u3092\u7d44\u307f\u5408\u308f\u305b\u308c\u3070\u3001\u30ab\u30b9\u30bf\u30e0\u30b3\u30fc\u30c9\uff08\u30d7\u30e9\u30b0\u30a4\u30f3\u5316\uff09\u3067\u81ea\u52d5\u30b7\u30a7\u30a2\u304c\u5b9f\u88c5\u3067\u304d\u3001\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u30d7\u30e9\u30b0\u30a4\u30f3\u306e\u4f9d\u5b58\u3092\u6e1b\u3089\u305b\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3002<\/p>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<ol>\n<li>\u65b0\u898f\u6295\u7a3f\u6642\u306b\u81ea\u52d5\u7684\u306b X \u3078\u6295\u7a3f\u3092\u901a\u77e5<\/li>\n<li>API \u8a8d\u8a3c\u30d5\u30ed\u30fc\u3068\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306e\u78ba\u8a8d<\/li>\n<li>\u30ec\u30fc\u30c8\u5236\u9650\u30fb\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306e\u73fe\u5b9f\u7684\u306a\u5bfe\u5fdc\u6cd5\u3092\u63a2\u308b<\/li>\n<\/ol>\n<h2>\u691c\u8a3c\u74b0\u5883\u30fb\u524d\u63d0\u6761\u4ef6<\/h2>\n<p><strong>\u74b0\u5883\u60c5\u5831<\/strong><\/p>\n<ul>\n<li>WordPress 6.4.3 (\u6700\u65b0\u5b89\u5b9a\u7248)<\/li>\n<li>X API v2 (Elevated Access \u3092\u60f3\u5b9a)<\/li>\n<li>PHP 8.1 \u4ee5\u4e0a<\/li>\n<li>wp-cli \u5c0e\u5165\u6e08\u307f<\/li>\n<li>\u691c\u8a3c\u65e5\u6642: 2025\u5e74 5 \u6708\u5b9f\u65bd<\/li>\n<\/ul>\n<p><strong>\u524d\u63d0<\/strong><\/p>\n<ul>\n<li>X Developer Portal \u3067\u306e\u30a2\u30d7\u30ea\u767b\u9332\u5b8c\u4e86<\/li>\n<li>API \u30ad\u30fc\u3068 Bearer Token \u306e\u53d6\u5f97\u6e08\u307f<\/li>\n<li>WordPress \u306e\u30c6\u30fc\u30de\u307e\u305f\u306f\u30ab\u30b9\u30bf\u30e0\u30d7\u30e9\u30b0\u30a4\u30f3\u306b\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u53ef\u80fd\u306a\u74b0\u5883<\/li>\n<\/ul>\n<h2>\u5b9f\u88c5\u306e\u57fa\u672c\u30d5\u30ed\u30fc<\/h2>\n<h3>WordPress REST API \u3067\u6295\u7a3f\u30d5\u30c3\u30af\u3092\u6355\u6349<\/h3>\n<p>\u307e\u305a WordPress \u5074\u3067\u3001\u65b0\u898f\u6295\u7a3f\u306e\u4f5c\u6210\u30fb\u66f4\u65b0\u6642\u306b\u4f55\u304b\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u8d77\u52d5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002REST API \u7d4c\u7531\u3067\u6295\u7a3f\u304c\u4f5c\u3089\u308c\u308b\u5834\u5408\u3001<code>rest_insert_post<\/code> \u30d5\u30c3\u30af\u304c\u767a\u706b\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-php\">\/\/ functions.php \u307e\u305f\u306f custom-plugin.php\nadd_action('rest_insert_post', 'share_to_x_api', 10, 3);\n\nfunction share_to_x_api($post, $request, $creating) {\n    \/\/ \u65b0\u898f\u4f5c\u6210\u6642\u306e\u307f\u5b9f\u884c\n    if (!$creating) {\n        return;\n    }\n\n    \/\/ \u7279\u5b9a\u306e\u30ab\u30c6\u30b4\u30ea\u3084\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u307f\u5bfe\u8c61\u306b\u3059\u308b\n    if ($post-&gt;post_status !== 'publish') {\n        return;\n    }\n\n    \/\/ X API \u3078\u901a\u77e5\u3092\u9001\u308b\n    send_to_x($post);\n}\n<\/code><\/pre>\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f\u3001<strong>REST API \u7d4c\u7531\u3067\u306e\u4f5c\u6210\u306e\u307f<\/strong> \u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b\u3053\u3068\u3067\u3059\u3002WordPress \u306e\u7ba1\u7406\u753b\u9762\u304b\u3089\u6295\u7a3f\u3057\u305f\u5834\u5408\u306f\u3001\u3053\u306e\u30d5\u30c3\u30af\u304c\u767a\u706b\u3057\u307e\u305b\u3093\uff08\u305d\u306e\u5834\u5408\u306f <code>publish_post<\/code> \u306a\u3069\u306e\u5225\u30d5\u30c3\u30af\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\u3002<\/p>\n<h3>X API \u3078\u306e\u6295\u7a3f\u9001\u4fe1<\/h3>\n<p>\u6b21\u306b\u3001X API v2 \u306e POST \/tweets \u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u53e9\u304d\u307e\u3059\u3002\u8a8d\u8a3c\u306f Bearer Token \u3092\u4f7f\u3046\u65b9\u5f0f\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-php\">function send_to_x($post) {\n    $post_title = $post-&gt;post_title;\n    $post_url = get_permalink($post-&gt;ID);\n\n    \/\/ X \u306b\u6295\u7a3f\u3059\u308b\u30c6\u30ad\u30b9\u30c8\uff08280\u5b57\u4ee5\u5185\uff09\n    $tweet_text = sprintf(\n        &quot;\ud83d\udcdd \u65b0\u3057\u3044\u8a18\u4e8b\u3092\u516c\u958b\u3057\u307e\u3057\u305f\\n\\n%s\\n\\n%s&quot;,\n        $post_title,\n        $post_url\n    );\n\n    \/\/ 280\u5b57\u8d85\u904e\u30c1\u30a7\u30c3\u30af\n    if (mb_strlen($tweet_text) &gt; 280) {\n        $tweet_text = mb_substr($post_title, 0, 50) . &quot;...\\n\\n&quot; . $post_url;\n    }\n\n    $x_api_url = 'https:\/\/api.twitter.com\/2\/tweets';\n    $bearer_token = get_option('x_api_bearer_token'); \/\/ \u74b0\u5883\u5909\u6570\u304b\u3089\u53d6\u5f97\u63a8\u5968\n\n    $response = wp_remote_post($x_api_url, [\n        'headers' =&gt; [\n            'Authorization' =&gt; 'Bearer ' . $bearer_token,\n            'Content-Type' =&gt; 'application\/json',\n        ],\n        'body' =&gt; wp_json_encode([\n            'text' =&gt; $tweet_text,\n        ]),\n        'timeout' =&gt; 10,\n    ]);\n\n    if (is_wp_error($response)) {\n        error_log('X API Error: ' . $response-&gt;get_error_message());\n        return false;\n    }\n\n    $status_code = wp_remote_retrieve_response_code($response);\n    if ($status_code !== 201) {\n        $body = wp_remote_retrieve_body($response);\n        error_log('X API returned ' . $status_code . ': ' . $body);\n        return false;\n    }\n\n    return true;\n}\n<\/code><\/pre>\n<p><strong>\u30c1\u30a7\u30c3\u30af\u30dd\u30a4\u30f3\u30c8<\/strong><\/p>\n<ul>\n<li>Bearer Token \u306f\u74b0\u5883\u5909\u6570\u307e\u305f\u306f\u30bb\u30ad\u30e5\u30a2\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u30c6\u30fc\u30d6\u30eb\u306b\u4fdd\u7ba1<\/li>\n<li><code>wp_remote_post()<\/code> \u3067 HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\uff08curl \u3088\u308a\u3082 WordPress \u6a19\u6e96\uff09<\/li>\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9 201 \u3067\u6210\u529f\u5224\u5b9a<\/li>\n<\/ul>\n<h3>\u8a8d\u8a3c\u30c8\u30fc\u30af\u30f3\u306e\u7ba1\u7406<\/h3>\n<p>Bearer Token \u3092\u76f4\u63a5\u30b3\u30fc\u30c9\u306b\u57cb\u3081\u8fbc\u3080\u306e\u306f\u5371\u967a\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3092\u63a8\u5968\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1: .env \u30d5\u30a1\u30a4\u30eb\uff08WP-CLI \u3084 Composer \u74b0\u5883\u3067\u63a8\u5968\uff09<\/strong><\/p>\n<pre><code>X_API_BEARER_TOKEN=AAAABNRjWwEBBA...\n<\/code><\/pre>\n<p>functions.php \u3067\u8aad\u307f\u8fbc\u307f:<\/p>\n<pre><code class=\"language-php\">if (file_exists(__DIR__ . '\/.env')) {\n    $env_vars = parse_ini_file(__DIR__ . '\/.env');\n    define('X_API_BEARER_TOKEN', $env_vars['X_API_BEARER_TOKEN'] ?? '');\n}\n<\/code><\/pre>\n<p><strong>\u65b9\u6cd5 2: wp-config.php \u306b\u5b9a\u7fa9<\/strong><\/p>\n<pre><code class=\"language-php\">define('X_API_BEARER_TOKEN', getenv('X_API_BEARER_TOKEN'));\n<\/code><\/pre>\n<p><strong>\u65b9\u6cd5 3: WordPress \u30aa\u30d7\u30b7\u30e7\u30f3\u30c6\u30fc\u30d6\u30eb\uff08\u7ba1\u7406\u753b\u9762\u304b\u3089\u5909\u66f4\u53ef\u80fd\uff09<\/strong><\/p>\n<pre><code class=\"language-php\">update_option('x_api_bearer_token', 'AAAA...', false);\n$token = get_option('x_api_bearer_token');\n<\/code><\/pre>\n<p>\u65b9\u6cd5 3 \u306f\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30ea\u30b9\u30af\u304c\u3042\u308b\u305f\u3081\u3001\u672c\u756a\u74b0\u5883\u3067\u306f\u74b0\u5883\u5909\u6570\u63a8\u5968\u3067\u3059\u3002<\/p>\n<h2>\u30ec\u30fc\u30c8\u5236\u9650\u3078\u306e\u5bfe\u5fdc<\/h2>\n<p>X API v2 \u306b\u306f \u30ec\u30fc\u30c8\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u901a\u5e38\u30011 \u30e6\u30fc\u30b6\u30fc\u5f53\u305f\u308a 15 \u5206\u9593\u306b 300 \u30ea\u30af\u30a8\u30b9\u30c8\u3067\u3059\u3002\u30d6\u30ed\u30b0 1 \u3064\u7a0b\u5ea6\u306a\u3089\u554f\u984c\u306b\u306a\u308a\u307e\u305b\u3093\u304c\u3001\u8907\u6570\u30c9\u30e1\u30a4\u30f3\u3092\u904b\u7528\u3059\u308b\u5834\u5408\u306f\u5de5\u592b\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<h3>\u30ad\u30e5\u30fc\u30a4\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u306e\u5c0e\u5165<\/h3>\n<p>\u6295\u7a3f\u3092\u5373\u5ea7\u306b X \u3078\u9001\u3089\u305a\u3001\u4e00\u5ea6 WordPress \u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u306b\u4fdd\u5b58\u3057\u3001\u5225\u30bf\u30b9\u30af\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-php\">add_action('rest_insert_post', 'queue_x_share', 10, 3);\n\nfunction queue_x_share($post, $request, $creating) {\n    if (!$creating || $post-&gt;post_status !== 'publish') {\n        return;\n    }\n\n    \/\/ \u30e1\u30bf\u30c7\u30fc\u30bf\u306b\u4fdd\u5b58\uff08\u307e\u3060\u9001\u4fe1\u3057\u3066\u3044\u306a\u3044\u72b6\u614b\uff09\n    update_post_meta($post-&gt;ID, 'x_share_pending', true);\n    update_post_meta($post-&gt;ID, 'x_share_timestamp', current_time('timestamp'));\n}\n\n\/\/ \u5b9a\u671f\u5b9f\u884c\uff08WP-Cron \u307e\u305f\u306f \u5916\u90e8 cron\uff09\nadd_action('wp_scheduled_x_share', 'process_x_share_queue');\n\nfunction process_x_share_queue() {\n    $pending_posts = get_posts([\n        'meta_key' =&gt; 'x_share_pending',\n        'meta_value' =&gt; true,\n        'numberposts' =&gt; 5, \/\/ 1 \u56de\u306e\u51e6\u7406\u3067 5 \u4ef6\u307e\u3067\n    ]);\n\n    foreach ($pending_posts as $post) {\n        if (send_to_x($post)) {\n            delete_post_meta($post-&gt;ID, 'x_share_pending');\n            update_post_meta($post-&gt;ID, 'x_share_sent', true);\n        }\n    }\n}\n\n\/\/ 15 \u5206\u3054\u3068\u306b\u5b9f\u884c\nif (!wp_next_scheduled('wp_scheduled_x_share')) {\n    wp_schedule_event(time(), '15min', 'wp_scheduled_x_share');\n}\n<\/code><\/pre>\n<p>\u3053\u306e\u65b9\u5f0f\u306b\u3088\u308a\u3001API \u30ec\u30fc\u30c8\u5236\u9650\u306b\u5dfb\u304d\u8fbc\u307e\u308c\u308b\u30ea\u30b9\u30af\u304c\u4f4e\u6e1b\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n<h3>\u30a8\u30e9\u30fc: 401 Unauthorized<\/h3>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<ul>\n<li>Bearer Token \u304c\u7a7a\u307e\u305f\u306f\u4e0d\u6b63<\/li>\n<li>Token \u306e\u6709\u52b9\u671f\u9650\u5207\u308c\uff08OAuth 2.0 \u306e\u5834\u5408\uff09<\/li>\n<\/ul>\n<p><strong>\u5bfe\u5fdc<\/strong><\/p>\n<pre><code class=\"language-php\">if ($status_code === 401) {\n    error_log('X API: \u8a8d\u8a3c\u30c8\u30fc\u30af\u30f3\u7121\u52b9\u3002\u30c8\u30fc\u30af\u30f3\u3092\u518d\u53d6\u5f97\u3057\u3066\u304f\u3060\u3055\u3044\u3002');\n    \/\/ \u7ba1\u7406\u8005\u30e1\u30fc\u30eb\u901a\u77e5\n    wp_mail(get_option('admin_email'), 'X API \u8a8d\u8a3c\u30a8\u30e9\u30fc', '\u30c8\u30fc\u30af\u30f3\u66f4\u65b0\u304c\u5fc5\u8981\u3067\u3059\u3002');\n}\n<\/code><\/pre>\n<h3>\u30a8\u30e9\u30fc: 429 Too Many Requests<\/h3>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<p>\u30ec\u30fc\u30c8\u5236\u9650\u306b\u9054\u3057\u305f\u3002<\/p>\n<p><strong>\u5bfe\u5fdc<\/strong><\/p>\n<pre><code class=\"language-php\">if ($status_code === 429) {\n    \/\/ \u30e1\u30bf\u30c7\u30fc\u30bf\u3092\u518d\u5ea6\u30bb\u30c3\u30c8\uff08\u5f8c\u3067\u518d\u8a66\u884c\uff09\n    update_post_meta($post-&gt;ID, 'x_share_retry_count', \n        (int)get_post_meta($post-&gt;ID, 'x_share_retry_count', true) + 1);\n\n    \/\/ 3 \u56de\u307e\u3067\u30ea\u30c8\u30e9\u30a4\n    $retry_count = (int)get_post_meta($post-&gt;ID, 'x_share_retry_count', true);\n    if ($retry_count &lt; 3) {\n        update_post_meta($post-&gt;ID, 'x_share_pending', true);\n    }\n}\n<\/code><\/pre>\n<h3>\u30a8\u30e9\u30fc: 422 Unprocessable Entity<\/h3>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<p>POST \u30c7\u30fc\u30bf\u306e\u5f62\u5f0f\u304c\u4e0d\u6b63\u3002\u3088\u304f\u306f 280 \u5b57\u8d85\u904e\u3002<\/p>\n<p><strong>\u5bfe\u5fdc<\/strong><\/p>\n<p>\u30c6\u30ad\u30b9\u30c8\u306e\u4e8b\u524d\u691c\u8a3c\u3092\u5f37\u5316:<\/p>\n<pre><code class=\"language-php\">function validate_tweet_text($text) {\n    $text = trim($text);\n\n    \/\/ \u7d75\u6587\u5b57\u3092\u542b\u3080\u5834\u5408\u306e\u6b63\u78ba\u306a\u6587\u5b57\u6570\u30ab\u30a6\u30f3\u30c8\n    $length = mb_strlen($text);\n\n    if ($length &gt; 280) {\n        return false; \/\/ \u307e\u305f\u306f\u81ea\u52d5\u77ed\u7e2e\n    }\n\n    return true;\n}\n<\/code><\/pre>\n<h2>\u672c\u756a\u904b\u7528\u3078\u306e\u6ce8\u610f\u70b9<\/h2>\n<h3>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/h3>\n<ul>\n<li>Bearer Token \u306f\u74b0\u5883\u5909\u6570\u3067\u7ba1\u7406\u3057\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u542b\u3081\u306a\u3044<\/li>\n<li>WordPress \u306e\u30e6\u30fc\u30b6\u30fc\u6a29\u9650\u3067 <code>publish_posts<\/code> \u304c\u5fc5\u8981\u306a\u5834\u5408\u3001REST API \u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u4fdd\u8b77\u3092\u5f37\u5316<\/li>\n<li>X API \u30a2\u30d7\u30ea\u306e\u6a29\u9650\u3092\u300cRead and Write\u300d\u306b\u9650\u5b9a\uff08\u300cRead and Delete\u300d\u306f\u4e0d\u8981\uff09<\/li>\n<\/ul>\n<h3>\u30e2\u30cb\u30bf\u30ea\u30f3\u30b0<\/h3>\n<p>\u30a8\u30e9\u30fc\u30ed\u30b0\u3092\u5b9a\u671f\u78ba\u8a8d\u3059\u308b\u304b\u3001Slack \/ \u30e1\u30fc\u30eb\u901a\u77e5\u3092\u7d44\u307f\u8fbc\u3080:<\/p>\n<pre><code class=\"language-php\">function notify_x_share_failure($post_id, $error_message) {\n    $post = get_post($post_id);\n    $message = sprintf(\n        &quot;X API \u6295\u7a3f\u5931\u6557\\n\u30bf\u30a4\u30c8\u30eb: %s\\n\u30a8\u30e9\u30fc: %s\\nURL: %s&quot;,\n        $post-&gt;post_title,\n        $error_message,\n        get_edit_post_link($post_id)\n    );\n\n    wp_mail(get_option('admin_email'), 'X API \u30b7\u30a7\u30a2\u5931\u6557\u901a\u77e5', $message);\n}\n<\/code><\/pre>\n<h3>\u6295\u7a3f\u306e\u9078\u5225<\/h3>\n<p>\u3059\u3079\u3066\u306e\u6295\u7a3f\u3092 X \u3078\u30b7\u30a7\u30a2\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u30ab\u30c6\u30b4\u30ea\u3084\u30bf\u30b0\u3067\u7d5e\u308b:<\/p>\n<pre><code class=\"language-php\">function should_share_to_x($post) {\n    $categories = wp_get_post_categories($post-&gt;ID);\n    $category_ids = wp_list_pluck(get_categories(['fields' =&gt; 'ids']), 'cat_ID');\n\n    \/\/ \u300c\u304a\u77e5\u3089\u305b\u300d\u30ab\u30c6\u30b4\u30ea\u306e\u307f\u30b7\u30a7\u30a2\n    return in_array($post-&gt;post_category[0], $category_ids);\n}\n<\/code><\/pre>\n<h2>\u8003\u5bdf\uff1a\u30d7\u30e9\u30b0\u30a4\u30f3 vs \u30ab\u30b9\u30bf\u30e0\u30b3\u30fc\u30c9<\/h2>\n<p>\u3053\u306e\u691c\u8a3c\u3092\u901a\u3058\u3066\u3001API \u9023\u643a\u3092\u30ab\u30b9\u30bf\u30e0\u30b3\u30fc\u30c9\u3067\u5b9f\u88c5\u3059\u308b\u5229\u70b9\u3068\u8ab2\u984c\u304c\u660e\u78ba\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><strong>\u30e1\u30ea\u30c3\u30c8<\/strong><\/p>\n<ul>\n<li>\u7d30\u304b\u3044\u30ed\u30b8\u30c3\u30af\u5236\u5fa1\u304c\u53ef\u80fd<\/li>\n<li>\u4e0d\u8981\u306a\u6a5f\u80fd\uff08\u7ba1\u7406\u753b\u9762 UI \u306a\u3069\uff09\u304c\u4e0d\u8981<\/li>\n<li>\u30d0\u30fc\u30b8\u30e7\u30f3\u66f4\u65b0\u6642\u306e\u4e92\u63db\u6027\u4e0d\u5b89\u304c\u306a\u3044<\/li>\n<\/ul>\n<p><strong>\u30c7\u30e1\u30ea\u30c3\u30c8<\/strong><\/p>\n<ul>\n<li>\u30b3\u30fc\u30c9\u306e\u4fdd\u5b88\u8cac\u4efb\u304c\u81ea\u5206\u305f\u3061\u306b\u3042\u308b<\/li>\n<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u691c\u8a3c\u306f\u81ea\u5206\u305f\u3061\u3067\u884c\u3046\u5fc5\u8981\u304c\u3042\u308b<\/li>\n<li>\u30c7\u30d0\u30c3\u30b0\u6642\u306e\u30ed\u30b0\u78ba\u8a8d\u304c\u91cd\u8981<\/li>\n<\/ul>\n<p>\u7d50\u8ad6\u3068\u3057\u3066\u3001<strong>\u904b\u7528\u898f\u6a21\u304c\u5c0f\u3055\u3044\u5834\u5408\uff081-2 \u30c9\u30e1\u30a4\u30f3\uff09\u306f\u30ab\u30b9\u30bf\u30e0\u30b3\u30fc\u30c9\u3001\u8907\u6570\u904b\u7528\u306a\u3089\u30d7\u30e9\u30b0\u30a4\u30f3\u5316\u3092\u691c\u8a0e<\/strong> \u3059\u308b\u65b9\u91dd\u304c\u73fe\u5b9f\u7684\u3067\u3059\u3002<\/p>\n<h2>API \u30ec\u30fc\u30c8\u5236\u9650\u3068\u30ad\u30e5\u30fc\u30a4\u30f3\u30b0\u306e\u5b9f\u88c5\u8a73\u7d30<\/h2>\n<p>\u30ec\u30fc\u30c8\u5236\u9650\u306e\u5bfe\u5fdc\u306f\u30d6\u30ed\u30b0\u898f\u6a21\u306b\u3088\u3063\u3066\u6226\u7565\u304c\u5909\u308f\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u30013 \u3064\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u6574\u7406\u3057\u307e\u3057\u305f\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u30d1\u30bf\u30fc\u30f3<\/th>\n<th>\u60f3\u5b9a\u898f\u6a21<\/th>\n<th>\u30ad\u30e5\u30fc\u30a4\u30f3\u30b0<\/th>\n<th>\u30ea\u30c8\u30e9\u30a4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u76f4\u9001<\/td>\n<td>\u6708 1-5 \u6295\u7a3f<\/td>\n<td>\u4e0d\u8981<\/td>\n<td>\u30b7\u30f3\u30d7\u30eb<\/td>\n<\/tr>\n<tr>\n<td>\u30ad\u30e5\u30fc + \u5b9a\u671f\u5b9f\u884c<\/td>\n<td>\u6708 6-30 \u6295\u7a3f<\/td>\n<td>\u63a8\u5968<\/td>\n<td>3 \u56de<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u90e8\u30ad\u30e5\u30fc\uff08SQS \u306a\u3069\uff09<\/td>\n<td>\u6708 30+ \u6295\u7a3f<\/td>\n<td>\u5fc5\u9808<\/td>\n<td>\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u53ef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6708 30 \u6295\u7a3f\u7a0b\u5ea6\u306a\u3089\u3001\u524d\u8ff0\u306e\u30ad\u30e5\u30fc\u30a4\u30f3\u30b0\u65b9\u5f0f\uff0815 \u5206\u3054\u3068\uff09\u3067\u5341\u5206\u5bfe\u5fdc\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u30ab\u30b9\u30bf\u30e0\u30d7\u30e9\u30b0\u30a4\u30f3\u5316\u3078\u306e\u6700\u7d42\u624b\u9806<\/h2>\n<p>\u3053\u3053\u307e\u3067\u306e\u30b3\u30fc\u30c9\u3092\u6574\u7406\u3057\u3001\u30d7\u30e9\u30b0\u30a4\u30f3\u5316\u3059\u308b\u624b\u9806\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u30d5\u30a1\u30a4\u30eb\u69cb\u6210<\/strong><\/p>\n<pre><code>wp-content\/plugins\/x-api-auto-share\/\n\u251c\u2500\u2500 x-api-auto-share.php \uff08\u30e1\u30a4\u30f3\u30d7\u30e9\u30b0\u30a4\u30f3\u30d5\u30a1\u30a4\u30eb\uff09\n\u251c\u2500\u2500 includes\/\n\u2502   \u251c\u2500\u2500 class-x-api.php\n\u2502   \u251c\u2500\u2500 class-queue-manager.php\n\u2502   \u2514\u2500\u2500 hooks.php\n\u251c\u2500\u2500 readme.txt\n\u2514\u2500\u2500 .gitignore\n<\/code><\/pre>\n<p><strong>x-api-auto-share.php (\u30e1\u30a4\u30f3)<\/strong><\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/**\n * Plugin Name: X API Auto Share\n * Description: REST API \u3067\u6295\u7a3f\u4f5c\u6210\u6642\u306b\u81ea\u52d5\u7684\u306b X \u3078\u30b7\u30a7\u30a2\n * Version: 1.0.0\n * Author: Your Name\n * License: GPL v2\n *\/\n\nif (!defined('ABSPATH')) exit;\n\ndefine('X_API_SHARE_PATH', plugin_dir_path(__FILE__));\n\nrequire_once X_API_SHARE_PATH . 'includes\/class-x-api.php';\nrequire_once X_API_SHARE_PATH . 'includes\/class-queue-manager.php';\nrequire_once X_API_SHARE_PATH . 'includes\/hooks.php';\n\nregister_activation_hook(__FILE__, function() {\n    \/\/ \u5b9a\u671f\u30bf\u30b9\u30af\u767b\u9332\n    if (!wp_next_scheduled('wp_scheduled_x_share')) {\n        wp_schedule_event(time(), '15min', 'wp_scheduled_x_share');\n    }\n});\n\nregister_deactivation_hook(__FILE__, function() {\n    wp_clear_scheduled_hook('wp_scheduled_x_share');\n});\n<\/code><\/pre>\n<p><strong>includes\/class-x-api.php<\/strong><\/p>\n<pre><code class=\"language-php\">&lt;?php\nclass X_API {\n    private $bearer_token;\n    private $api_url = 'https:\/\/api.twitter.com\/2\/tweets';\n\n    public function __construct() {\n        $this-&gt;bearer_token = getenv('X_API_BEARER_TOKEN') \n            ?: get_option('x_api_bearer_token');\n    }\n\n    public function post_tweet($text) {\n        if (mb_strlen($text) &gt; 280) {\n            return new WP_Error('text_too_long', 'Tweet text exceeds 280 characters.');\n        }\n\n        $response = wp_remote_post($this-&gt;api_url, [\n            'headers' =&gt; [\n                'Authorization' =&gt; 'Bearer ' . $this-&gt;bearer_token,\n                'Content-Type' =&gt; 'application\/json',\n            ],\n            'body' =&gt; wp_json_encode(['text' =&gt; $text]),\n            'timeout' =&gt; 10,\n        ]);\n\n        if (is_wp_error($response)) {\n            return $response;\n        }\n\n        $status_code = wp_remote_retrieve_response_code($response);\n\n        if ($status_code === 201) {\n            return true;\n        }\n\n        $body = json_decode(wp_remote_retrieve_body($response), true);\n        return new WP_Error('x_api_error', \n            'X API error: ' . wp_json_encode($body));\n    }\n}\n<\/code><\/pre>\n<p><strong>includes\/hooks.php<\/strong><\/p>\n<pre><code class=\"language-php\">&lt;?php\nadd_action('rest_insert_post', function($post, $request, $creating) {\n    if (!$creating || $post-&gt;post_status !== 'publish') {\n        return;\n    }\n\n    update_post_meta($post-&gt;ID, 'x_share_pending', true);\n    update_post_meta($post-&gt;ID, 'x_share_timestamp', current_time('timestamp'));\n}, 10, 3);\n\nadd_action('wp_scheduled_x_share', function() {\n    $x_api = new X_API();\n    $queue_mgr = new Queue_Manager();\n    $queue_mgr-&gt;process($x_api);\n});\n<\/code><\/pre>\n<p>\u3053\u306e\u30d7\u30e9\u30b0\u30a4\u30f3\u5316\u306b\u3088\u308a\u3001\u8907\u6570\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306e\u518d\u5229\u7528\u3084\u3001GitHub \u3067\u306e\u7ba1\u7406\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u203b\u672c\u8a18\u4e8b\u306f2026-05-16\u6642\u70b9\u306e\u60c5\u5831\u306b\u57fa\u3065\u304d\u307e\u3059\u3002AI \u30e2\u30c7\u30eb\u3084 API \u306e\u4ed5\u69d8\u30fb\u6599\u91d1\u306f\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u65b0\u306f\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u3054\u78ba\u8a8d\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>AI \/ tech \u306e\u9078\u629e\u306f\u8981\u4ef6\u3084\u74b0\u5883\u306b\u3088\u3063\u3066\u6700\u9069\u89e3\u304c\u5909\u308f\u308a\u307e\u3059\u3002\u672c\u8a18\u4e8b\u306f\u53c2\u8003\u60c5\u5831\u3067\u3001\u6700\u7d42\u7684\u306a\u6280\u8853\u5224\u65ad\u306f\u3054\u81ea\u8eab\u306e\u691c\u8a3c\u306b\u57fa\u3065\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li><strong>REST API \u30d5\u30c3\u30af\u3068 X API Bearer Token \u3067\u76f4\u7d50\u53ef\u80fd<\/strong> \u2014 \u30d7\u30e9\u30b0\u30a4\u30f3\u4e0d\u8981\u306a\u5b9f\u88c5\u304c\u3067\u304d\u308b<\/li>\n<li><strong>\u30ec\u30fc\u30c8\u5236\u9650\u5bfe\u7b56\u3068\u3057\u3066\u30ad\u30e5\u30fc\u30a4\u30f3\u30b0 + \u5b9a\u671f\u5b9f\u884c\u304c\u73fe\u5b9f\u7684<\/strong> \u2014 \u6708 30 \u6295\u7a3f\u7a0b\u5ea6\u307e\u3067\u30ab\u30d0\u30fc\u53ef\u80fd<\/li>\n<li><strong>\u672c\u756a\u904b\u7528\u3067\u306f\u30c8\u30fc\u30af\u30f3\u7ba1\u7406\u30fb\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u30fb\u30e2\u30cb\u30bf\u30ea\u30f3\u30b0\u304c\u5fc5\u9808<\/strong> \u2014 \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3068\u5b89\u5b9a\u6027\u306e\u305f\u3081<\/li>\n<\/ul>\n<p>\u6b21\u306e\u6bb5\u968e\u3068\u3057\u3066\u306f\u3001\u8907\u6570\u306e X \u30a2\u30ab\u30a6\u30f3\u30c8\u904b\u7528\u3078\u306e\u5bfe\u5fdc\u3001\u307e\u305f\u306f Bluesky API \u3078\u306e\u540c\u6642\u6295\u7a3f\u3092\u8a66\u3057\u3066\u307f\u305f\u3044\u3002<\/p>\n<hr>\n<p style=\"font-size:0.85em;color:#888;margin-top:2em;\">Photo by <a href=\"https:\/\/unsplash.com\/@1981digital?utm_source=shimaken&#038;utm_medium=referral\" target=\"_blank\" rel=\"noopener nofollow\">1981 Digital<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=shimaken&#038;utm_medium=referral\" target=\"_blank\" rel=\"noopener nofollow\">Unsplash<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress \u30d6\u30ed\u30b0\u306e\u6295\u7a3f\u3092\u81ea\u52d5\u7684\u306b X \u65e7 Twitter \u306b\u30b7\u30a7\u30a2\u3057\u305f\u3044\u3002\u305d\u3046\u8003\u3048\u308b\u7ba1\u7406\u8005\u306f\u591a\u3044\u3067\u3059\u304c\u3001\u5b9f\u88c5\u65b9\u6cd5\u3068\u306a\u308b\u3068\u9078\u629e\u80a2\u304c\u5206\u304b\u308c\u307e\u3059\u3002\u30d7\u30e9\u30b0\u30a4\u30f3\u983c\u307f\u306b\u3059\u308b\u304b\u3001API \u3092\u76f4\u63a5\u53e9\u304f\u304b\u3002 \u4eca\u56de\u306f\u3001WordPress REST API \u3068 X API \u3092\u76f4\u7d50\u3055\u305b\u3001\u30d6\u30ed\u30b0\u66f4\u65b0\u6642\u306b\u81ea\u52d5\u6295\u7a3f\u3059\u308b\u4ed5\u7d44\u307f\u3092\u5b9f\u969b\u306b\u69cb\u7bc9\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u305d\u306e\u904e\u7a0b\u3067\u898b\u3048\u305f\u8ab2\u984c\u3001\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u3001\u305d\u3057\u3066\u610f\u5916\u306a\u843d\u3068\u3057\u7a74\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057<\/p>\n","protected":false},"author":1,"featured_media":169,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_xw_keep_modified_date":false,"footnotes":""},"categories":[86,78,84],"tags":[26,71,77,69,75,70,76,74,73,72],"class_list":["post-170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-api-integration","category-tech-basics","category-automation-pipeline","tag-api","tag-oauth-2-0-","tag-php","tag-wordpress-rest-api","tag-wp-remote-post","tag-x-api-v2","tag-76","tag-74","tag-73","tag-72"],"xw_count_pv":"10","_links":{"self":[{"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/posts\/170\/revisions\/171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/media\/169"}],"wp:attachment":[{"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shimaken.com\/schnauzer\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}