To implement custom fields for SEO, content classification, and data tracking, we will set up three different types of custom fields in the SayPro platform using the meta box system. Each set of fields will correspond to specific content areas: SEO (for metadata), content classification (for categories and tags), and data tracking (for analytics or other content-specific tracking).
1. SEO Custom Fields Implementation
SEO fields will allow content creators to input meta information, such as SEO titles, descriptions, and keywords for better search engine visibility.
1.1 Create SEO Meta Box
We will create a meta box that contains the following custom fields:
- SEO Title: Custom title for SEO purposes.
- Meta Description: A concise summary of the content for search engine results.
- SEO Keywords: Keywords related to the content for SEO optimization.
1.2 Code Implementation:
// Register the SEO meta box
function saypro_register_seo_meta_box() {
add_meta_box(
'seo_meta_box', // ID
'SEO Settings', // Title
'saypro_seo_meta_box_callback', // Callback function
'post', // Post type
'normal', // Context
'high' // Priority
);
}
add_action('add_meta_boxes', 'saypro_register_seo_meta_box');
// Meta box callback function
function saypro_seo_meta_box_callback($post) {
// Add nonce for security
wp_nonce_field('saypro_save_seo_meta_box_data', 'seo_meta_box_nonce');
// Get the current SEO values
$seo_title = get_post_meta($post->ID, '_seo_title', true);
$meta_description = get_post_meta($post->ID, '_meta_description', true);
$seo_keywords = get_post_meta($post->ID, '_seo_keywords', true);
// Display fields in the meta box
echo '<label for="seo_title">SEO Title:</label>';
echo '<input type="text" id="seo_title" name="seo_title" value="' . esc_attr($seo_title) . '" class="widefat" />';
echo '<label for="meta_description">Meta Description:</label>';
echo '<textarea id="meta_description" name="meta_description" rows="4" class="widefat">' . esc_textarea($meta_description) . '</textarea>';
echo '<label for="seo_keywords">SEO Keywords:</label>';
echo '<input type="text" id="seo_keywords" name="seo_keywords" value="' . esc_attr($seo_keywords) . '" class="widefat" />';
}
// Save SEO meta box data
function saypro_save_seo_meta_box_data($post_id) {
// Check nonce for security
if (!isset($_POST['seo_meta_box_nonce']) || !wp_verify_nonce($_POST['seo_meta_box_nonce'], 'saypro_save_seo_meta_box_data')) {
return;
}
// Check for autosave and permissions
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('post' !== $_POST['post_type']) return;
// Save SEO title, description, and keywords
if (isset($_POST['seo_title'])) {
update_post_meta($post_id, '_seo_title', sanitize_text_field($_POST['seo_title']));
}
if (isset($_POST['meta_description'])) {
update_post_meta($post_id, '_meta_description', sanitize_textarea_field($_POST['meta_description']));
}
if (isset($_POST['seo_keywords'])) {
update_post_meta($post_id, '_seo_keywords', sanitize_text_field($_POST['seo_keywords']));
}
}
add_action('save_post', 'saypro_save_seo_meta_box_data');
2. Content Classification Custom Fields Implementation
Content classification helps in categorizing and tagging the content to ensure that it is easily discoverable.
2.1 Create Classification Meta Box
We will add a meta box for content classification with the following fields:
- Category: A dropdown for selecting a category (we will use WordPress’s default categories).
- Tags: An input field for adding tags to the post.
2.2 Code Implementation:
// Register the Content Classification meta box
function saypro_register_classification_meta_box() {
add_meta_box(
'classification_meta_box', // ID
'Content Classification', // Title
'saypro_classification_meta_box_callback', // Callback function
'post', // Post type
'side', // Context (sidebar)
'low' // Priority
);
}
add_action('add_meta_boxes', 'saypro_register_classification_meta_box');
// Meta box callback function for content classification
function saypro_classification_meta_box_callback($post) {
// Add nonce for security
wp_nonce_field('saypro_save_classification_meta_box_data', 'classification_meta_box_nonce');
// Get the current categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
// Display category dropdown
$categories_list = get_categories();
echo '<label for="post_category">Category:</label>';
echo '<select name="post_category" id="post_category" class="widefat">';
echo '<option value="">Select a category</option>';
foreach ($categories_list as $category) {
$selected = in_array($category->term_id, wp_list_pluck($categories, 'term_id')) ? 'selected' : '';
echo '<option value="' . esc_attr($category->term_id) . '" ' . $selected . '>' . esc_html($category->name) . '</option>';
}
echo '</select>';
// Display tag input
echo '<label for="post_tags">Tags:</label>';
$tag_names = $tags ? wp_list_pluck($tags, 'name') : '';
echo '<input type="text" name="post_tags" id="post_tags" value="' . esc_attr(implode(', ', $tag_names)) . '" class="widefat" />';
}
// Save Classification meta box data
function saypro_save_classification_meta_box_data($post_id) {
// Check nonce for security
if (!isset($_POST['classification_meta_box_nonce']) || !wp_verify_nonce($_POST['classification_meta_box_nonce'], 'saypro_save_classification_meta_box_data')) {
return;
}
// Check for autosave and permissions
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('post' !== $_POST['post_type']) return;
// Save the selected category
if (isset($_POST['post_category'])) {
wp_set_post_categories($post_id, array(intval($_POST['post_category'])));
}
// Save the tags
if (isset($_POST['post_tags'])) {
wp_set_post_tags($post_id, sanitize_text_field($_POST['post_tags']));
}
}
add_action('save_post', 'saypro_save_classification_meta_box_data');
3. Data Tracking Custom Fields Implementation
Data tracking fields are useful for collecting analytics or tracking specific content interactions.
3.1 Create Data Tracking Meta Box
We will add a meta box for tracking data, such as:
- Page Views: Input for tracking the number of views.
- Tracking ID: A field for an external tracking ID (e.g., Google Analytics, custom tracking).
3.2 Code Implementation:
// Register the Data Tracking meta box
function saypro_register_tracking_meta_box() {
add_meta_box(
'tracking_meta_box', // ID
'Data Tracking', // Title
'saypro_tracking_meta_box_callback', // Callback function
'post', // Post type
'side', // Context (sidebar)
'low' // Priority
);
}
add_action('add_meta_boxes', 'saypro_register_tracking_meta_box');
// Meta box callback function for data tracking
function saypro_tracking_meta_box_callback($post) {
// Add nonce for security
wp_nonce_field('saypro_save_tracking_meta_box_data', 'tracking_meta_box_nonce');
// Get the current tracking data
$page_views = get_post_meta($post->ID, '_page_views', true);
$tracking_id = get_post_meta($post->ID, '_tracking_id', true);
// Display page views input
echo '<label for="page_views">Page Views:</label>';
echo '<input type="number" name="page_views" id="page_views" value="' . esc_attr($page_views) . '" class="widefat" />';
// Display tracking ID input
echo '<label for="tracking_id">Tracking ID:</label>';
echo '<input type="text" name="tracking_id" id="tracking_id" value="' . esc_attr($tracking_id) . '" class="widefat" />';
}
// Save Data Tracking meta box data
function saypro_save_tracking_meta_box_data($post_id) {
// Check nonce for security
if (!isset($_POST['tracking_meta_box_nonce']) || !wp_verify_nonce($_POST['tracking_meta_box_nonce'], 'saypro_save_tracking_meta_box_data')) {
return;
}
// Check for autosave and permissions
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('post' !== $_POST['post_type']) return;
// Save page views
if (isset($_POST['page_views'])) {
update_post_meta($post_id, '_page_views', intval($_POST['page_views']));
}
// Save tracking ID
if (isset($_POST['tracking_id'])) {
update_post_meta($post_id, '_tracking_id', sanitize_text_field($_POST['tracking_id']));
}
}
add_action('save_post', 'saypro_save_tracking_meta_box_data');
Summary of Custom Fields
- SEO Meta Box: Contains fields for SEO Title, Meta Description, and Keywords.
- Content Classification Meta Box: Allows selection of categories and input of tags for better content organization.
- Data Tracking Meta Box: Tracks metrics like page views and includes an external tracking ID.
Testing and Finalization:
- Test: After implementing these fields, test them by adding content and verifying that the data is saved correctly and displayed on the frontend as required.
- Adjustments: Make necessary adjustments based on feedback from the SayPro team or testing results.
Leave a Reply