Table of Contents
This guide walks through creating a simple PHP snippet from scratch.
Step 1 — Open the Add New screen #
From GravityOps > Code Snippets, click Add New.
Step 2 — Fill in the details #
- Name — A descriptive name, e.g. “Remove admin bar for subscribers”.
- Description — Optional. Included in snippet search and in the generated file’s header.
- Type — Choose PHP, JavaScript, or CSS. Custom snippets can be changed later; GitHub-sourced snippets are locked to the type declared in their manifest.
- Source Code — Paste or type your code. For PHP, include the opening
<?phptag.
Step 3 — Set scope (PHP snippets) #
In the Scope card on the right:
- Hook — The WordPress action hook to run the snippet on. Default:
init. - Priority — Hook priority. Default:
10. - Context — Where to run: Everywhere, Frontend only, Admin only, Login page, REST requests, or Cron only. Admin only and Frontend only skip AJAX requests, and Frontend only also skips REST requests.
Step 4 — Save #
Click Save Snippet. The snippet is saved as inactive.
Step 5 — Activate #
Click Activate under the snippet’s name in the snippets list, or the Activate button on the edit screen. GOCSM validates the source, generates the file, and sets the snippet to active. PHP snippets use PHP’s parser for syntax validation. JavaScript snippets are checked for balanced brackets only. CSS snippets are checked for balanced braces, and @import is rejected.
PHP example #
<?php
add_filter( 'show_admin_bar', function( $show ) {
if ( current_user_can( 'subscriber' ) ) {
return false;
}
return $show;
} );