1

Recently wpapi updated and ther is no option to send posts or images via post api but maybe some errors in code. Here is half workable code for uploading images and post data to wordpress.

#!/usr/bin/env node
var moveFrom = __dirname+"/gallery/galleryfolder/year/";
var fs = require( 'fs' );
var path = require( 'path' );
// In newer Node.js versions where process is already global this isn't necessary.
var process = require( "process" );
// Loop through all the files in the temp directory
fs.readdir( moveFrom, function( err, files ) {

var moveFrom = __dirname+"/gallery/galleryfolder/year/";//setting up directory for file upload
        if( err ) {
            console.error( "Could not list the directory.", err );
            process.exit( 1 );
        }

        files.forEach( function( file, index ) {
                // Make one pass and make the file complete
                var fromPath = path.join( moveFrom, file );
              //  var toPath = path.join( moveTo, file );
//starting getting filenames
                fs.stat( fromPath, function( error, stat ) {
                    if( error ) {
                        console.error( "Error stating file.", error );
                        return;
                    }

                    if( stat.isFile() ){


                      console.log( "'%s' is a file.",moveFrom+file );
                      var fullpath=moveFrom+file;
                      var filename=file.replace('.jpg', '');//specifying the fullpath to the images on node server or in file system.
                      var WPAPI = require("wpapi");
                      var wp= new WPAPI({
                        endpoint: 'http://localhost/wordpress/wp-json/',
                        username: 'admibn',
                        password: '1234567890'

                      });
                    //  let category=moveFrom.match('/[^/]*$/');
                      wp.posts().create({
                        title: filename,
                        content: '',
                        if ( err ) {
                             // handle err
                         }
                      }).then(function( post ) {
                        // Create the media record & upload your image file
                        var filePath = fullpath;
                        return wp.media().file( filePath ).create({
                          title: filename,
                          post: post.id
                        }).then(function( media ) {

                          // Set the new media record as the post's featured media
                          return wp.posts().id( post.id ).update({
                            featured_media: media.id
                          });

                        });
                      });
}








                } );
        } );
} );

The code is uploading images and posts text to the wordpress site.The first part of the code (node fs library)is working fine but the second i cannot even manage to debug or output the errors to console. I am a littele bit new to node.js. Please advice.

1 Answers1

0

Solved. Thnx for the attention. Recently WPapi requires 2 plugins to be installed: JSON Basic Authentication and http://v2.wp-api.org/ wpapi plugin. I have edited code if someone else will be in search of batch wp uploading solution from pc(texts and images ). Also the post switcher plugin saves time that needed for custom post gateway.