How to Modify Code to Place Posts in Drafts Instead of Publishing in WordPress?

This topic was automatically generated from Slack. You can find the original thread here.

GM I would like to alter this code to put the post in drafts.

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    wordpressDomain: {
      type: "string",
      label: "WordPress Domain",
    },
    username: {
      type: "string",
      label: "WordPress Username",
    },
    password: {
      type: "string",
      label: "WordPress Password",
    },
    postTitle: {
      type: "string",
      label: "Post Title",
    },
    postContent: {
      type: "string",
      label: "Post Content",
    },
  },
  async run({ steps, $ }) {
    const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64");

    return await axios($, {
      method: "POST",
      url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts`,
      headers: {
        Authorization: `Basic ${authToken}`,
        "Content-Type": "application/json",
      },
      data: {
        title: this.postTitle,
        content: this.postContent,
        status: "publish",
      },
    });
  },
});

To save the post as a draft instead of publishing it, you just need to change the status property in the data object from "publish" to "draft". Here’s the updated code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { wordpressDomain: { type: “string”, label: “WordPress Domain”, }, username: { type: “string”, label: “WordPress Username”, }, password: { type: “string”, label: “WordPress Password”, }, postTitle: { type: “string”, label: “Post Title”, }, postContent: { type: “string”, label: “Post Content”, }, }, async run({ steps, $ }) { const authToken = Buffer.from(${this.username}:${this.password}).toString(“base64”); return await axios($, { method: “POST”, url: https://${this.wordpressDomain}/wp-json/wp/v2/posts, headers: { Authorization: Basic ${authToken}, “Content-Type”: “application/json”, }, data: { title: this.postTitle, content: this.postContent, status: “draft”, // Changed from “publish” to “draft” }, }); }, });

Using the WORDPRESS REST API, can you write me a code that will change the author ID based on the POST ID?

Thanks. Once that code is executed, I want to change the author ID to the post that was just written.

To change the author ID of the post that was just written, you can make an additional API call to update the post using the returned post ID from the previous request. Here’s the modified code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { wordpressDomain: { type: "string", label: "WordPress Domain", }, username: { type: "string", label: "WordPress Username", }, password: { type: "string", label: "WordPress Password", }, postTitle: { type: "string", label: "Post Title", }, postContent: { type: "string", label: "Post Content", }, newAuthorId: { type: "integer", label: "New Author ID", }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64"); const createPostResponse = await axios($, { method: "POST", url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts`, headers: { Authorization: `Basic ${authToken}`, "Content-Type": "application/json", }, data: { title: this.postTitle, content: this.postContent, status: "draft", }, }); const postId = createPostResponse.id; const updatePostResponse = await axios($, { method: "POST", url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts/${postId}`, headers: { Authorization: `Basic ${authToken}`, "Content-Type": "application/json", }, data: { author: this.newAuthorId, }, }); return updatePostResponse; }, }); This code first creates a draft post and then updates the author ID of the created post using the newAuthorId prop. Make sure to provide the new author ID when running the component.

I would prefer to add the post, and then create another code to update the author ID.

I understand. Here’s the code to update the author ID of an existing post using the WordPress REST API: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { wordpressDomain: { type: "string", label: "WordPress Domain", }, username: { type: "string", label: "WordPress Username", }, password: { type: "string", label: "WordPress Password", }, postId: { type: "integer", label: "Post ID", }, newAuthorId: { type: "integer", label: "New Author ID", }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64"); const updatePostResponse = await axios($, { method: "POST", url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts/${this.postId}`, headers: { Authorization: `Basic ${authToken}`, "Content-Type": "application/json", }, data: { author: this.newAuthorId, }, }); return updatePostResponse; }, }); This code takes the postId and newAuthorId as input and updates the author ID of the specified post. Make sure to provide the correct post ID and new author ID when running the component.

The code does not return errors, BUT the author ID did not change.

I apologize for the confusion. Let’s double-check the code to ensure it’s updating the author ID correctly. Please make sure you’re providing the correct postId and newAuthorId when running the component. If the issue persists, please share any additional details or error messages you encounter, and I’ll help you troubleshoot the problem.

Here is what I have:

wordpressDomain [hidden]
username sdotsamuel
password [hidden]
postId 360413
newAuthorId 22

