SayPro Ensure custom fields are properly saved and displayed on the frontend.

SayPro is a Global Solutions Provider working with Individuals, Governments, Corporate Businesses, Municipalities, International Institutions. SayPro works across various Industries, Sectors providing wide range of solutions.

Email: info@saypro.online Call/WhatsApp: Use Chat Button 👇

To ensure that the custom fields (e.g., SEO, content classification, and tracking fields) are properly saved in the backend and displayed correctly on the frontend, you need to follow these steps for proper integration, validation, and rendering.

1. Ensure Custom Fields Are Saved Correctly in the Backend

First, you need to make sure that the custom fields are properly saved in the database when users (content creators, editors, etc.) enter data into the meta boxes. This process includes adding and saving the fields in the CMS and ensuring the data persists after content is saved or updated.

1.1. Add Custom Fields to Meta Boxes

Ensure that the meta boxes for SEO, classification, and tracking are created correctly in the backend. In WordPress, for example, this is done using the add_meta_box() function.

Here’s how to add the meta boxes for SEO fields, content classification, and tracking data:

// Add the meta boxes for SEO, classification, and tracking
function saypro_register_meta_boxes() {
    add_meta_box(
        'seo_meta_box',
        'SEO Settings',
        'seo_meta_box_callback',
        'post', // Add other post types as needed (e.g., 'page', 'custom_post_type')
        'normal',
        'high'
    );

    add_meta_box(
        'classification_meta_box',
        'Content Classification',
        'classification_meta_box_callback',
        'post',
        'normal',
        'high'
    );

    add_meta_box(
        'tracking_meta_box',
        'Tracking Information',
        'tracking_meta_box_callback',
        'post',
        'normal',
        'high'
    );
}

add_action('add_meta_boxes', 'saypro_register_meta_boxes');

1.2. Save the Custom Fields Data

Make sure the custom fields are saved properly when the post is updated or published. This is achieved using the save_post action hook in WordPress.

// Save the custom fields when the post is saved
function saypro_save_meta_box_data($post_id) {
    // Check for autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // Check nonce for security (to prevent unauthorized saving)
    if (!isset($_POST['seo_meta_box_nonce']) || !wp_verify_nonce($_POST['seo_meta_box_nonce'], 'save_seo_meta_box')) return;

    // Save SEO fields
    if (isset($_POST['seo_title'])) {
        $seo_title = sanitize_text_field($_POST['seo_title']);
        update_post_meta($post_id, '_seo_title', $seo_title);
    }
    if (isset($_POST['seo_description'])) {
        $seo_description = sanitize_textarea_field($_POST['seo_description']);
        update_post_meta($post_id, '_seo_description', $seo_description);
    }

    // Save classification fields (e.g., category or custom taxonomy)
    if (isset($_POST['content_category'])) {
        $content_category = sanitize_text_field($_POST['content_category']);
        update_post_meta($post_id, '_content_category', $content_category);
    }

    // Save tracking fields
    if (isset($_POST['tracking_id'])) {
        $tracking_id = sanitize_text_field($_POST['tracking_id']);
        update_post_meta($post_id, '_tracking_id', $tracking_id);
    }
}

add_action('save_post', 'saypro_save_meta_box_data');

Make sure each field has a corresponding nonce field for security:

// Add nonce field to your meta box callback
function seo_meta_box_callback($post) {
    wp_nonce_field('save_seo_meta_box', 'seo_meta_box_nonce');
    
    $seo_title = get_post_meta($post->ID, '_seo_title', true);
    $seo_description = get_post_meta($post->ID, '_seo_description', true);
    
    // Display the form fields for SEO
    echo '<label for="seo_title">SEO Title:</label>';
    echo '<input type="text" id="seo_title" name="seo_title" value="' . esc_attr($seo_title) . '" />';

    echo '<label for="seo_description">SEO Description:</label>';
    echo '<textarea id="seo_description" name="seo_description">' . esc_textarea($seo_description) . '</textarea>';
}

2. Display Custom Fields on the Frontend

Once the custom fields are saved correctly in the backend, the next step is to display them on the frontend where the content is rendered. This can be done by modifying the relevant templates (e.g., single.php, page.php, or custom templates for custom post types).

2.1. Displaying SEO Fields on the Frontend

You can use get_post_meta() to retrieve the custom field values and display them on the frontend.

Here’s an example of how to display the SEO title and description:

// Display SEO fields on the frontend
function display_seo_data($post_id) {
    // Get the saved SEO title and description
    $seo_title = get_post_meta($post_id, '_seo_title', true);
    $seo_description = get_post_meta($post_id, '_seo_description', true);

    if ($seo_title) {
        echo '<meta name="title" content="' . esc_attr($seo_title) . '" />';
    }
    if ($seo_description) {
        echo '<meta name="description" content="' . esc_attr($seo_description) . '" />';
    }
}

// Call this function inside the `<head>` tag of your template to display SEO metadata
display_seo_data(get_the_ID());

2.2. Displaying Content Classification (e.g., Category)

If the content classification is a simple category or taxonomy field, you can display it like this:

// Display content category on the frontend
function display_content_category($post_id) {
    $category = get_post_meta($post_id, '_content_category', true);
    if ($category) {
        echo '<p>Category: ' . esc_html($category) . '</p>';
    }
}

// Call this function where you want the category displayed (e.g., in a post template)
display_content_category(get_the_ID());

2.3. Displaying Tracking Information

For tracking fields like Google Analytics tracking IDs, you can display them in the relevant place (usually the footer or header).

// Display tracking ID on the frontend (e.g., in footer)
function display_tracking_id($post_id) {
    $tracking_id = get_post_meta($post_id, '_tracking_id', true);
    if ($tracking_id) {
        echo '<script>console.log("Tracking ID: ' . esc_js($tracking_id) . '");</script>';
    }
}

// Call this function where you need the tracking ID, such as in the footer or header
display_tracking_id(get_the_ID());

3. Handle Edge Cases (Optional)

To ensure the fields are properly handled, consider the following:

  • Empty Fields: If the custom field is empty, ensure that nothing is outputted or show a default value. $seo_title = get_post_meta($post_id, '_seo_title', true); if (empty($seo_title)) { $seo_title = 'Default SEO Title'; // Provide a default value if empty }
  • Formatting/Display: Ensure that the custom field values are properly escaped when displayed to avoid security vulnerabilities (e.g., XSS). echo esc_html($seo_title); // For text echo esc_textarea($seo_description); // For textarea

4. Testing the Frontend Display

Once the custom fields are properly saved and the necessary code is added to display them on the frontend, you should test the following:

  • Check Data Visibility: Ensure that the SEO title, description, and other custom fields are showing up on the frontend for the right posts/pages.
  • Validate Output: Verify that the data is correctly rendered in the HTML source (for meta tags, headers, etc.).
  • Test Different Content Types: Test the display across different post types (e.g., posts, pages, custom post types).
  • Mobile/Responsive Testing: Ensure that the custom fields are displayed properly on various devices and screen sizes, particularly for SEO/meta tags and tracking scripts.

Conclusion

By following these steps, you can ensure that custom fields are properly saved in the backend and displayed correctly on the frontend. Proper validation, security measures, and responsive design will ensure that the system works smoothly and the custom fields are utilized effectively for SEO, content classification, and data tracking.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!