### Activate WordPress Code Snippets Safe Mode Per-Page via URL Parameters Source: https://help.codesnippets.pro/en/article/12-safe-mode These examples demonstrate how to enable safe mode for a specific WordPress page by appending `&snippets-safe-mode=1` to its URL. This method is effective for logged-in administrators to troubleshoot snippet issues on a particular page without affecting other site visitors. ```URL Original Admin URL: https://yoursiteurl.com/wp-admin/admin.php?page=snippets Safe Mode Admin URL: https://yoursiteurl.com/wp-admin/admin.php?page=snippets&snippets-safe-mode=1 Original Front-End URL: https://yoursiteurl.com/about-us/ Safe Mode Front-End URL: https://yoursiteurl.com/about-us/?snippets-safe-mode=1 ``` -------------------------------- ### JavaScript Snippet Example: Popup Message and Paragraph Hiding Source: https://help.codesnippets.pro/en/article/18-why-you-need-javascript-snippets-codesnippets-pro This snippet demonstrates two basic JavaScript functionalities: displaying a welcome alert when the page loads, and hiding all paragraph elements when a specific button is clicked. It illustrates fundamental DOM manipulation and event handling, showcasing how JavaScript can add interactivity to a website. ```javascript // Show an alert message when the page loads window.onload = function() { alert('Welcome to my cool site!'); } // Hide all paragraphs when clicking the button with ID 'hide-btn' const button = document.getElementById('hide-btn'); const paragraphs = document.querySelectorAll('p'); button.addEventListener('click', function() { paragraphs.forEach(p => p.style.display = 'none'); }); ``` -------------------------------- ### XMLHttpRequest Basic Setup Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This JavaScript snippet initializes a new `XMLHttpRequest` object, sets the request method to POST and the URL, and configures the `Content-Type` header for sending form-urlencoded data. ```JavaScript const request = new XMLHttpRequest(); request.open('POST', data.ajaxurl, true); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); ``` -------------------------------- ### Add Facebook Pixel Base Code to WordPress Head Source: https://help.codesnippets.pro/en/article/7-how-to-add-a-facebook-pixel This PHP snippet demonstrates how to insert the Facebook Pixel base code into the `` section of a WordPress site. It leverages the `wp_head` action hook, making it suitable for use with the Code Snippets plugin for manual pixel installation. ```php add_action( 'wp_head', function () { ?> headings to blue and add 20 pixels of padding to all

