### Markdown Documentation Example Source: https://docs.wpcodebox.com/snippet_types Use this format to provide context or instructions for your code snippets within WPCodeBox. ```markdown ## Snippet Usage The following snippet customizes the footer style by using the `wp_enqueue_styles` function to register new styles. **Note:** You'll want to clear any caches on your site after making changes. ``` -------------------------------- ### Add Notes or Documentation to Snippets Source: https://docs.wpcodebox.com/snippet_types Use TXT snippets for non-executable content like notes, explanations, or documentation related to your code snippets. This example includes a comment and a reminder. ```text // This snippet customizes the footer style. // Note: Make sure to clear cache after making changes. ``` -------------------------------- ### JavaScript Snippet Example Source: https://docs.wpcodebox.com/snippet_doesnt_run Use this format for JavaScript code when the snippet type is set to 'JS'. Do not include script tags. ```javascript console.log('Hello world'); ``` -------------------------------- ### Add JavaScript for Page Load Alert and AJAX Source: https://docs.wpcodebox.com/snippet_types Implement client-side interactivity with JavaScript snippets. This example shows an alert on page load and a placeholder for an AJAX request on button click. ```javascript // Display an alert when the page finishes loading document.addEventListener('DOMContentLoaded', function() { alert('Welcome to our website!'); }); // AJAX request example document.getElementById('load-more-button').addEventListener('click', function() { // Your AJAX request code here }); ``` -------------------------------- ### SCSS Snippet for Button Styling Source: https://docs.wpcodebox.com/snippet_types Utilize SCSS/LESS snippets for advanced CSS with features like variables and nesting. This SCSS example defines a primary color and styles a button. ```scss $primary-color: #007bff; .button { background-color: $primary-color; color: white; padding: 10px 20px; } ``` -------------------------------- ### Add Custom HTML Content to WordPress Source: https://docs.wpcodebox.com/snippet_types Insert custom HTML elements, forms, or content structures into your WordPress site using HTML snippets. This example adds a simple welcome banner. ```html

Welcome to Our Website!

Discover amazing products and services.

``` -------------------------------- ### Add Custom PHP Function to WordPress Footer Source: https://docs.wpcodebox.com/snippet_types Use PHP snippets to inject custom PHP code, like functions, into your WordPress site. This example adds a 'Hello, World!' message to the footer. ```php ` tags, select the 'HTML' snippet type instead of 'JS'. ```html ``` -------------------------------- ### Customize CSS for WordPress Elements Source: https://docs.wpcodebox.com/snippet_types Apply custom styles to your WordPress site using CSS snippets. This example changes the header background color and increases post title font size. ```css /* Customize the header background color */ .site-header { background-color: #f2f2f2; } /* Increase the font size of post titles */ .entry-title { font-size: 24px; } ``` -------------------------------- ### Enable Safe Mode via URL Parameter Source: https://docs.wpcodebox.com/safe_mode Append the safe_mode=1 parameter to the WPCodeBox admin URL to bypass snippet execution when unable to access the site normally. ```text https://site.com/wp-admin/admin.php?page=wpcodebox2&safe_mode=1 ``` -------------------------------- ### Importing an SCSS Partial Source: https://docs.wpcodebox.com/scss_partials Use the @use directive with the partial filename enclosed in single quotes to include it in another snippet. ```scss @use '_typography'; ``` -------------------------------- ### Execute a basic shortcode Source: https://docs.wpcodebox.com/shortcodes Use the defined shortcode name within brackets to render the snippet content on a page or post. ```text [my_snippet] ``` -------------------------------- ### Database error during initial activation Source: https://docs.wpcodebox.com/known_issues A harmless database error may appear in logs during the first activation because snippets execute before custom tables are fully created. ```sql WordPress database error Server shutdown in progress for query SELECT * FROM wp_wpcb_snippets WHERE id = 1 made by require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), include_once('/plugins/wpcodebox2/wpcodebox2.php'), Wpcb2\Service\WPCodeBox2->executeSnippets, Wpcb2\Runner\QueryRunner->runQueries, eval, Wpcb2\ConditionBuilder\ShouldExecute::should_execute, Wpcb2\ConditionBuilder\ShouldExecute->shouldExecute, Wpcb2\Repository\SnippetRepository->getSnippet" ``` -------------------------------- ### PHP add_action Hook Timing Source: https://docs.wpcodebox.com/snippet_doesnt_run If using `add_action` in a PHP snippet, ensure the action hasn't already fired. Select an earlier hook like 'Root' or 'plugins_loaded' in the WPCodeBox UI to avoid timing issues. ```php 'default_value1', 'parameter2' => 'default_value2', ), $atts ); // Access the parameters like this: $value1 = $params['parameter1']; $value2 = $params['parameter2']; // Your shortcode code goes here... echo $output; ``` -------------------------------- ### Preventing foreach Invalid Argument Warnings Source: https://docs.wpcodebox.com/common_errors Use is_array() to validate input before iterating. The first snippet demonstrates the problematic code, while the second shows the corrected implementation. ```php function print_post_titles($post_ids) { // Ensure we have an array of post IDs if (!is_array($post_ids)) { // Trigger a warning if the argument is not an array foreach ($post_ids as $post_id) { $post_title = get_the_title($post_id); echo $post_title . '
'; } } } ``` ```php function print_post_titles($post_ids) { // Ensure we have an array of post IDs if (is_array($post_ids)) { foreach ($post_ids as $post_id) { $post_title = get_the_title($post_id); echo $post_title . '
'; } } } ``` -------------------------------- ### Fix XML parsing error in PHP snippets Source: https://docs.wpcodebox.com/known_issues Add a comment containing the PHP opening tag at the end of the snippet to prevent XML syntax errors. ```php '; ``` ```php ; // `) to access properties of a post object returned by `get_post()`. Array notation (`[]`) can cause a 'Trying to access array offset on value of type null' notice. ```php function get_post_title() { // Get the post object $post = get_post(); // Access the post title $title = $post['post_title']; // This line may cause a notice return $title; } ``` ```php function get_post_title() { // Get the post object $post = get_post(); // Access the post title using object notation $title = $post->post_title; return $title; } ``` -------------------------------- ### Create ACF Field Shortcode Source: https://docs.wpcodebox.com/display_acf_field_in_a_shortcode This PHP snippet registers a shortcode to display an ACF field. Ensure you replace 'your_acf_field_name' with the actual name of your ACF field. This code should be added to your theme's functions.php file or as a PHP snippet in WordPress. ```php ``` -------------------------------- ### Fixing WP_Post Object Access Errors Source: https://docs.wpcodebox.com/common_errors Access WP_Post properties using object notation ($post->property) instead of array notation. Always verify the object type with is_a() before access. ```php function get_post_title_by_id($post_id) { $post = get_post($post_id); $title = $post['post_title']; // This line will trigger an error return $title; } // Call the function with a non-existent post ID (causing an error) $post_title = get_post_title_by_id(9999); echo $post_title; ``` ```php function get_post_title_by_id($post_id) { $post = get_post($post_id); if (is_a($post, 'WP_Post')) { $title = $post->post_title; // Use object notation to access post_title return $title; } else { return 'Post not found'; } } // Call the function with a non-existent post ID (handled gracefully) $post_title = get_post_title_by_id(9999); echo $post_title; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.