Posted on 1 Comment

How and why you would want to use the MainWP Code Snippets Extension on your MainWP Dashboard

Code snippet is a small block of code which can be used to alter or make a change to a plugin function or to extend a plugin. For example, I would like additional fields to show for a customer on checkout, and I am using WooCommerce on a site.

WordPress can be modified and improved in a number of ways by using code snippets on your connected child sites. The MainWP Code Snippets Extension for your MainWP Dashboard site is a way to execute code that would normally be in the form of updating the active themes functions.php file, or the child sites wp-config.php. When using the MainWP Code Snippets Extension, you can execute a function on the child sites, return information, or add to the wp-config.php.

MainWP Code Snippets extension has three different options for what will be done with the code snippet:

  • Execute function on the Child Site
  • Return information from the Child Site
  • Insert function to the wp-config.php file.

Example 1

Bypass logout confirmation on the My Account page when using WooCommerce on a child site.

Bypass logout confirmation on WooCommerce

function wc_bypass_logout_confirmation() {
    global $wp;
 
    if ( isset( $wp->query_vars['customer-logout'] ) ) {
        wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
        exit;
    }
}

add_action( 'template_redirect', 'wc_bypass_logout_confirmation' );

Example 2

Set the number of post revisions in the child site wp-config.php to 10.

Set post revisions to 10

define('WP_POST_REVISIONS', 10);

Example 3

Disable plugin and theme editor on child sites.

Disable WordPress file editor

define( 'DISALLOW_FILE_EDIT', true );

Github Repository

To make adding code snippets easier, MainWP has a GitHub repo for code snippet functions.

The GitHub repo is split into folders. One folder is for broken functions, which would be constants that can be added to the child site wp-config.php files. These are every available constant, from increasing the memory limit to disable theme and plugin editing to set the number of post revisions, which will be stored on the site.

The other major folder is > functions, which can be run on the child sites for specific plugins. The plugins covered are WooCommerce, Yoast SEO and a number of others. These plugin function code snippets will be added to over time.

The code snippets will improve productivity by enabling you to make changes quickly and efficiently on the child sites.

Get MainWP News and Notifications

Sebastian Moran on Github
Sebastian Moran
Sebastian Moran is a WordPress performance expert and technical liaison for MainWP.

1 thought on “How and why you would want to use the MainWP Code Snippets Extension on your MainWP Dashboard

  1. These code snippets will be very handy for anyone that deals with WooCommerce on child sites. Some real field tested ones as well.
    https://github.com/mainwp/Code-Snippets-Functions/tree/master/Execute%20a%20function%20on%20a%20child%20site/WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.