Published on
Dec 17, 2024
Updated on
01 Jan 2025
Share

Learn all about WPMasterToolKit's "Code Snippets" module

Introduction

The Code Snippets module in WPMasterToolKit is designed to simplify the process of adding small pieces of custom code to your WordPress site. Whether you want to include globally executed PHP code or create reusable shortcodes, this module replaces the need for additional plugins dedicated to this task. With a user-friendly interface and built-in validation options, this module focuses on security and performance.

Which plugin can the Code Snippets module replace?

The Code Snippets module can replace popular plugins such as Code Snippets, WPCodeor any other plugin that lets you add PHP scripts or shortcodes to your site.

Main features

1. Two types of snippets

The module offers two types of snippets, depending on your needs:

  • Include File PHP code is included directly in the site, executed before the WordPress hooks. This type is ideal for adding custom actions or filters via add_action() or add_filter().
  • Shortcode The code is associated with a shortcode, which can be used in your pages or articles. This type is ideal for dynamically displaying content wherever you like.

2. Optimized code editor

The module features a code editor with support for CodeMirroroffering features such as :

  • Colorful PHP syntax
  • Autocomplete
  • Quick comments (Ctrl-/)
  • Advanced search (Alt-F)
  • Error indicators thanks to built-in validator
WordPress snippets editing interface with PHP code.

3. Automatic snippet validation

To avoid fatal errors, the module includes a PHP code validator. Before publishing or activating a snippet, your code is analyzed:

  • Syntax errors are detected.
  • If an error is present, it is displayed with details (line concerned and description).
Syntax error in PHP code.

4. Managing snippets in the WordPress interface

  • Snippets are managed via a Custom Post Type entitled "Code Snippets".
    WordPress snippets interface
  • A clear list shows all your snippets, with specific columns for description, type and status (active or inactive).
    Screenshot of the WordPress snippets management interface.
  • Active snippets are automatically generated as PHP files, ensuring that they run smoothly.
    PHP code extract displaying the current date.

5. Secure mode

In the event of a problem, activate secure mode by adding the following constant to your file wp-config.php :

define('WPMASTERTOOLKIT_SNIPPETS_SAFE_MODE', true);

This will temporarily disable all snippets to allow safe troubleshooting.

Discover the module : Disable comments

How do I use the Code Snippets module?

  1. Create a new snippet :
    • Go to "Code Snippets" in the WordPress dashboard.
    • Click on "Add New" and enter a descriptive title for the snippet.
    • Insert your PHP code in the editor.
  2. Choose snippet type :
    • Select Include File to execute code directly.
    • Select Shortcode to generate a shortcode that can be used in your pages, for example : [wpmtk_code_snippets id="123"]
      image
  3. Enabling or disabling a snippet :
    • Use the "Status" toggle to activate or deactivate a snippet without having to delete it.
      Plugin settings with button enabled
  4. Validate and save :
    • Before publishing, the validator automatically analyzes the code for potential errors.
  5. Regenerate all active snippets :
    • If necessary, use the "Regenerate Snippets Files" button to regenerate all active snippets files.
      WordPress interface for managing code snippets.

Our technical choices

PHP file generation

Active snippets are converted into PHP files stored in a dedicated directory (wp-content/wpmastertoolkit/code-snippets). This offers two advantages:

  • Performance Snippets are included via require_onceto reduce the load on the database.
  • Security Inactive or invalid snippets are not loaded.

Strict validation

Thanks to the WPMastertoolkit_PHP_Code_ValidatorWe analyze the code before executing it. This considerably reduces the risk of errors that could break the site.

Compatibility with WordPress best practices

The module is designed to integrate seamlessly into the WordPress ecosystem:

  • It uses the REST API and standard actions/filters.
  • Features such as custom column management and message types enrich the user experience.

Pre-validated shortcodes

To avoid unforeseen behavior, generated shortcodes are only active if the corresponding snippet is correctly validated and published.

Pro
from
2.50$
/Month
14
Days
Money-back guarantee
risk-free at 100 %!

Conclusion

WPMasterToolKit's Code Snippets module is an indispensable tool for any WordPress administrator looking to add custom code securely and easily. By replacing several specialized plugins with this single module, you centralize the management of your snippets while improving your site's performance. Try it today to get the most out of your WordPress projects!

Category
Custom Code
Version
≤ 1.2.0
Type
Free
More than 18 reviews
+1000
Installations
104
Modules
Pro
from
30.00$
/Year