Understanding and Troubleshooting Code Snippets in WordPress Multisite
Content
Managing code snippets across a WordPress Multisite network can be incredibly powerful, but it sometimes introduces unique challenges. This guide covers the most common issues users face and provides clear solutions based on community experience.
Common Multisite Issues and Their Solutions
1. Snippet Execution Order (Network vs. Site-Specific)
Problem: Network-activated snippets run after site-specific snippets, even if they have a lower priority number. This can break functionality if a site-specific snippet tries to use a class defined in a network snippet.
Solution: This is the intended behavior. To ensure a network snippet runs first, you must use the 'Shared Network' mode. When creating a network-level snippet, uncheck the option 'Allow this snippet to be activated on individual sites on the network'. This will make the snippet run automatically on all sites before any site-specific snippets.
2. Missing Admin Menu for Site Administrators
Problem: Site administrators on individual subsites cannot see the Code Snippets menu, even though the plugin is network-activated.
Solution: The 'Snippets' admin menu is controlled by a network-wide setting.
- Go to Network Admin → Settings → Network Settings.
- In the 'Enable administration menus' section, ensure the checkbox for 'Snippets' is checked.
- Save the settings. Site admins should now have access.
3. The "Allow on Individual Sites" Option Doesn't Save or Work
Problem: The 'Allow this snippet to be activated on individual sites on the network' checkbox does not stay checked when saving a network snippet, or activating it returns a '500' or '400' error.
Solution: This was a known bug in older versions of the plugin. The Code Snippets team has stated that this issue was resolved in version 3.6.6. If you are experiencing this problem, the first step is to update the plugin to the latest version. If the problem persists after updating, try clearing any server or object caching on your site.
4. Snippets Not Copied to a Newly Created or Cloned Subsites
Problem: When a new subsite is created (manually or via a cloning plugin), shared network snippets are not automatically active on it.
Solution: By default, network snippets must be manually activated on new subsites. However, you can automate this with a custom PHP snippet. Use the wp_initialize_site action hook to programmatically activate specific network snippets by their ID whenever a new site is created.
add_action( 'wp_initialize_site', function () {
update_option( 'active_shared_network_snippets', array( 42, 152, 68 ) );
} );
Replace the numbers in the array (42, 152, 68) with the actual IDs of the snippets you want to be active by default.
5. Cannot Edit or Create Network Snippets
Problem: In the network admin, you are unable to save edits to a snippet, create new ones, or use functions like 'Clone'. The page may refresh without saving changes or show an error.
Solution: This has been linked to specific plugin versions (e.g., v2.14.0). The recommended action is to:
- Ensure you are running the latest version of the Code Snippets plugin.
- If the problem started immediately after an update, temporarily downgrading to a previous stable version may be a workaround while waiting for a patch. Always back up your site before attempting this.
- As a last resort, snippets can be edited directly in the database table
wp_ms_snippets, but this is only advised for advanced users.
Key Multisite Concepts
- Network Snippets: Created in the Network Admin dashboard. They have two modes:
- Run Everywhere: Snippet runs on all sites automatically (option is unchecked).
- Shared Network: Snippet appears in the list on every subsite where it can be activated individually (option is checked).
- Site-Specific Snippets: Created and managed on individual subsites. They only run on that specific site.
By understanding these common pitfalls and their solutions, you can effectively harness the power of the Code Snippets plugin to manage code across your entire WordPress Multisite network.
Related Support Threads Support
-
network activated scripts fire after site-specific once, intended or not?https://wordpress.org/support/topic/network-activated-scripts-fire-after-site-specific-once-intended-or-not/
-
Missing network settings to enable admin menushttps://wordpress.org/support/topic/missing-network-settings-to-enable-admin-menus/
-
I can’t see Code Snippet in Child Sitehttps://wordpress.org/support/topic/i-cant-see-code-snippet-in-child-site/
-
Snippet doesn’t work / multisitehttps://wordpress.org/support/topic/snippet-doesnt-work-multisite/
-
Edit snippet in Multisitehttps://wordpress.org/support/topic/edit-snippet-in-multisite/
-
Network activate individual snippetshttps://wordpress.org/support/topic/network-activate-individual-snippets/
-
Multisite Network Activate/Deactivate – is there documentation?https://wordpress.org/support/topic/multisite-network-activate-deactivate-is-there-documentation/
-
network activated scripts fire after site-specific once, intended or not?https://wordpress.org/support/topic/network-activated-scripts-fire-after-site-specific-once-intended-or-not-2/
-
Missing snippets in multisite wordpresshttps://wordpress.org/support/topic/missing-snippets-in-multisite-wordpress/
-
Running Code Snippet on Individual siteshttps://wordpress.org/support/topic/running-code-snippet-on-individual-sites/
-
Activate snippet network wide not workinghttps://wordpress.org/support/topic/activate-snippet-network-wide-not-working/
-
Can’t create snippets in cloned multisite (SOLVED)https://wordpress.org/support/topic/cant-create-snippets-in-cloned-multisite-solved/
-
‘Enable administration menus’ Not Saving Settings on Multisitehttps://wordpress.org/support/topic/enable-administration-menus-not-saving-settings-on-multisite/
-
ms snippets tablehttps://wordpress.org/support/topic/ms-snippets-table/
-
Admin of a subsite in network installation has no access to Code Snippetshttps://wordpress.org/support/topic/admin-of-a-subsite-in-network-installation-has-no-access-to-code-snippets/
-
Snippet on Individual Sites – Multisitehttps://wordpress.org/support/topic/snippet-on-individual-sites-multisite/
-
Multisite Network Level Snippets “Request failed with status code 400”https://wordpress.org/support/topic/multisite-network-level-snippets-request-failed-with-status-code-400/
-
Automatic snippets activation following a multisite creationhttps://wordpress.org/support/topic/automatic-snippets-activation-following-a-multisite-creation/
-
Issue on multisite with last plugin v 2.14.0https://wordpress.org/support/topic/issue-on-multisite-with-last-plugin-v-2-14-0/
-
Multisite Activation (Feature Request)https://wordpress.org/support/topic/multisite-activation-feature-request/
-
Necessary to have wp_ms_snippets for non-multi-sites?https://wordpress.org/support/topic/necessary-to-have-wp_ms_snippets-for-non-multi-sites/
-
Activate on individual sites – Multisitehttps://wordpress.org/support/topic/activate-on-individual-sites-multisite/