1

I am using node-wp-cli to run WP-cli commands from Node.js. All inbuilt WP-cli commands works perfectly but I stuck while trying to run my custom WP-cli commands.

Is there a way to to run my custom WP-Cli commands wp MyRegisteredCommand SubCommand from node-wp-cli? like:

WP.MyRegisteredCommand.SubCommand(function(err,res){ //get response
    console.log(res);
});

1 Answer 1

0

This is relatively easy, but requires some tweaking...

Node wp-cli uses wp cli cmd-dump to identify the available commands, but this command runs before the WordPress is loaded, so it will recognize only the in-built commands.

The tweak in short:

  • See WP.js, line schema(function(err,schema){.... You need to know the structure of schema, so just run some in-built command and put a breakpoint here.
  • Implement the schema for your commands, following the schema structure above. It will look something like this:

    my_commands.js:

    var schema = {
        mycmd: {
            mysubcmd: {
                args: [ {
                    arg: 'arg1',
                    required: true,
                    type: 'args'
                } ],
                endpoint: true,
                options: {
                    opt1: {
                        required: true,
                        type: 'param'
                    },
                    flag1: {
                        required: false,
                        type: 'flag'
                    },
                }
            },
            use: 'wp mycmd mysubcmd'
        }
    }
    
    module.exports.schema = schema;
    
  • Modify WP.js:

    var mySchema = require('my_commands.js').schema;
    
    ...
    
    schema(function(err,schema){
    
        schema = _.merge(schema, mySchema); // this line added
    
        var toMerge = function mapSchema(schema){
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.