WordPress.com: Publish Post

WordPress.com: Publish Post

WordPress.com: 記事公開

This item publishes a post in draft status on WordPress.com. When the publish datetime is not specified, the post will be immediately published.

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_Auth
C1: OAuth2 Setting *
conf_Domain
C2: Domain of the WordPress.com site *
conf_PostId
C3: Post ID *
conf_Date
C4: Publish Datetime (If in future, schedule publish)

Notes

  • This Auto Step is for WordPress.com (a service that hosts WordPress sites, provided by Automattic itself)
  • OAuth2 settings should be created per user per site
    • When the same user handles multiple WordPress.com sites, create an OAuth2 setting for each site

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • wordpress-post-publish.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step

// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://public-api.wordpress.com/oauth2/authorize
// - Token Endpoint URL: https://public-api.wordpress.com/oauth2/token
// - Scope: posts
// - Client ID: (Get by https://developer.wordpress.com/apps/)
// - Client Secret: (Get by https://developer.wordpress.com/apps/)

const API_VERSION = 'v1.1';
const DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";

const main = () => {
    const auth = configs.getObject('conf_Auth');
    const domain = configs.get('conf_Domain');
    const postId = retrievePostId();
    const date = retrieveDate();

    checkPost(auth, domain, postId);
    publishPost(auth, domain, postId, date);
};

/**
 * config から投稿 ID を読み出す
 * 空の場合、数字以外の文字が含まれる場合はエラー
 * @returns {String} postId
 */
const retrievePostId = () => {
    const postId = engine.findData(configs.getObject('conf_PostId'));
    if (postId === null) {
        throw 'Post ID is blank.';
    }
    const idReg = new RegExp('^[0-9]+$');
    if (!idReg.test(postId)) {
        throw 'Post ID must be a non-negative integer.';
    }
    return postId;
};

/**
 * config から公開日時を文字列として読み出す
 * データ項目が選択されていない場合、空文字列を返す
 * データ項目が選択されていて値が空の場合は、エラー
 * @returns {String} date
 */
const retrieveDate = () => {
    const dateDef = configs.getObject('conf_Date');
    if (dateDef === null) {
        return '';
    }
    const datetimeObj = engine.findData(dateDef);
    if (datetimeObj === null) {
        throw 'Publish Datetime is selected but its data is null.';
    }
    return dateFormatter.format(DATETIME_FORMAT, datetimeObj);
};

/**
 * 投稿のステータスを確認し、下書き状態でない場合はエラー
 * @param {AuthSettingWrapper} auth
 * @param {String} domain
 * @param {String} postId
 * @returns {any}
 */
const checkPost = (auth, domain, postId) => {
    const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/posts/${postId}`;
    const response = httpClient.begin().authSetting(auth)
        .queryParam('fields', 'status')
        .get(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to get post. status: ${status}`;
    }
    const postStatus = JSON.parse(responseStr).status;
    if (postStatus !== 'draft') {
        throw `The status of the post is not draft but ${postStatus}.`;
    }
};

/**
 * 投稿を公開 / 公開予約する
 * @param {AuthSettingWrapper} auth
 * @param {String} domain
 * @param {String} postId
 * @param {String} date
 * @returns {any}
 */
const publishPost = (auth, domain, postId, date) => {
    const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/posts/${postId}`;
    const response = httpClient.begin().authSetting(auth)
        .queryParam('fields', 'URL,status,date')
        .formParam('status', 'publish')
        .formParam('date', date)
        .post(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to publish post. status: ${status}`;
    }
    engine.log(`Publish request succeeded. ${responseStr}`);
};

    
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading