OCodeInsert v1.1.0
Insert custom HTML, JavaScript, and CSS into any page without touching theme files. Snippets survive theme updates, target by page type, URL, and user — with safe mode to prevent site breakage.
What OCodeInsert does
A lightweight alternative to editing functions.php or installing bloated tools like WPCode — built for inserting snippets that survive theme updates and stay organised.
?oci_safe=1 URL bypasswp-codemirrorwp_options — lightweight, portable, easy to back upfunctions.php, header.php, bloated alternatives like WPCode / Insert Headers and Footers, or relying on theme option panels for script injection.Getting installed
From WordPress admin
ocodeinsert.zip and click Install Now, then Activate.Manual (FTP / SFTP)
ocodeinsert.zip.ocodeinsert/ folder to /wp-content/plugins/.wp_options. No database tables are created. The plugin is immediately ready to use.Up and running in minutes
Adding your first snippet — a Google Analytics 4 script injected into every page's <head>.
The script now appears inside <head> on every page, automatically wrapped in <script> tags.
Types, locations & targeting
Snippet types
| Type | Badge | Auto-wrap behaviour |
|---|---|---|
html | HTML | Raw output — no wrapping applied |
js | JavaScript | Wraps bare JS in <script>…</script>. If code already starts with <script, skipped. |
css | CSS | Wraps bare CSS in <style>…</style>. If code already starts with <style, skipped. |
The Type field drives syntax highlighting, auto-wrap logic, and the visual badge in the list. It does not control where the snippet runs — that is Location.
Injection locations
<head>wp_footer for page builders.<p> in article body.</body>. Chat widgets, deferred scripts.the_content filter, OCodeInsert fires a JS fallback via wp_footer that finds the first and last <p> in the article body.Page type targeting
| Value | Matches |
|---|---|
all | Every page (default) — overrides all others when selected |
front | Front page — is_front_page() |
single | Single posts, pages, CPTs — is_singular() |
archive | Category, tag, date archives + blog index |
search | Search results — is_search() |
404 | 404 error page — is_404() |
URL pattern targeting
Comma-separated URL paths relative to the domain root. Matched against $_SERVER['REQUEST_URI']. Case-insensitive, trailing slashes ignored. Applied in addition to Page Type — both must pass.
/about, /blog/*, /shop/product-category/*
| Pattern | Matches |
|---|---|
/about | /about and /about/ only |
/blog/* | /blog/, /blog/my-post, /blog/2024/01/post |
/shop/*, /cart | Any URL under /shop/ plus exactly /cart |
User condition & priority
| Setting | Value | Behaviour |
|---|---|---|
| User condition | all | All visitors (default) |
logged_in | Logged-in users only | |
logged_out | Logged-out visitors only | |
| Priority | 1–99 | Controls injection order within the same location. Lower = earlier. Default: 10. |
Admin interface
Navigate to OCodeInsert in the WordPress sidebar. Single menu entry — no submenu interference.
Snippets list columns
| Column | Description |
|---|---|
| Status | Toggle Active (green) / Paused (grey) without reloading the page |
| Title | Snippet name |
| Type | HTML JavaScript CSS badge |
| Location | Injection point |
| Targeting | Page type + user condition summary |
| Priority | Numeric priority within the location |
| Actions | Edit · Duplicate · Export JSON · Delete |
The filter toolbar supports live search by title (client-side) and filtering by Type, Status, and Location.
Snippet editor
Click + New Snippet or the edit icon on any row. The modal contains: Basic Info (title, type, location, priority), CodeMirror code editor (mode switches on type change), Targeting (page types, URL patterns, user condition), and an Active checkbox to save as draft.
Duplicate & export
Duplicate: Click the copy icon in Actions. The copy gets a new ID, (Copy) appended to the title, and is saved as inactive by default.
Export single snippet Free: Click the download icon to export a snippet as .json, generated client-side.
JSON{
"id": "...",
"title": "My Snippet",
"type": "js",
"code": "console.log('hello');",
"location": "head",
"active": true,
"priority": 10,
"pages": "",
"page_types": ["all"],
"user_condition": "all",
"created": "2025-01-01 12:00:00"
}
Settings page
Navigate to OCodeInsert → Settings via the top navigation tabs.
Global enable / disable
Master switch for the entire plugin. When disabled: no snippets are injected anywhere, the admin interface remains accessible, and all snippet data is preserved. Useful for temporary maintenance or debugging without deactivating the plugin.
Role permissions
Choose which WordPress roles can access the OCodeInsert admin panel. Administrators always have access. Default: Administrators only. Adding Editor lets editors manage snippets without full admin access.
Error log
Maintains an internal log of issues during snippet processing (max 50 entries). Columns: Time, Snippet ID, Message. Click Clear Log to delete all entries.
Safe mode
Safe mode suspends all snippet output globally to protect your site when a snippet causes unexpected issues.
Activating safe mode
| Method | Effect |
|---|---|
| Admin toggle | OCodeInsert → Settings → enable Safe Mode → Save. Affects all visitors. An amber notice bar appears in the snippets admin. |
| URL parameter | Append ?oci_safe=1 to any frontend URL. Disables snippets for that request only — no other visitors are affected. |
https://example.com/my-page/?oci_safe=1
Diagnosing issues
?oci_safe=1 to confirm the issue is snippet-related.Evaluation order
All conditions must pass for a snippet to be injected:
- 1Is the plugin globally enabled?
- 2Is Safe Mode active?
- 3Is
?oci_safe=1in the current URL? - 4Does the snippet pass page type targeting?
- 5Does the snippet pass user condition?
- 6Does the snippet pass URL pattern matching?
- 7Is the snippet set to
active?
CodeMirror editor
OCodeInsert uses WordPress's built-in CodeMirror editor (wp-codemirror) — no external CDN or library dependencies. Loaded only on the Snippets page, not the Settings page.
- Syntax highlighting for HTML, JavaScript, and CSS
- Line numbers
- Bracket and tag matching
- Auto-indent
- Mode switches automatically when you change the Type dropdown
| Type | CodeMirror mode |
|---|---|
| HTML | text/html — HTML mixed mode, supports embedded JS and CSS |
| JavaScript | text/javascript |
| CSS | text/css |
Data storage & uninstall
OCodeInsert uses WordPress wp_options exclusively — no custom database tables. All snippets load from a single get_option() call per page load.
Options stored
| Option key | Contents |
|---|---|
oci_code_snippets | JSON-encoded array of all snippets |
oci_settings | Plugin settings: enabled, safe_mode, allowed_roles |
oci_error_log | Array of error log entries (max 50) |
Snippet schema
JSON{
"id": "uuid-v4",
"title": "string",
"type": "html | js | css",
"code": "string (raw code)",
"location": "head | before_content | after_content | footer",
"active": true,
"priority": 10,
"pages": "comma-separated URL patterns or empty string",
"page_types": ["all"],
"user_condition": "all | logged_in | logged_out",
"created": "Y-m-d H:i:s"
}
Uninstall
Deleting via Plugins → Delete (not just deactivating) runs uninstall.php and removes oci_code_snippets, oci_settings, and oci_error_log.
OCodeInsert Pro
Pro features are visible in the free UI with a PRO badge but non-functional until upgraded.
.json bundle. Import single snippets or bundles — imported with new UUIDs, saved inactive by default.HTTP_USER_AGENT: all, mobile + tablet, or desktop browsers.wp oci list, toggle, disable-all, safe-mode, export, import commands.WP-CLI commands (Pro)
BASHwp oci list # List all snippets
wp oci list --status=active # Active snippets only
wp oci toggle <snippet-id> # Toggle on/off by ID
wp oci disable-all # Disable all snippets
wp oci safe-mode on # Enable safe mode
wp oci export --file=snippets.json # Export all snippets
wp oci import --file=snippets.json # Import snippets
Developer reference
All PHP methods are static and available after init.
PHP API — OCI_Snippets
PHP// Get all snippets
$snippets = OCI_Snippets::get_all();
// Get a single snippet by UUID
$snippet = OCI_Snippets::get_by_id( 'uuid' );
// Save (create or update)
$id = OCI_Snippets::save_snippet([
'id' => '', // empty = create new
'title' => 'My Script',
'type' => 'js', // html | js | css
'code' => 'alert(1);',
'location' => 'footer', // head | before_content | after_content | footer
'active' => true,
'priority' => 10,
'pages' => '', // comma-separated URL patterns
'page_types' => ['all'],
'user_condition' => 'all', // all | logged_in | logged_out
]);
// Delete
OCI_Snippets::delete_snippet( 'uuid' );
// Toggle active state — returns new state (bool)
$is_active = OCI_Snippets::toggle_snippet( 'uuid' );
// Force-disable (always sets active=false)
OCI_Snippets::disable_snippet( 'uuid' );
// Duplicate — returns new ID
$new_id = OCI_Snippets::duplicate_snippet( 'uuid' );
PHP API — OCI_Settings
PHP$settings = OCI_Settings::get_all();
$enabled = OCI_Settings::is_enabled();
$safe_mode = OCI_Settings::is_safe_mode();
$can_access = OCI_Settings::can_access();
// Error log
OCI_Settings::log_error( 'uuid', 'Error message' );
$log = OCI_Settings::get_error_log();
OCI_Settings::clear_error_log();
AJAX endpoints
All endpoints require nonce (from ociAdmin.nonce) and manage_options capability. Return wp_send_json_success() or wp_send_json_error().
| Action | Parameters | Response |
|---|---|---|
oci_save_snippet | snippet_id, title, type, code, location, active, priority, pages, page_types[], user_condition | {id, snippet} |
oci_delete_snippet | snippet_id | {} |
oci_toggle_snippet | snippet_id | {active: bool} |
oci_duplicate_snippet | snippet_id | {id, snippet} |
oci_import_snippets | json (string) | {imported: int} |
oci_save_settings | enabled, safe_mode, allowed_roles[] | {} |
oci_clear_error_log | — | {} |
Constants
| Constant | Default | Purpose |
|---|---|---|
OCI_VERSION | '1.1.0' | Plugin version string |
OCI_DIR | plugin_dir_path(__FILE__) | Absolute path to plugin directory |
OCI_URL | plugin_dir_url(__FILE__) | URL to plugin directory |
OCI_PRO | false | Set to true when a Pro license is active |
PHP// Enable Pro features in a custom build (wp-config.php or must-use plugin)
define( 'OCI_PRO', true );
Hooks (Pro)
PHP// Action: fires before a snippet is injected
add_action( 'oci_before_inject', function( array $snippet, string $location ) {
// $location = 'head' | 'before_content' | 'after_content' | 'footer'
}, 10, 2 );
// Filter: decide whether a snippet should be injected
add_filter( 'oci_should_inject', function( bool $should, array $snippet ): bool {
if ( $snippet['title'] === 'My Script' ) return false;
return $should;
}, 10, 2 );
// Helper: get all active snippets for a location
$snippets = oci_get_snippets( 'head' );
What's changed
- NewSnippet type field — HTML / JavaScript / CSS
- NewAuto-wrap JavaScript in
<script>tags - NewAuto-wrap CSS in
<style>tags - NewPage type targeting (front, single, archive, search, 404)
- NewUser condition targeting (all / logged-in / logged-out)
- NewFourth injection location: Footer (
wp_footer) - NewDuplicate snippet button
- NewExport single snippet as JSON (free)
- NewImport snippets from JSON (Pro)
- NewExport all snippets as JSON bundle (Pro)
- NewSafe mode — global toggle and
?oci_safe=1URL bypass - NewSettings page — global enable/disable, safe mode, role permissions
- NewError log in settings (max 50 entries)
- NewCodeMirror syntax highlighting editor via
wp-codemirror - NewLive search and filter toolbar in snippets list
- NewLight/dark mode toggle with localStorage persistence
- NewFull admin redesign — brand bar, top nav tabs, v2 design system
- NewClean
uninstall.php— removes all options on plugin deletion - NewPro feature stubs with upsell UI and Snippet Library card
- FixNavigation now uses tab-based routing — single WP sidebar entry, no submenu interference
- NewSnippet storage in WP options
- New3 injection locations (head, before content, after content)
- NewPriority system
- NewURL pattern targeting
- NewActive/inactive toggle
- NewAJAX admin (save, delete, toggle)
Frequently asked questions
wp_options, not plugin files. Updating or reinstalling the plugin does not delete snippet data.before_content or after_content, does the theme use the_content filter?functions.php for PHP.head and footer locations always work. For before_content / after_content, if the page builder bypasses the_content filter, OCodeInsert's JS fallback handles injection. For most page builders, use head or footer for maximum reliability.wp_options is optimal. OCodeInsert Pro automatically migrates to a dedicated database table when the count exceeds 50.?oci_safe=1 to any URL to disable snippets for just that page load without affecting other visitors.wp_options under oci_code_snippets as a JSON-encoded array. Do not edit it directly.Got a question about OCodeInsert?
Reach out directly — Kenneth replies within 24 hours.
