Custom code for adding a custom footer message in WordPress

David K Dec 17, 2024 Footer Customization
How can I add a message at the bottom of my website that I can change anytime?
What is the proper method to implement a custom footer message in WordPress using hooks and functions.php that allows for easy updates via the admin panel?
Andy answered Dec 17, 2024

Custom Footer Message Solution

Method 1: Custom Code Solution

First, let's create a custom option in WordPress admin to store and update the footer message.

Add this code to your theme's functions.php:

This code creates the admin menu item and settings field:

function custom_footer_admin_menu() {
    add_menu_page(
        'Footer Settings',
        'Footer Message',
        'manage_options',
        'custom-footer-settings',
        'custom_footer_settings_page',
        'dashicons-admin-generic',
        99
    );
}
add_action('admin_menu', 'custom_footer_admin_menu');

function custom_footer_settings_page() {
    if (isset($_POST['footer_message'])) {
        update_option('custom_footer_message', wp_kses_post($_POST['footer_message']));
        echo '<div class="updated"><p>Footer message updated!</p></div>';
    }
    $message = get_option('custom_footer_message', '');
    ?>
    <div class="wrap">
        <h2>Custom Footer Message</h2>
        <form method="post">
            <textarea name="footer_message" rows="5" style="width: 100%;"><?php echo esc_textarea($message); ?></textarea>
            <p><input type="submit" class="button button-primary" value="Save Message"></p>
        </form>
    </div>
    <?php
}

This code adds the footer message to your site:

function display_custom_footer_message() {
    $message = get_option('custom_footer_message');
    if (!empty($message)) {
        echo '<div class="custom-footer-message">' . wp_kses_post($message) . '</div>';
    }
}
add_action('wp_footer', 'display_custom_footer_message');

Add this CSS to your theme's style.css:

.custom-footer-message {
    text-align: center;
    padding: 20px;
    background: #f9f9f9;
    border-top: 1px solid #ddd;
}

Method 2: Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Footer Text - Simple plugin for custom footer text
  2. Insert Headers and Footers - More features, including custom HTML/scripts

Usage Instructions:

  1. Add the PHP code to your theme's functions.php
  2. Add the CSS to your theme's style.css
  3. Go to WordPress admin panel
  4. Look for "Footer Message" in the left menu
  5. Enter your desired footer message
  6. Click "Save Message"

The message will appear at the bottom of every page on your site. You can update it anytime through the admin panel.

Note: This solution includes basic security measures (wp_kses_post) and follows WordPress coding standards. Adjust the CSS styling to match your theme's design.