paragraph elements across your site. It's a basic example of customizing visual elements without directly editing theme files. ```CSS /* Change the color of all headings to blue */ h1 { color: blue; } /* Add 20px of padding to paragraphs */ p { padding: 20px; } ``` -------------------------------- ### Get HTML Button Element by ID Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This JavaScript snippet retrieves the HTML button element with the ID 'shea-ajax-button' from the document, preparing it for event listening. ```JavaScript const ajaxButton = document.getElementById('shea-ajax-button'); ``` -------------------------------- ### Restrict WordPress snippet to multiple pages using an array Source: https://help.codesnippets.pro/en/article/42-can-i-run-my-snippet-on-just-one-page This PHP example illustrates how to restrict a WordPress code snippet to run on multiple specific pages. It uses the `is_page` function with an array containing various page identifiers (IDs, titles, slugs). The snippet is wrapped within an `add_action` hook, ensuring it only executes on the specified pages, allowing the rest of the snippet's logic to follow. ```PHP add_action( 'wp_footer', function () { if ( ! is_page( array( 42, 'Page Name', 'about-us', 968 ) ) ) return; // rest of snippet code here } ); ``` -------------------------------- ### PHP: Configure cURL Proxy for WordPress HTTP API Source: https://help.codesnippets.pro/en/article/62-license-activation-issues This PHP snippet, intended for `functions.php`, sets a proxy for cURL requests made via the WordPress HTTP API. It's a temporary solution for license activation issues caused by ISP blockages. Users should replace placeholders with actual proxy details and comment out the code after successful activation to prevent all future cURL requests from using the proxy. ```PHP add_action('http_api_curl',function($handle){ curl_setopt($handle, CURLOPT_PROXY,"REPLACE_ME_WITH_PROXY_IP"); curl_setopt($handle, CURLOPT_PROXYPORT, REPLACE_ME_WITH_PROXY_PORT); }, 10); ``` -------------------------------- ### Bookmarklet to Toggle WordPress Code Snippets Safe Mode Source: https://help.codesnippets.pro/en/article/12-safe-mode This JavaScript bookmarklet provides a convenient shortcut to activate safe mode for the current WordPress page. When executed from the browser's bookmarks, it automatically appends the necessary `snippets-safe-mode=1` parameter to the URL, allowing quick access to troubleshooting for administrators. ```JavaScript javascript:window.location.href+=(-1===window.location.href.indexOf('?')?'?':'&')+'snippets-safe-mode=1' ``` -------------------------------- ### JavaScript Data Initialization from PHP Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This JavaScript line converts the PHP-prepared `$data` variable into a JavaScript constant `data`, making the AJAX URL, action, and nonce accessible on the client-side. ```JavaScript const data = ; ``` -------------------------------- ### Loading a WordPress Snippet in the Footer using add_action Source: https://help.codesnippets.pro/en/article/47-snippet-action-hooks This PHP code demonstrates how to use the `add_action` hook to execute a custom function when the `wp_footer` hook is triggered. This ensures the snippet's code runs just before the closing `` tag of a WordPress page, making it suitable for scripts or content that should appear at the end of the page. ```PHP add_action( 'wp_footer', function () { // code here } ); ``` -------------------------------- ### Add Microsoft Clarity Tracking Code to WordPress Head Source: https://help.codesnippets.pro/en/article/6-how-to-add-microsoft-clarity-tracking-code This PHP snippet integrates Microsoft Clarity tracking into a WordPress site by hooking the Clarity JavaScript code into the `wp_head` action. It ensures the tracking script is loaded within the `` section of every page. Users must replace 'XXXXXXXXXX' with their specific Microsoft Clarity project ID. ```php add_action( 'wp_head', function () { ?> admin_url( 'admin-ajax.php' ), 'action' => ACTION_NAME, 'nonce' => wp_create_nonce( ACTION_NAME ), ]; ?> section of a WordPress site. Users should replace the placeholder comment with their desired HTML, CSS, or JavaScript. ```PHP add_action( 'wp_head', function () { ?> admin_url( 'admin-ajax.php' ), 'action' => ACTION_NAME, 'nonce' => wp_create_nonce( ACTION_NAME ), ]; ``` -------------------------------- ### Add Code to Website Footer Section (PHP) Source: https://help.codesnippets.pro/en/article/41-how-can-i-add-code-to-my-site-s-header-or-footer This PHP snippet utilizes the 'wp_footer' action to insert custom code just before the closing tag of a WordPress site. It is suitable for JavaScript libraries or other scripts that should load at the end of the page, and users should replace the placeholder comment with their code. ```PHP add_action( 'wp_footer', function () { ?> ` tag, ensuring Google Analytics is loaded correctly. Remember to replace `UA-XXXXXXXX-X` with your actual Google Analytics Tracking ID. ```PHP add_action( 'wp_head', function () { ?> Click me for AJAX'; } ); ``` -------------------------------- ### Add Click Event Listener to Button Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This JavaScript code attaches a click event listener to the `ajaxButton` element, defining an anonymous function to be executed when the button is clicked. This function will contain the AJAX request logic. ```JavaScript ajaxButton.addEventListener('click', () => { // code goes here }); ``` -------------------------------- ### Send JSON Success Response from WordPress AJAX Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This PHP function wp_send_json_success() sends a JSON-encoded success response back to the client from a WordPress AJAX handler. It automatically sets the correct HTTP headers and terminates the script, making it ideal for returning data or status messages. ```PHP wp_send_json_success( 'it works!' ); ``` -------------------------------- ### Define XMLHttpRequest onload Handler Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This JavaScript snippet defines the `onload` event handler for the `XMLHttpRequest` object. The code within this function will execute once a response is received from the server. ```JavaScript request.onload = () => { // this code will run when we get a response back from PHP. }; ``` -------------------------------- ### Define PHP Constant for WordPress AJAX Action Source: https://help.codesnippets.pro/en/article/48-ajax-within-snippets This PHP snippet defines a constant ACTION_NAME to hold the unique string identifier for the AJAX action. Using a constant promotes code clarity and reusability across different parts of the AJAX implementation. ```PHP const ACTION_NAME = 'shea_ajax_example'; ``` -------------------------------- ### Conditionally Run CSS on Specific WordPress Pages Source: https://help.codesnippets.pro/en/article/46-how-can-i-make-a-snippet-run-on-only-a-certain-page This PHP snippet demonstrates how to use WordPress conditional tags to ensure that embedded CSS code only executes on pages matching specific criteria, such as page ID, URL slug, or title. It leverages the 'wp_head' action hook to inject the CSS into the page's header, but only after checking the page conditions. This prevents the CSS from loading unnecessarily on other parts of the site. ```php add_action( 'wp_head', function () { if ( ! is_page( [ 123, 'some-page-slug', 'Some Page Title' ] ) ) { return; } ?> request.status || 400 <= request.status) { alert('AJAX failure :( We received error code ' + request.status); return; } ``` -------------------------------- ### PHP snippet to disable comments in WordPress Source: https://help.codesnippets.pro/en/article/52-disable-comments-in-wordpress-by-roy-eyal This PHP snippet completely disables comments across a WordPress site. It handles various aspects such as redirecting users from the comments page, removing the comments metabox from the dashboard, disabling comment and trackback support for all post types, closing comments on the front-end, hiding existing comments, removing the comments page from the admin menu, and removing comments links from the admin bar. ```PHP