Table of Contents

Code Snippets Extension

MainWP Code Snippets Extension is a simple way to add code snippets to your child sites from one centralized location.

The code snippet is a small chunk of PHP code inserted directly into your child sites. 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 Snippet Extension allows you to run code in three ways:

Have a Code Snippet execute a function on the Child site

This type of snippet will add the snippet on your child site, and it will be executed on-site load. This type of snippets will be saved in your child site database and executed each time your child site is loaded by the PHP command eval().

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.

Have a Code Snippet return information from a Child site

This type of snippet will get the info from child sites and display it in the Output Display. 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.

Have a Code Snippet edit the wp-config.php file on a Child site

This type of snippets will be added to your child site wp-config.php file. This type of snippets will be saved only in your child site wp-config.php 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!

Important Notes

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 execute a bad code snippet and you 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. Login in to your cPanel, locate the phpMyAdmin, and open your site database
  2. Locate and open the wp_options table
  3. Locate and remove the option_name = mainwp_ext_snippets_enabled row, or option_name = mainwp_ext_code_snippets.

This will remove all code snippets that were applied using our Code Snippets extension.

If you use bad snippets that are saved to the WP-Config.php, the file will need to be edited manually!

Execute a Code Snippet

Execute a New Code Snippet

  1. Login in to your MainWP Dashboard
  2. Go to the MainWP > Extensions > 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
    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 editorMainWP Code Snippets > Code Editor
  8. Click the Save & Execute Snippet button

Execute a Saved Code Snippet

  1. Login in to your MainWP Dashboard
  2. Go to the MainWP > Extensions > Code Snippets > Saved Snippets page
  3. Locate the Code Snippet that you want to execute
  4. Click the Load buttonMainWP 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. Login in to your MainWP Dashboard
  2. Go to the MainWP > Extensions > 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
    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 editorMainWP Code Snippets > Code Editor
  8. Click the Save Snippet button

Edit a Saved Code Snippet

  1. Login in to your MainWP Dashboard
  2. Go to the MainWP > Extensions > Code Snippets > Saved Snippets page
  3. Locate the Code Snippet that you want to edit
  4. 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. Login in to your MainWP Dashboard
  2. Go to the MainWP > Extensions > 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
  6. Click the Save & Execute Snippet button.

This process will remove the snippet from all your site 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 Save Snippets list so it can be used again in the future. If you want to remove the snippet from all child sites, you can un-check 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 this document do to see how to fix it.

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 (3rd) 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 (3rd) 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 (3rd) 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 (3rd) option when using the code snippet.

define('WP_HTTP_BLOCK_EXTERNAL', true);
This Snippet will hide plugin and theme updates. Because of that, you won’t be able to see it in your Dashboard too.

Increase WordPress memory limit

This snippet goes to the wp-config.php file of your site. Be sure to select the correct (3rd) 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 (3rd) 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 3rd) option when using the code snippet.

define( 'WP_AUTO_UPDATE_CORE', false );

Protect WordPress against malicious URL requests

This snippet has to be saved to the functions.php file so be sure to select the “Execute on Child Sites” options when executing the snippet.

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

Still Have a Questions?
Search for additional solutions in the MainWP Community or start your own discussion