Active PHP snippets silently stop executing
-
Environment
- WPCode Lite: 2.3.5 (latest)
- WordPress: 6.9.4
- PHP: 8.2.30 (ZTS)
- Hosting: SiteGround Cloud
- Object cache: Not enabled (standard wp_options autoload)
- Active PHP snippets: ~59
Summary
All active PHP snippets (location: “Run Everywhere”) silently stop executing. No errors are logged, no snippets are deactivated — they simply don’t run. The
wpcode_snippetsoption inwp_optionsexists and contains what appears to be valid cached data, but the snippets are not executed on page load.This affects all PHP snippet types including shortcode registrations,
add_filter()/add_action()hooks, and custom URL routing — meaning entire sections of the site break silently when this occurs.ReproductionThis issue is persistent on our installation. Every time we rely solely on WPCode’s native snippet loader (the
wpcode_snippetsoption cache), PHP snippets do not execute. We have confirmed this by:- Creating a workaround mu-plugin that loads snippets directly from the database (bypassing WPCode’s cache) — all snippets work correctly.
- Disabling the mu-plugin and flushing all caches (
wp cache flush,wp transient delete --all) — snippets immediately stop executing. In our case, this breaks custom post type URL routing (registered via a WPCode snippet), causing hotel pages on our website to 404 and redirect to the homepage. - Re-enabling the mu-plugin — everything works again immediately.
This has been tested multiple times with the same result. The issue is 100% reproducible.Suspected root cause
After tracing through the WPCode source code, I believe the issue is in how the snippet cache interacts with the
WPCode_Snippetclass:1.get_data_for_caching()omits theactivepropertyIn
class-wpcode-snippet.php(around line 935), theget_data_for_caching()method returns an array withid,title,code,code_type,location,auto_insert,insert_number,use_rules,rules,priority,location_extra,shortcode_attributes,compiled_code, andmodified— but does not includeactive.2. Cache-loaded snippets haveis_active() === falseWhen a snippet is reconstructed from cached array data:
load_from_array()iteratesget_object_vars($this)and sets matching properties. Sinceactiveis not in the cached array,$this->activeremainsnull.$this->post_datais also never set (it’s only set when loading from aWP_Postobject or by ID from the database).- When
is_active()is called, it checks!isset($this->active)→true(sincenull), then falls through toisset($this->post_data->post_status) && 'publish' === $this->post_data->post_status→false(sincepost_datais not set). - Result: every snippet loaded from cache reports
is_active() === false.
3. This blocks shortcode execution
In
includes/shortcode.phpline 41, shortcode output is gated by:if ( ! $snippet->is_active() ) { return ''; }Since
is_active()returnsfalsefor all cache-loaded snippets, no shortcode registered by a WPCode snippet produces any output.4. The “Run Everywhere” auto-insert path may also be affectedWhile
WPCode_Auto_Insert_Everywhere::run_snippets()doesn’t directly callis_active(), it passes snippet output through thewpcode_snippet_output_phpandwpcode_snippet_outputfilters. If any other part of the system checksis_active()during this flow, it would also fail.Suggested fixAdd
activeto the cached data array inget_data_for_caching():return array( 'id' => $this->get_id(), 'title' => $this->get_title(), 'active' => $this->is_active(), // <-- ADD THIS 'code' => $this->get_code(), 'code_type' => $this->get_code_type(), // ... rest of array );Since only active/published snippets are cached (the cache is built from
cache_all_loaded_snippets()which queries published snippets), this value will always betruein practice — but without it,is_active()can’t determine the snippet’s status whenpost_dataisn’t available.Current workaroundWe are running a must-use plugin (
wp-content/mu-plugins/fix-wpcode-cache.php) that:- Hooks into
plugins_loadedat priority 3 (before WPCode’s priority 5) - Queries the database directly for all published PHP snippets (
post_type = 'wpcode',post_status = 'publish', taxonomywpcode_type=php, plus_wpcode_active = 1meta) eval()s each snippet’spost_content- Suppresses WPCode’s own PHP execution via
add_filter('wpcode_snippet_output_php', '__return_empty_string', 1)to prevent redeclaration errors
This workaround has been stable for several months but adds an extra database query on every request, which is not ideal.
Note on version 2.3.5 changelog
The 2.3.5 changelog mentions: “Resolved a fatal error that could occur when the snippet library cache returns invalid data.” This fix appears to address a different symptom (a fatal error from malformed cache data) but does not fix the underlying issue of snippets silently not executing due to the missing
activeproperty in cached data.
You must be logged in to reply to this topic.