{"id":1794,"date":"2024-12-17T10:47:10","date_gmt":"2024-12-17T09:47:10","guid":{"rendered":"https:\/\/wpmastertoolkit.com\/module\/code-snippets\/"},"modified":"2025-01-01T12:03:22","modified_gmt":"2025-01-01T11:03:22","slug":"code-snippets","status":"publish","type":"module","link":"https:\/\/wpmastertoolkit.com\/en\/module\/code-snippets\/","title":{"rendered":"Code Snippets"},"content":{"rendered":"<h1 class=\"wp-block-heading\">Learn all about WPMasterToolKit's \"Code Snippets\" module<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>The Code Snippets module in <strong>WPMasterToolKit<\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Which plugin can the Code Snippets module replace?<\/h2>\n\n\n\n<p>The Code Snippets module can replace popular plugins such as <strong>Code Snippets<\/strong>, <strong>WPCode<\/strong>or any other plugin that lets you add PHP scripts or shortcodes to your site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Main features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Two types of snippets<\/h3>\n\n\n\n<p>The module offers two types of snippets, depending on your needs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Include File<\/strong> PHP code is included directly in the site, executed before the WordPress hooks. This type is ideal for adding custom actions or filters via <code data-no-auto-translation=\"\">add_action()<\/code> or <code data-no-auto-translation=\"\">add_filter()<\/code>.<\/li>\n\n\n\n<li><strong>Shortcode<\/strong> 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.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Optimized code editor<\/h3>\n\n\n\n<p>The module features a code editor with support for <strong>CodeMirror<\/strong>offering features such as :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Colorful PHP syntax<\/li>\n\n\n\n<li>Autocomplete<\/li>\n\n\n\n<li>Quick comments (<code data-no-auto-translation=\"\">Ctrl-\/<\/code>)<\/li>\n\n\n\n<li>Advanced search (<code data-no-auto-translation=\"\">Alt-F<\/code>)<\/li>\n\n\n\n<li>Error indicators thanks to built-in validator<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img alt=\"WordPress snippets editing interface with PHP code.\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"408\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30-1024x408.png\" class=\"wp-image-2910\" srcset=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30-1024x408.png 1024w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30-300x120.png 300w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30-768x306.png 768w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30-1536x612.png 1536w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-30.png 1825w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Automatic snippet validation<\/h3>\n\n\n\n<p>To avoid fatal errors, the module includes a PHP code validator. Before publishing or activating a snippet, your code is analyzed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Syntax errors are detected.<\/li>\n\n\n\n<li>If an error is present, it is displayed with details (line concerned and description).<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img alt=\"Syntax error in PHP code.\" decoding=\"async\" width=\"638\" height=\"358\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-31.png\" class=\"wp-image-2911\" srcset=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-31.png 638w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-31-300x168.png 300w\" sizes=\"(max-width: 638px) 100vw, 638px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. Managing snippets in the WordPress interface<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Snippets are managed via a <strong>Custom Post Type<\/strong> entitled \"Code Snippets\".<figure class=\"wp-block-image size-full\"><img alt=\"WordPress snippets interface\" decoding=\"async\" width=\"158\" height=\"134\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-32.png\" class=\"wp-image-2912\"><\/figure><\/li>\n\n\n\n<li>A clear list shows all your snippets, with specific columns for description, type and status (active or inactive).<figure class=\"wp-block-image size-large\"><img alt=\"Screenshot of the WordPress snippets management interface.\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"464\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-33-1024x464.png\" class=\"wp-image-2913\" srcset=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-33-1024x464.png 1024w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-33-300x136.png 300w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-33-768x348.png 768w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-33.png 1256w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li>\n\n\n\n<li>Active snippets are automatically generated as PHP files, ensuring that they run smoothly.<figure class=\"wp-block-image size-large\"><img alt=\"PHP code extract displaying the current date.\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"762\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-34-1024x762.png\" class=\"wp-image-2914\" srcset=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-34-1024x762.png 1024w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-34-300x223.png 300w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-34-768x572.png 768w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-34.png 1252w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. Secure mode<\/h3>\n\n\n\n<p>In the event of a problem, activate secure mode by adding the following constant to your file <strong>wp-config.php<\/strong> :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\" data-no-auto-translation=\"\">define('WPMASTERTOOLKIT_SNIPPETS_SAFE_MODE', true);\n<\/code><\/pre>\n\n\n\n<p>This will temporarily disable all snippets to allow safe troubleshooting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How do I use the Code Snippets module?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a new snippet<\/strong> :\n<ul class=\"wp-block-list\">\n<li>Go to \"Code Snippets\" in the WordPress dashboard.<\/li>\n\n\n\n<li>Click on \"Add New\" and enter a descriptive title for the snippet.<\/li>\n\n\n\n<li>Insert your PHP code in the editor.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Choose snippet type<\/strong> :\n<ul class=\"wp-block-list\">\n<li>Select <strong>Include File<\/strong> to execute code directly.<\/li>\n\n\n\n<li>Select <strong>Shortcode<\/strong> to generate a shortcode that can be used in your pages, for example : <code data-no-auto-translation=\"\">[wpmtk_code_snippets id=\"123\"]<\/code><figure class=\"wp-block-image size-full\"><img alt=\"image\" loading=\"lazy\" decoding=\"async\" width=\"301\" height=\"254\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-35.png\" class=\"wp-image-2915\"><\/figure><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Enabling or disabling a snippet<\/strong> :\n<ul class=\"wp-block-list\">\n<li>Use the \"Status\" toggle to activate or deactivate a snippet without having to delete it.<figure class=\"wp-block-image size-full\"><img alt=\"Plugin settings with button enabled\" loading=\"lazy\" decoding=\"async\" width=\"292\" height=\"241\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-36.png\" class=\"wp-image-2916\"><\/figure><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Validate and save<\/strong> :\n<ul class=\"wp-block-list\">\n<li>Before publishing, the validator automatically analyzes the code for potential errors.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Regenerate all active snippets<\/strong> :\n<ul class=\"wp-block-list\">\n<li>If necessary, use the \"Regenerate Snippets Files\" button to regenerate all active snippets files.<figure class=\"wp-block-image size-large\"><img alt=\"WordPress interface for managing code snippets.\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"457\" src=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-37-1024x457.png\" class=\"wp-image-2918\" srcset=\"https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-37-1024x457.png 1024w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-37-300x134.png 300w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-37-768x343.png 768w, https:\/\/wpmastertoolkit.com\/wp-content\/uploads\/2024\/12\/image-37.png 1250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Our technical choices<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">PHP file generation<\/h3>\n\n\n\n<p>Active snippets are converted into PHP files stored in a dedicated directory (<code data-no-auto-translation=\"\">wp-content\/wpmastertoolkit\/code-snippets<\/code>). This offers two advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Performance<\/strong> Snippets are included via <code data-no-auto-translation=\"\">require_once<\/code>to reduce the load on the database.<\/li>\n\n\n\n<li><strong>Security<\/strong> Inactive or invalid snippets are not loaded.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Strict validation<\/h3>\n\n\n\n<p>Thanks to the <code data-no-auto-translation=\"\">WPMastertoolkit_PHP_Code_Validator<\/code>We analyze the code before executing it. This considerably reduces the risk of errors that could break the site.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compatibility with WordPress best practices<\/h3>\n\n\n\n<p>The module is designed to integrate seamlessly into the WordPress ecosystem:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It uses the REST API and standard actions\/filters.<\/li>\n\n\n\n<li>Features such as custom column management and message types enrich the user experience.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Pre-validated shortcodes<\/h3>\n\n\n\n<p>To avoid unforeseen behavior, generated shortcodes are only active if the corresponding snippet is correctly validated and published.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>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!<\/p>","protected":false},"excerpt":{"rendered":"<p>Simplify the process of adding custom code to your WordPress site with WPMasterToolKit's Code Snippets module. This all-in-one module lets you securely create, manage and validate PHP code snippets or shortcodes, without the need for additional plugins. With an integrated editor, secure mode and optimized file management, improve your site's performance and flexibility in the blink of an eye!<\/p>","protected":false},"featured_media":0,"parent":0,"template":"","meta":{"_acf_changed":true,"_seopress_robots_primary_cat":"","_seopress_titles_title":"Optimisez WordPress avec Code Snippets sans plugins !","_seopress_titles_desc":"Simplifiez l'ajout de code sur WordPress avec \u00ab Code Snippets \u00bb. Gagnez en s\u00e9curit\u00e9 et performance tout en rempla\u00e7ant les plugins d\u00e9di\u00e9s.","_seopress_robots_index":"","_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true},"class_list":["post-1794","module","type-module","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/wpmastertoolkit.com\/en\/wp-json\/wp\/v2\/module\/1794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmastertoolkit.com\/en\/wp-json\/wp\/v2\/module"}],"about":[{"href":"https:\/\/wpmastertoolkit.com\/en\/wp-json\/wp\/v2\/types\/module"}],"wp:attachment":[{"href":"https:\/\/wpmastertoolkit.com\/en\/wp-json\/wp\/v2\/media?parent=1794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}