Snippets
The snippet command group lets you version-control PHP snippets as plain files in Git. Each snippet is stored as a code file alongside a .json sidecar in a local directory that you commit like any other code.
Works with either WPCode or the Code Snippets plugin. The Loopress WordPress plugin detects whichever one is active on the site, so the CLI commands are the same either way. If the site has neither, add one: lps plugin add code-snippets (or insert-headers-and-footers for WPCode).
Typical workflow
Section titled “Typical workflow”# 1. Download existing snippets from WordPresslps snippet pull
# 2. Edit locally, commit to Gitgit add snippets/ && git commit -m "feat: update price formatter snippet"
# 3. Deploy back to WordPresslps snippet pushCommands
Section titled “Commands”Every command below also accepts the common flags: --env, --yes, and --json where it applies.
lps snippet pull
Section titled “lps snippet pull”Download all snippets from WordPress and write them as .php files.
lps snippet pull [path]| Argument | Default | Description |
|---|---|---|
path |
./snippets (or loopress.json’s snippetsDir) |
Local directory where snippets are written |
| Flag | Description |
|---|---|
--dry-run / -d |
Show what would be written without touching the filesystem |
Local files following the <id>-<slug> naming whose id is no longer on WordPress are removed on pull, so the directory always mirrors the site. See pull mirrors the site for the confirmation asked first. Snippets with no name are skipped with a warning.
Example:
lps snippet pull ./wp-snippets --dry-runlps snippet push
Section titled “lps snippet push”Upload .php files from a local directory to WordPress.
- If the sidecar
.jsoncontains anid, that snippet is updated by ID. - If that id doesn’t exist on the target site (e.g. a fresh WordPress install), a new snippet is created instead and the local sidecar is updated with the assigned id.
- Otherwise, a new snippet is created.
After a push, local files that don’t already follow the <id>-<slug> naming (a hand-created demo.php, or a stale slug after a rename) are renamed on disk to match, the same convention lps snippet pull writes.
lps snippet push [path]| Argument | Default | Description |
|---|---|---|
path |
./snippets (or loopress.json’s snippetsDir) |
Local directory to read snippet files from |
| Flag | Description |
|---|---|
--dry-run / -d |
Show what would be pushed without making any changes |
Example:
lps snippet push ./wp-snippetslps snippet list
Section titled “lps snippet list”Print all snippets currently on WordPress.
lps snippet list| Flag | Description |
|---|---|
--json |
Output raw JSON instead of formatted text |
Example output:
Found 3 snippets:
42. Price Formatter Active: yes Tags: woocommerce, formatting Description: Formats WooCommerce prices
17. Redirect Homepage Active: nolps snippet diff
Section titled “lps snippet diff”Show what differs between your local snippets and a WordPress environment, or between two environments with --against. Also part of the aggregate lps diff (--only snippet).
lps snippet diff [path]lps snippet diff --env staging --against productionExits 0 in sync, 1 on drift, 2 when the comparison failed.
lps snippet rollback
Section titled “lps snippet rollback”Restore snippets on WordPress to the snapshot saved automatically before an earlier lps snippet push. See Rollback and Snapshots.
lps snippet rollback # the most recent snapshotlps snippet rollback --list # available snapshotslps snippet rollback --to <id> # an older onelps snippet publish
Section titled “lps snippet publish”Publish local snippet files to your Loopress account so they can be deployed to other projects. This does not touch any WordPress site, it uploads to Loopress only.
Requires lps login first, and the current project must be linked to your Loopress account (lps project push). Since it never talks to WordPress, it works whichever plugin edition is installed.
lps snippet publish [path]| Argument | Default | Description |
|---|---|---|
path |
./snippets (or loopress.json’s snippetsDir) |
Local directory to read snippet files from |
Example:
lps snippet publishlps snippet publish ./wp-snippetsFile format
Section titled “File format”Each snippet is stored as two files in the snippets directory: a code file and a .json sidecar that holds the metadata. Files are named {id}-{slug}.{ext}, where {slug} is the snippet name lowercased and slugified.
snippets/ 42-price-formatter.php 42-price-formatter.json 17-redirect-homepage.php 17-redirect-homepage.json 11-custom-login-logo.css 11-custom-login-logo.jsonSidecar files
Section titled “Sidecar files”lps snippet pull writes a .json sidecar next to each snippet file. The sidecar holds the metadata that lps snippet push uses to identify and configure the snippet on WordPress.
{ "id": 42, "name": "Price Formatter", "type": "php", "active": true, "description": "Formats WooCommerce prices", "tags": ["woocommerce", "formatting"]}Supported fields
Section titled “Supported fields”| Field | Description |
|---|---|
id |
WordPress snippet ID. Used by push to update the correct snippet. |
name |
Snippet title in WordPress. Takes precedence over the filename. |
description |
Optional description shown in the WordPress admin. |
type |
Snippet type: php, css, js, html, or text. |
tags |
Array of tag strings. |
active |
Whether the snippet is active (true / false). |