WordPress Code Snippets for MainWP is a simple way to add code snippets to your Child Sites from one centralized location. No need to manually edit your theme’s functions.php file or wp-config.php file.
Extension Add-on - This add-on provides standalone functionality within MainWP Dashboard. No third-party plugins required.
The MainWP Code Snippets Extension is a simple way to add code snippets to your child sites from one centralized location. A code snippet is a small chunk of PHP code sent to your child sites, saved on your child sites, or written to wp-config.php, depending on the snippet type. Sometimes they contain full functions; other times, they modify an existing function.
This tool is recommended for advanced users only. We only recommend utilizing it if you are an experienced PHP coder.
The MainWP Code Snippets Extension allows you to run code in three ways:
Execute on Child Sites
This type of snippet will save the snippet in your child site database and execute it when the child site loads. You would use this option to make actual changes to your child sites. For example, if you wanted to customize the admin footer across different sites in your network, you would use this option.
Return info from Child Sites
This type of snippet will get the info from child sites and display it in the Console output. This type of snippet will not be saved on your child sites. This option queries the child and returns the information to you. An example would be if you wanted to know the published post count of all of your sites.
Save to wp-config.php
This type of snippet will be added to your child site’s wp-config.php file and saved only in that file. One of the most important files in your WordPress installation is the wp-config.php file. A code snippet in this section allows you to do things such as increase WordPress memory, block external requests, disable WP-Cron, and much more.
MainWP is not responsible for the code that you run on your sites. Use this tool with extreme care and at your own risk. Run any code on a test site before releasing it on live sites. If you execute a bad code snippet and crash your child sites, you need to remove the code snippet from your sites.
Code snippets can be removed directly from the database. To do this:
1
Log in to your cPanel, locate phpMyAdmin, and open your site database
2
Locate and open the site's options table
The default table name is wp_options, but the prefix may be different on your site.
3
Remove the Code Snippets option rows
Locate and remove the option_name = mainwp_ext_snippets_enabled row, or the option_name = mainwp_ext_code_snippets row.
This will remove all database-saved code snippets that were applied using our Code Snippets extension. If you use bad snippets that are saved to wp-config.php, the file will need to be edited manually.
Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page
Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page
3
Enter a Snippet Title
4
Enter a Snippet Description (optional)
5
Select the snippet type
6
Select Child Sites where you want to execute the snippet
Select Child Sites where you want to execute the snippet NOTE: A snippet cannot be executed on both the selection of Sites and a selection of Tags. If it is already executed to either Sites or Tags, and you choose the other selection type, it will be removed from the existing selection.
Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page
Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page
3
Enter a Snippet Title
4
Enter a Snippet Description (optional)
5
Select the snippet type
6
Select Child Sites where you plan to save the snippet
Select Child Sites where you plan to save the snippet NOTE: A snippet cannot be saved on both the selection of sites and a selection of tags. If it is already saved to either sites or tags, and you choose the other selection type, it will be removed from the existing selection.
Go to the MainWP > Add-ons > Administrative > Code Snippets > Saved Snippets page
3
Locate the Code Snippet that you want to remove from your child sites
4
Click the Load button
5
Once the Code Snippet loads, unselect child sites where you want to remove the snippet
Once the Code Snippet loads, unselect child sites where you want to remove the snippet
6
Click the Save & Execute Snippet button.
This process will remove the snippet from all child sites and re-add it only on sites that are still selected. This method is recommended in case you want to keep your snippet saved in the Saved Snippets list so it can be used again in the future. If you want to remove the snippet from all child sites, you can uncheck all sites and click the Save & Execute Snippet button.
MainWP is not responsible for the code that you run on your sites. Use this tool with extreme care and at your own risk. It is recommended that you run any code on a test site before releasing it on live sites. In case you damage your site by using a bad code snippet, read Remove a Code Snippet From a Broken Site to see how to fix it.
Delete rows in the wp_mainwp_stream_meta and wp_mainwp_stream tables
This can be useful if the wp_mainwp_stream_meta and wp_mainwp_stream tables have grown large in size. To prevent them from growing this large in the future, consider lowering the number of days for which the records are kept. NOTE: This will delete all records in these two tables, which means that the Pro Reports reports will not have any information about events prior to this deletion.When running the script, make sure to select Return info from Child Sites.
global $wpdb;$wpdb->query( "TRUNCATE TABLE {$wpdb->mainwp_stream}" );$wpdb->query( "TRUNCATE TABLE {$wpdb->mainwp_streammeta}" );
Use the Execute on Child Sites option when executing this snippet. The extension saves this snippet in the child site’s database and runs it when the child site loads.
global $user_ID; if($user_ID) { if(!current_user_can('administrator')) { if (strlen($_SERVER['REQUEST_URI']) > 255 || stripos($_SERVER['REQUEST_URI'], "eval(") || stripos($_SERVER['REQUEST_URI'], "CONCAT") || stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") || stripos($_SERVER['REQUEST_URI'], "base64")) { @header("HTTP/1.1 414 Request-URI Too Long"); @header("Status: 414 Request-URI Too Long"); @header("Connection: Close"); @exit; } }}