PHP code example of trk / processwire-console

1. Go to this page and download the library: Download trk/processwire-console library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

trk / processwire-console example snippets



declare(strict_types=1);
namespace ProcessWire;

return new class {
    public function up(): void
    {
        $field = new Field();
        $field->type = wire('modules')->get('FieldtypeText');
        $field->name = 'subtitle';
        $field->label = 'Subtitle';
        wire('fields')->save($field);
    }

    public function down(): void
    {
        $field = wire('fields')->get('subtitle');
        if (!$field || !$field->id) {
            return;
        }

        // Guard: field must not be attached to any template
        $fieldgroups = $field->getFieldgroups();
        if ($fieldgroups->count() > 0) {
            $names = $fieldgroups->implode(', ', 'name');
            throw new WireException(
                "Cannot delete field 'subtitle' — attached to: {$names}. Detach first."
            );
        }

        wire('fields')->delete($field);
    }
};


// site/commands/SyncProductsCommand.php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class SyncProductsCommand extends Command
{
    protected function configure(): void
    {
        $this
            ->setName('products:sync')
            ->setDescription('Synchronize products from external API.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        // Your logic here — $pages, $modules etc. are available via wire()
        $io->success('Products synchronized.');
        return Command::SUCCESS;
    }
}
bash
php vendor/bin/wire list
bash
php vendor/bin/wire list
cmd
@echo off
php vendor/bin/wire %*
bash
php vendor/bin/wire page:find "template=blog-post, title%=ProcessWire, limit=10"

# JSON output
php vendor/bin/wire page:find "parent=/products/, sort=-created, limit=5" --json
bash
php vendor/bin/wire page:publish --id=1042
php vendor/bin/wire page:unpublish --id=1042

# Skip confirmation
php vendor/bin/wire page:publish --id=1042 --force
bash
php vendor/bin/wire page:trash --id=1042
php vendor/bin/wire page:restore --id=1042

# Force without confirmation
php vendor/bin/wire page:trash --id=1042 --force
bash
php vendor/bin/wire page:move --id=1042 --parent=/new-section/
php vendor/bin/wire page:move --id=1042 --parent=1001 --dry-run
bash
php vendor/bin/wire field:info --name=body
php vendor/bin/wire field:info --name=body --json
bash
php vendor/bin/wire field:delete --name=unused_field
php vendor/bin/wire field:delete --name=unused_field --force
bash
php vendor/bin/wire template:list
php vendor/bin/wire template:list --search=blog --json
bash
php vendor/bin/wire template:info --name=basic-page
php vendor/bin/wire template:info --name=basic-page --json
bash
php vendor/bin/wire template:update --name=blog-post --set="label=Blog Article"
php vendor/bin/wire template:update --name=blog-post --set="noChildren=1" --set="noParents=-1"
bash
php vendor/bin/wire template:rename --name=old-template --new-name=new-template
bash
php vendor/bin/wire template:delete --name=unused-template
php vendor/bin/wire template:delete --name=unused-template --force
bash
php vendor/bin/wire module:install --name=ModuleName
php vendor/bin/wire module:uninstall --name=ModuleName --force
bash
php vendor/bin/wire module:upgrade --name=ModuleName
bash
# List permissions
php vendor/bin/wire permission:list

# Create a custom permission
php vendor/bin/wire permission:create --name=my-custom-permission

# Delete a custom permission
php vendor/bin/wire permission:delete --name=my-custom-permission
bash
php vendor/bin/wire cache:clear
php vendor/bin/wire cache:clear --json
bash
php vendor/bin/wire log:list
php vendor/bin/wire log:list --json
bash
# Read 'errors' log
php vendor/bin/wire log:read errors

# Read with a specific date range
php vendor/bin/wire log:read messages --from="2026-04-01" --to="today"

# Limit output and filter by string
php vendor/bin/wire log:read errors --limit=50 --find="Exception"

# JSON output
php vendor/bin/wire log:read errors --json
bash
php vendor/bin/wire log:clear errors
php vendor/bin/wire log:clear exceptions --force
bash
php vendor/bin/wire db:backup
php vendor/bin/wire db:backup --json
bash
php vendor/bin/wire db:restore --file=backup-2026-04-08.sql
php vendor/bin/wire db:restore --file=backup-2026-04-08.sql --force
bash
php vendor/bin/wire backup:list
php vendor/bin/wire backup:list --json
bash
# Keep the 5 most recent backups
php vendor/bin/wire backup:purge --keep=5
php vendor/bin/wire backup:purge --keep=3 --force
bash
php vendor/bin/wire make:migration create_blog_section
php vendor/bin/wire make:migration add_body_field --type=create-field --field=body
bash
php vendor/bin/wire make:queue SendEmailQueue

# Save directly to a specific module instead of the global site/queue directory
php vendor/bin/wire make:queue CompileImagesQueue --module=FieldtypeAiAssistant
bash
# Apply all pending
php vendor/bin/wire migrate

# Apply one at a time
php vendor/bin/wire migrate --step=1

# Preview without applying
php vendor/bin/wire migrate --dry-run

# Non-interactive
php vendor/bin/wire migrate --force

# JSON output
php vendor/bin/wire migrate --json
bash
php vendor/bin/wire migrate:reset
php vendor/bin/wire migrate:reset --force
bash
php vendor/bin/wire migrate:refresh
php vendor/bin/wire migrate:refresh --force --json
bash
php vendor/bin/wire migrate:fresh
php vendor/bin/wire migrate:fresh --force
bash
php vendor/bin/wire migrate:status
php vendor/bin/wire migrate:status --json

+---------------------------------------------+---------+-------+
| Migration                                   | Status  | Batch |
+---------------------------------------------+---------+-------+
| 20260408120000_create_blog_template.php      | Applied |     1 |
| 20260408120100_add_featured_image_field.php  | Applied |     1 |
| 20260408120200_create_editor_role.php        | Pending |     - |
+---------------------------------------------+---------+-------+
 ! [NOTE] 2 applied, 1 pending.
bash
php vendor/bin/wire migrate:install
bash
php vendor/bin/wire version
php vendor/bin/wire -V
bash
php vendor/bin/wire queue:table
bash
php vendor/bin/wire queue:work --queue=default --sleep=3 --tries=3
bash
php vendor/bin/wire queue:failed
bash
php vendor/bin/wire queue:retry 12
php vendor/bin/wire queue:retry all
bash
php vendor/bin/wire queue:clear
bash
php vendor/bin/wire make:seeder UsersSeeder
php vendor/bin/wire make:seeder DemoProductsSeeder --module=Shop
bash
php vendor/bin/wire make:task SyncDataTask
php vendor/bin/wire make:task CleanupTask --module=AppAdmin
bash
php vendor/bin/wire schedule:run
bash
php vendor/bin/wire up
bash
php vendor/bin/wire tinker
bash
# Pipe page data to jq
php vendor/bin/wire page:list --template=blog-post --json | jq '.data.items[].title'

# Count modules
php vendor/bin/wire module:list --site --json | jq '.data.total'

# Use in shell scripts
BACKUP=$(php vendor/bin/wire db:backup --json | jq -r '.data.file')
echo "Backup saved to: $BACKUP"
bash
php vendor/bin/wire page:update --id=1042 --title="Test" --dry-run
php vendor/bin/wire template:delete --name=old-template --dry-run
php vendor/bin/wire db:restore --file=backup.sql --dry-run