WP APIでplugin / themeのリストを取得する

WP 5.5かららしいですが、WP APIからプラグインやテーマにアクセスできるようになりました。 普通に使っている分にはお世話になることがなさそうですが、管理画面などを作る時に用事がでてきそうです。 事前準備 plug […]

広告ここから
広告ここまで

目次

    WP 5.5かららしいですが、WP APIからプラグインやテーマにアクセスできるようになりました。

    普通に使っている分にはお世話になることがなさそうですが、管理画面などを作る時に用事がでてきそうです。

    事前準備

    plugin / themeのAPIは認証必須です。

    簡単に試すだけであれば、WP APIチームが用意しているBasic Authプラグインあたりを入れて有効化します。

    APIコール

    認証付きでAPIリクエストを出してやると一覧がかえってきます。

     % curl --user username:password  https://example.com/wp-json/wp/v2/plugins | jq ".[].plugin"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  2541    0  2541    0     0   2795      0 --:--:-- --:--:-- --:--:--  2792
    [{
      "plugin": "Basic-Auth-master/basic-auth",
      "status": "active",
      "name": "JSON Basic Authentication",
      "plugin_uri": "https://github.com/WP-API/Basic-Auth",
      "author": "WordPress API Team",
      "author_uri": "https://github.com/WP-API",
      "description": {
        "raw": "Basic Authentication handler for the JSON API, used for development and debugging purposes",
        "rendered": "Basic Authentication handler for the JSON API, used for development and debugging purposes <cite>By <a href=\"https://github.com/WP-API\">WordPress API Team</a>.</cite>"
      },
      "version": "0.1",
      "network_only": false,
      "requires_wp": "",
      "requires_php": "",
      "textdomain": "Basic-Auth-master",
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wp/v2/plugins/Basic-Auth-master/basic-auth"
          }
        ]
      }
    }]

    認証に失敗したり、認証系プラグインが入っていなかったりするとエラーがきます。

    {
      "code": "rest_cannot_view_plugins",
      "message": "Sorry, you are not allowed to manage plugins for this site.",
      "data": {
        "status": 401
      }

    themeも同様に取れます。こちらはstatusの指定が必要です。

    % curl --user username:password  "https://example.com/wp-json/wp/v2/themes?status=active" | jq .
    {
      "stylesheet": "twentytwenty",
      "template": "twentytwenty",
      "requires_php": "5.2.4",
      "requires_wp": "4.7",
      "textdomain": "twentytwenty",
      "version": "1.2",
      "screenshot": "https://example.com/wp-content/themes/twentytwenty/screenshot.png",
      "author": {
        "raw": "the WordPress team",
        "rendered": "<a href=\"https://wordpress.org/\">the WordPress team</a>"
      },
      "author_uri": {
        "raw": "https://wordpress.org/",
        "rendered": "https://wordpress.org/"
      },
      "description": {
        "raw": "Our default theme for 2020 is designed to take full advantage of the flexibility of the block editor. Organizations and businesses have the ability to create dynamic landing pages with endless layouts using the group and column blocks. The centered content column and fine-tuned typography also makes it perfect for traditional blogs. Complete editor styles give you a good idea of what your content will look like, even before you publish. You can give your site a personal touch by changing the background colors and the accent color in the Customizer. The colors of all elements on your site are automatically calculated based on the colors you pick, ensuring a high, accessible color contrast for your visitors.",
        "rendered": "Our default theme for 2020 is designed to take full advantage of the flexibility of the block editor. Organizations and businesses have the ability to create dynamic landing pages with endless layouts using the group and column blocks. The centered content column and fine-tuned typography also makes it perfect for traditional blogs. Complete editor styles give you a good idea of what your content will look like, even before you publish. You can give your site a personal touch by changing the background colors and the accent color in the Customizer. The colors of all elements on your site are automatically calculated based on the colors you pick, ensuring a high, accessible color contrast for your visitors."
      },
      "name": {
        "raw": "Twenty Twenty",
        "rendered": "Twenty Twenty"
      },
      "tags": {
        "raw": [
          "blog",
          "one-column",
          "custom-background",
          "custom-colors",
          "custom-logo",
          "custom-menu",
          "editor-style",
          "featured-images",
          "footer-widgets",
          "full-width-template",
          "rtl-language-support",
          "sticky-post",
          "theme-options",
          "threaded-comments",
          "translation-ready",
          "block-styles",
          "wide-blocks",
          "accessibility-ready"
        ],
        "rendered": "blog, one-column, custom-background, custom-colors, custom-logo, custom-menu, editor-style, featured-images, footer-widgets, full-width-template, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready, block-styles, wide-blocks, accessibility-ready"
      },
      "theme_uri": {
        "raw": "https://wordpress.org/themes/twentytwenty/",
        "rendered": "https://wordpress.org/themes/twentytwenty/"
      },
      "theme_supports": {
        "align-wide": true,
        "automatic-feed-links": true,
        "custom-background": {
          "default-image": "",
          "default-preset": "default",
          "default-position-x": "left",
          "default-position-y": "top",
          "default-size": "auto",
          "default-repeat": "repeat",
          "default-attachment": "scroll",
          "default-color": "f5efe0"
        },
        "custom-header": false,
        "custom-logo": {
          "width": 120,
          "height": 90,
          "flex-width": true,
          "flex-height": true,
          "header-text": [],
          "unlink-homepage-logo": false
        },
        "customize-selective-refresh-widgets": true,
        "dark-editor-style": false,
        "disable-custom-colors": false,
        "disable-custom-font-sizes": false,
        "disable-custom-gradients": false,
        "editor-color-palette": [
          {
            "name": "Accent Color",
            "slug": "accent",
            "color": "#cd2653"
          },
          {
            "name": "Primary",
            "slug": "primary",
            "color": "#000000"
          },
          {
            "name": "Secondary",
            "slug": "secondary",
            "color": "#6d6d6d"
          },
          {
            "name": "Subtle Background",
            "slug": "subtle-background",
            "color": "#dcd7ca"
          },
          {
            "name": "Background Color",
            "slug": "background",
            "color": "#f5efe0"
          }
        ],
        "editor-font-sizes": [
          {
            "name": "Small",
            "size": 18,
            "slug": "small"
          },
          {
            "name": "Regular",
            "size": 21,
            "slug": "normal"
          },
          {
            "name": "Large",
            "size": 26.25,
            "slug": "large"
          },
          {
            "name": "Larger",
            "size": 32,
            "slug": "larger"
          }
        ],
        "editor-gradient-presets": false,
        "editor-styles": false,
        "html5": [
          "search-form",
          "comment-form",
          "comment-list",
          "gallery",
          "caption",
          "script",
          "style"
        ],
        "formats": [
          "standard"
        ],
        "post-thumbnails": true,
        "responsive-embeds": true,
        "title-tag": true,
        "wp-block-styles": false
      }
    }

    使い所

    認証部分をどれだけセキュアにできるかにかかっている部分はありそうですが、うまく使えばAWSのSystems ManagerなどでWordPressのプラグイン・テーマについても監視・管理できるんじゃないかなと思います。

    広告ここから
    広告ここまで
    Home
    Search
    Bookmark