I apologize for any confusion earlier. Based on the information you provided, here’s the updated code to change the author ID of the specified post using the WordPress REST API: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { wordpressDomain: { type: "string", label: "WordPress Domain", }, username: { type: "string", label: "WordPress Username", }, password: { type: "string", label: "WordPress Password", }, postId: { type: "integer", label: "Post ID", }, newAuthorId: { type: "integer", label: "New Author ID", }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64"); const updatePostResponse = await axios($, { method: "POST", url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts/${this.postId}`, headers: { Authorization: `Basic ${authToken}`, "Content-Type": "application/json", }, data: { author: this.newAuthorId, }, }); return updatePostResponse.data; }, }); Please ensure that the provided wordpressDomain, username, password, postId, and newAuthorId are correct. If the issue persists, let me know if there are any error messages or additional details, and I’ll help you troubleshoot the problem.

Can I use cURL with Pipedream?

Yes, you can use cURL with Pipedream, especially when working with HTTP triggers in your workflows. For example, if you have a workflow with an HTTP trigger, you can send data to the trigger URL using cURL. Here’s an example of how to send a JSON payload to your workflow’s trigger URL: bash curl -d '{ "message": "Pipedream is awesome!" }' -H "Content-Type: application/json" YOUR-TRIGGER-URL-GOES-HERE Replace YOUR-TRIGGER-URL-GOES-HERE with the actual trigger URL of your workflow. This will send the JSON payload to your workflow, and you can process the data within the workflow as needed.

When I add a post using the Wordpress REST API, how do I store the content so it’s using Guttenberg Blocks?

To store content using Gutenberg blocks when adding a post using the WordPress REST API, you’ll need to format the content using the Gutenberg block structure. Here’s an example of how to create a post with Gutenberg blocks: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { wordpressDomain: { type: "string", label: "WordPress Domain", }, username: { type: "string", label: "WordPress Username", }, password: { type: "string", label: "WordPress Password", }, title: { type: "string", label: "Post Title", }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this.password}`).toString("base64"); // Gutenberg block structure for content const content = `<!-- wp:paragraph --> <p>This is a paragraph block.</p> <!-- /wp:paragraph --> <!-- wp:heading --> <h2>This is a heading block.</h2> <!-- /wp:heading -->`; const createPostResponse = await axios($, { method: "POST", url: `https://${this.wordpressDomain}/wp-json/wp/v2/posts`, headers: { Authorization: `Basic ${authToken}`, "Content-Type": "application/json", }, data: { title: this.title, content: content, status: "publish", }, }); return createPostResponse.data; }, }); Replace the wordpressDomain, username, and password with your WordPress credentials, and provide a title for the new post. The content variable contains the Gutenberg block structure for a paragraph block and a heading block. You can customize the content as needed using the Gutenberg block structure.

I might be asking the wrong question.

I am taking content from a Google Doc formatted with basic markup like BOLD, ITALICS, H1, and H2.

I want to store this in Wordpress using the Rest API without losing the formatting.

To store content from a Google Doc in WordPress using the REST API without losing the formatting, you can follow these steps: 1. First, you’ll need to extract the content from the Google Doc. You can use the Google Drive API to read the content of the document in HTML format. Here’s an example of how to do this using Pipedream: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, documentId: { type: "string", label: "Google Document ID", }, }, async run({ steps, $ }) { const response = await axios($, { method: "GET", url: `https://www.googleapis.com/drive/v3/files/${this.documentId}/export`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, params: { mimeType: "text/html", }, }); const htmlContent = response.data; return htmlContent; }, }); Replace documentId with the ID of your Google Doc. 2. Next, you’ll need to convert the HTML content to Gutenberg block structure. You can use a library like html-to-gutenberg to do this. However, this library is not available in Pipedream, so you might need to implement a custom conversion function or find an alternative library. 3. Finally, create a new post in WordPress using the REST API with the Gutenberg block structure as the content. Here’s an example of how to do this: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { wordpressDomain: { type: “string”, label: “WordPress Domain”, }, username: { type: “string”, label: “WordPress Username”, }, password: { type: “string”, label: “WordPress Password”, }, title: { type: “string”, label: “Post Title”, }, content: { type: “string”, label: “Post Content (Gutenberg block structure)”, }, }, async run({ steps, $ }) { const authToken = Buffer.from(`${this.username}:${this