Support » Developing with WordPress » How to create a post with image from external code?

  • Hello guys.
    I am trying to create a typescript program that uses wordpress rest api to create a new wordpress post.
    What is the proposed method for the authentication part?
    If I get past this first goal(authnetication) is it possible to create a post with more things than a title and text? (an image perhaps)
    Thank you very much!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz


    Cookie authentication is the default, and there are a few plugins offering various alternatives. See

    You can set any post field there is when inserting a post. The API affects DB data, AFAIK there is no mechanism for transporting actual media files, as that is server related, not DB. Media files can be uploaded to the temp folder via conventional HTTPS transport, then an Ajax or custom API endpoint request can trigger the rest of the upload process: moving out of temp and creating a media attachment post.

    Thank you sir for you kind answer.
    My problem is that all the documentation is based on php language.
    Are there any examples in some other language?
    (For example in the so popular javascript)
    Thank you again for your help!

    Moderator bcworkz


    A little bit of PHP is hard to escape. The cookie auth example on the page I linked is largely JS and jQuery. PHP is used to create WP nonces, which is then passed to your JS for sending in headers. No way around that part, but you can use the PHP example verbatim. It goes with the call to wp_enqueue_script() necessary to get a WP page to load an external JS code page, all within a callback to “wp_enqueue_scripts” action. If you want to use JS and jQuery in WP, you need to at least learn enough PHP to handle getting your JS to load correctly.

    If you are using one of the auth plugins, I’m not sure what’s available for examples, but the API is language agnostic. As long as your favorite code can generate the proper HTML request (and process JSON response), all of the API is available to you.

    Which auth method do you think you will be using? I’ll see if I can find something for you.

    In fact I don’t know if I was clear enough.
    I want to use javascript totally externally (from example running on my laptop inside nodejs).
    In fact I have created some code using the wpapi library but I have not managed to get to the next level, meaning create a post, because I am stuck with the authentication method.
    Check my code below (the post creation part is not working of course).
    Thank you again for your rapid answers!
    I really appreciate your help sir!

    get_request.ts :

    import WPAPI from 'wpapi';
    import {prettyJ} from "../utilities/beautify";
    const wp = new WPAPI({
        endpoint: '',
        // This assumes you are using basic auth, as described further below
        username: 'skiabox',
        password: 'skia0987'
    const apiPromise ='')
            site => {
                    return site.auth({
                        username: 'skiabox',
                        password: 'skia0987'
    // If default routes were detected, they are now available
        site => {
            // If default routes were detected, they are now available
                    posts => console.log(prettyJ(posts))
            );  //end of inner Promise
                    comments => console.log(prettyJ(comments))
            // If custom routes were detected, they can be accessed via .namespace()
                .then(authors => console.log(authors));
            // Namespaces can be saved out to variables:
            const myplugin = site.namespace( 'myplugin/v1' );
                .id( 7 )
                .then(author => console.log(author));
        });  //end of outer Promise
    //create a new post
        // "title" and "content" are the only required properties
        title: 'Your Post Title',
        content: 'Your post content',
        // Post will be created as a draft by default if a specific "status"
        // is not specified
        status: 'publish'
    }).then(function( response ) {
        // "response" will hold all properties of your newly-created post,
        // including the unique <code>id</code> the post was assigned on creation
        console.log( );
        ( data => console.log(data) )
        (err) => console.log(err)
    Moderator bcworkz


    It looks like you are trying to use Basic Authentication plugin. I’m assuming you’ve added the files to your plugins folder and activated the plugin through WP backend.

    The WPAPI class belongs to the node-wpapi JS client, so you should have installed that as well. Its docs have references for browser and server based apps. Not sure how to manage it for any other platform.

    Have you seen this page? It appears to be close to what you want to do.

    TBH, my JS skills are pretty basic. All that’s really required is a properly formed request be made to the API. Use what you know to do that. The Basic Auth plugin gets username and password from properly formatted HTTP headers, so be sure your API request does that somehow. Exactly how depends on the platform.

    One way is to send this sort of request:
    This is not a secure method though, I’ve heard there are issues using this with Microsoft related products (Edge/IIS), but Chrome/Apache etc. should still be OK.

    Disclaimer: I’m not experienced in using the Basic Auth plugin. I’ve only used cookie auth, but then you are going to need some PHP to get somewhere with that.

    Thank you sir once again for your rapid answer.
    Is it possible for the current wordpress version or the next Gutenberg version to create a wordpress post from external code without having to install manually a plugin (I mean either without any plugin or installing the plugin programmatically)?
    Sorry again for the mountain of questions!
    I promise this is the last one!

    I believe that one possible solution for plugin installation with one click is the following :

    Moderator bcworkz


    Yes, the one click install code is a viable approach. I think I see where you’re trying to go with this. If I’m right, it’s not all that helpful because the wpreset code would itself need to be made into a plugin and manually added so that other plugins can be added with one click. Are you gaining anything? Maybe if you need to install multiple plugins. But for one plugin? I think not.

    Currently, the only way to add posts from an external app without adding custom code through a plugin or theme is by using cookie authentication. I don’t see this changing anytime soon, but I’m not involved in core development, what do I know?

    With cookie authentication, we typically need users to first log in normally before we can add posts. But if your app is browser based, or can at least manage cookies, it can submit the user’s name and password just as the wp-login.php page would do, so I think this is worth looking at if it’s important to avoid requiring plugins be installed. Of course the user still needs to provide name and password, but they can provide it to your app and your app can log in on their behalf.

    Thank you sir for your kind replies!
    I don’t want to upload an image to the wordpress database, just display the image in the post created by wordpress api using image url.
    Do you believe it is possible!
    Thank you again for your time!


Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to create a post with image from external code?’ is closed to new replies.