Add Meta Description Tags to WordPress Pages Easily
How to add meta description in wordpress;
Add meta description to wordpress page;
Wordpress meta description tutorial;
Steps to add meta tags in wordpress;
Wordpress meta description plugin;
Manually add meta description wordpress;
Best way to add meta tags in wordpress;
Wordpress meta description guide;
How to set meta description in wordpress;
Add meta description without plugin wordpress;
Explanation
To add a meta description to your WordPress pages, this code does a few things:
- Adds a Meta Box: When you're editing a post or page, you'll see a new box on the right side where you can enter a meta description. This is done by adding a custom meta box to the post editing screen.
- Displays Meta Description: If you've entered a meta description, it will be included in the page's HTML code. This is important for search engines to understand what your page is about.
- Saves Your Input: When you save or update your post, the meta description you entered is saved. This ensures that your description is always available and can be displayed on your page.
Here's how it works:
- The code hooks into the WordPress head section to add the meta description if it exists.
- A meta box is added to the post and page editing screens, allowing you to enter a description.
- When you save a post, the description is securely saved and sanitized to prevent any issues.
This approach allows you to manually add and manage meta descriptions without needing a plugin, giving you more control over your site's SEO.
Code
<?php
// Add meta description to WordPress pages
// Hook into 'wp_head' to add meta description
add_action('wp_head', 'wp_dudecom_add_meta_description');
/**
* Function to add a meta description to WordPress pages
*/
function wp_dudecom_add_meta_description() {
if (is_singular()) {
global $post;
// Sanitize and retrieve the meta description
$meta_description = get_post_meta($post->ID, '_wp_dudecom_meta_description', true);
$meta_description = esc_attr($meta_description);
// Output the meta description if it exists
if (!empty($meta_description)) {
echo '<meta name="description" content="' . $meta_description . '" />';
}
}
}
// Add a meta box to the post editing screen
add_action('add_meta_boxes', 'wp_dudecom_add_meta_box');
/**
* Function to add a custom meta box for meta description
*/
function wp_dudecom_add_meta_box() {
add_meta_box(
'wp_dudecom_meta_description_box',
'Meta Description',
'wp_dudecom_meta_box_callback',
['post', 'page'],
'side',
'default'
);
}
/**
* Callback function to display the meta box
*
* @param WP_Post $post The post object
*/
function wp_dudecom_meta_box_callback($post) {
// Add a nonce field for security
wp_nonce_field('wp_dudecom_save_meta_description', 'wp_dudecom_meta_description_nonce');
// Retrieve existing meta description
$meta_description = get_post_meta($post->ID, '_wp_dudecom_meta_description', true);
// Display the form field
echo '<label for="wp_dudecom_meta_description">Enter Meta Description:</label>';
echo '<textarea id="wp_dudecom_meta_description" name="wp_dudecom_meta_description" rows="4" style="width:100%;">' . esc_textarea($meta_description) . '</textarea>';
}
/**
* Save the meta description when the post is saved
*
* @param int $post_id The ID of the post being saved
*/
function wp_dudecom_save_meta_description($post_id) {
// Check if nonce is set
if (!isset($_POST['wp_dudecom_meta_description_nonce'])) {
return;
}
// Verify the nonce
if (!wp_verify_nonce($_POST['wp_dudecom_meta_description_nonce'], 'wp_dudecom_save_meta_description')) {
return;
}
// Check if the user has permission to edit the post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Sanitize and save the meta description
if (isset($_POST['wp_dudecom_meta_description'])) {
$meta_description = sanitize_text_field($_POST['wp_dudecom_meta_description']);
update_post_meta($post_id, '_wp_dudecom_meta_description', $meta_description);
}
}
// Hook into 'save_post' to save the meta description
add_action('save_post', 'wp_dudecom_save_meta_description');
?>
Instructions
File Location: Add the provided code to your theme's functions.php
file or a custom plugin file.
Prerequisites: Ensure you have access to your WordPress theme files or the ability to create a custom plugin.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation.
- Locate the
functions.php
File: Navigate towp-content/themes/your-theme-name/
and open thefunctions.php
file for editing. Alternatively, create a new plugin file inwp-content/plugins/
if you prefer to keep this functionality separate from your theme. - Add the Code: Copy and paste the provided code into the
functions.php
file or your custom plugin file. Ensure you do not place the code within any existing function or class unless intended. - Save Changes: Save the changes to the
functions.php
file or plugin file. - Test the Implementation: Log in to your WordPress admin dashboard and edit a post or page. You should see a new "Meta Description" box on the right side of the editor screen.
- Enter a Meta Description: In the "Meta Description" box, enter a description for your post or page and save the changes.
- Verify the Meta Description: View the source code of the saved post or page on the front end to ensure the meta description is included in the HTML head section.
If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support and services.