Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mainwp-mintlify-c0f00f42.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

What You’ll Learn

  • Understanding the three code snippet types
  • Executing and saving code snippets
  • Removing snippets from child sites
  • Recovering from bad snippets
  • Useful wp-config.php snippets

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.

Code Snippet Types

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.
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.
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.

Remove a Code Snippet From a Broken Site

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.

Execute a Code Snippet

Execute a New Code Snippet

1

Log in to your MainWP Dashboard

2

Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page

Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet pageMainWP Code Snippets > Snippet Options
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.
7

Add your Code Snippet in the code editor

Add your Code Snippet in the code editor MainWP Code Snippets > Code Editor
8

Click the Save & Execute Snippet button

Execute a Saved Code Snippet

1

Log in to your MainWP Dashboard

2

Go to the MainWP > Add-ons > Administrative > Code Snippets > Saved Snippets page

3

Locate the Code Snippet that you want to execute

4

Click the Load button

Click the Load button MainWP Code Snippets > Load Snippet
5

Once the Code Snippet loads, click the Save & Execute Snippet button

Save a Code Snippet

Save a New Code Snippet

1

Log in to your MainWP Dashboard

2

Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet page

Go to the MainWP > Add-ons > Administrative > Code Snippets > Execute Snippet pageMainWP Code Snippets > Snippet Options
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.
7

Add your Code Snippet in the code editor

Add your Code Snippet in the code editor MainWP Code Snippets > Code Editor
8

Click the Save Snippet button

Edit a Saved Code Snippet

1

Log in to your MainWP Dashboard

2

Go to the MainWP > Add-ons > Administrative > Code Snippets > Saved Snippets page

3

Locate the Code Snippet that you want to edit

4

Click the Load button

Click the Load button MainWP Code Snippets > Load Snippet
5

Once the Code Snippet loads, edit wanted details

6

Click the Save Snippet button

Remove a Code Snippet From Child Sites

1

Log in to your MainWP Dashboard

2

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.

Useful Code Snippets

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}" );

Change the value of “Keep Records for” in MainWP Child Reports plugin

Code Snippets execute action with Return info from Child Sites option When running the script, make sure to select Return info from Child Sites.
global $wpdb;

$option = get_option( 'wp_mainwp_stream' );

if ( $option && isset( $option['general_records_ttl'] ) ) {
    $option['general_records_ttl'] = 31; // New desired value (e.g., 30 days)
    update_option( 'wp_mainwp_stream', $option );
}

Disable theme, plugin editor and plugin, core updates

This snippet goes to the wp-config.php file of your child sites. Be sure to select the correct third option when using the code snippet.
define('DISALLOW_FILE_EDIT',true); // theme, plugin
define('DISALLOW_FILE_MODS',true); // core, plugin

Disable CRON job feature

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define('DISABLE_WP_CRON', true);

Set auto-save interval

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define('AUTOSAVE_INTERVAL', 200);

Block external requests

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define('WP_HTTP_BLOCK_EXTERNAL', true);
This snippet blocks outbound HTTP requests. It can also prevent WordPress, plugin, and theme update checks unless you allow the required hosts.

Increase WordPress memory limit

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define('WP_MEMORY_LIMIT', '95M');

Enable Debug Mode

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define( 'WP_DEBUG', true );

Disable All Core Updates

This snippet goes to the wp-config.php file of your site. Be sure to select the correct third option when using the code snippet.
define( 'WP_AUTO_UPDATE_CORE', false );

Protect WordPress against malicious URL requests

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;
        }
    }
}
Source: WPSnip.com