View Categories

How to Debug Custom Commands (JS/REST)

4 min read

Learn how to diagnose problems in your JavaScript commands and REST API integrations.

If your custom command doesnโ€™t work, prints errors, or behaves unpredictably, this guide walks you through the best debugging process.

Custom commands are pure JavaScript functions, and REST calls use WordPressโ€™s JSON API, so debugging is straightforward once you know where to look.


๐Ÿ”น 1. Start With the Browser Console (Most Important Step) #

Open:

Right-click โ†’ Inspect โ†’ Console

Then run your custom command in the terminal.

Look for any JavaScript error:

Uncaught ReferenceError: mycommand is not defined
Uncaught TypeError: registry.mycmd is not a function
SyntaxError: Unexpected token

If you see any error:

  • your add-on script likely failed
  • commands wonโ€™t register
  • help wonโ€™t show them
  • terminal won’t run them

Fix the JS error first.


๐Ÿ”น 2. Confirm Your Script Actually Loaded #

Open:

Inspect โ†’ Network โ†’ JS

Look for your file:

my-addon.js
custom-commands.js

If the file is missing:

  • Enqueue path is wrong
  • File not uploaded
  • CDN blocked it
  • Plugin directory renamed
  • Wrong file permissions

If the file is there but greyed out (blocked):

  • caching plugin merged/minified it incorrectly
  • script blocked by security plugin

Disable:

  • JS minification
  • Combine JS files
  • Defer JS
  • Rocket Loader (Cloudflare)

Reload and test again.


๐Ÿ”น 3. Check Command Registration Order #

Commands must be registered after the terminal loads.

Wrong:

registry.mycommand = ...

Correct:

window.addEventListener('load', function() {
    CointactedSocialTerminalTerminal.registry.mycommand = function(input) {
        append_line("OK", "system");
    };
});

If you register too early โ†’ registry is undefined.


๐Ÿ”น 4. Check Your Metadata #

For command to show in help, metadata must be valid.

Example:

CointactedSocialTerminalTerminal.commands_meta.mycommand = {
    group: "ADDON",
    description: "Says hello",
    alias: ["hi"],
    public: true
};

Missing keys can cause help issues.

Common errors:

โŒ group name missing
โŒ typo in key (e.g., descripton)
โŒ setting hidden: true

Fix metadata โ†’ reload browser.


๐Ÿ”น 5. Test Command Execution Directly in Console #

Before typing in terminal, test the function in console:

CointactedSocialTerminalTerminal.registry.mycommand({ 
    cmd: "mycommand", 
    args: [], 
    raw: "mycommand" 
});

If this works โ†’ your function is fine.
If this fails โ†’ issue inside your code.

This isolates input parsing from your command logic.


๐Ÿ”น 6. Debug REST API Requests #

If your command uses fetch():

const res = await fetch('/wp-json/myaddon/v1/data');

Open:

Inspect โ†’ Network โ†’ Fetch/XHR

Click on your request:

Check for:

  • Status code (200, 403, 404, 500)
  • Response body
  • JSON output
  • CORS issues
  • Nonce/Authentication issues

Common problems:

โŒ 404 #

Route not registered:

register_rest_route('myaddon/v1', '/data', [...]

โŒ 403 #

Permission callback blocked it:

Fix:

'permission_callback' => '__return_true'

(or proper permission logic)

โŒ 500 #

PHP error in callback.

Enable debug:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);

Then check:

/wp-content/debug.log


๐Ÿ”น 7. Use Console Logging Inside Commands #

Example:

registry.mycmd = function(input) {
    console.log("Input:", input);
    console.log("Args:", input.args);
    console.log("Raw:", input.raw);
    append_line("OK", "system");
};

This helps with:

  • argument parsing
  • alias handling
  • debugging incorrect user syntax

๐Ÿ”น 8. Validate Command Arguments #

User may type something your command doesnโ€™t expect.

Example:

random a b

Add validation:

if (input.args.length < 2) {
    return append_line("Usage: random min max", "error");
}

Validate:

  • number arguments
  • URL formats
  • required fields
  • JSON structure

๐Ÿ”น 9. Check for Other Plugins Overriding Your Command #

If another plugin uses the same command name:

registry.status

Your command will be overwritten.

Check:

help search status

If the description doesnโ€™t match your metadata โ†’ you have a naming conflict.

Fix:

  • rename your command
  • use namespacing: myaddon_status

๐Ÿ”น 10. Disable Browser Extensions That Inject Scripts #

Extensions like:

  • Grammarly
  • LanguageTool
  • Password managers
  • Privacy blockers
  • Web3 blockers

โ€ฆinject scripts into input fields and can break terminal behavior.

Test in:

  • Chrome Incognito
  • Firefox Private Window

If command works there โ†’ an extension is interfering.


๐Ÿ”น 11. Test Add-On With All Other Plugins Disabled #

To isolate the environment:

  1. Deactivate all plugins except:
    • Cointacted Social Terminal
    • Your add-on
  2. Switch to default theme
  3. Test command again

If it works:
Another plugin or theme is breaking scripts.

If it still doesnโ€™t work:
Your command contains a logic or syntax error.


๐Ÿ”น 12. Debug Infinite Loops or Freezing #

If command freezes:

while(true) {}

or heavy loops without async:

for (let i=0; i<1e9; i++) {}

Use async functions to break work into chunks:

await new Promise(r => setTimeout(r));

Never block the main thread.


๐ŸŽฏ Summary #

To debug custom commands:

โœ” Check console for JS errors #

โœ” Ensure add-on JS loads correctly #

โœ” Register commands after terminal loads #

โœ” Validate and test metadata #

โœ” Log input arguments #

โœ” Test command function directly #

โœ” Debug REST responses via Network tab #

โœ” Check for syntax mistakes #

โœ” Remove naming conflicts #

โœ” Test in a clean environment #

Following these steps resolves almost every custom command issue.

Leave a Reply

Your email address will not be published. Required fields are marked *