GreenShift Tips and Tutorials
This JS snippet complements the work done with Interaction Layers to show/hide various helper icons (cookie notices, chat widgets, back to top buttons, etc.) so that they do not cover the footer area.
<?php

define('TARGET_PAGE_SLUGS', ['visitors']);
define('TARGET_PAGE_IDS', []);

/**
 * Output user profile data container in page body
 * 
 * Generates a hidden div with user profile data
 * (name, email, phone, pending email, nonce)
 * for JavaScript consumption.
 * Only runs on specified target pages for logged-in users.
 * 
 * @since 1.0.0
 */
add_action('wp_body_open', function() {
    // Only output for logged-in users
    if (!is_user_logged_in()) {
        return;
    }
    
    // Check if current page matches target slugs or IDs
    $current_page_slug = get_post_field('post_name', get_queried_object_id());
    $current_page_id = get_queried_object_id();
    
    $is_target_page = in_array($current_page_slug, TARGET_PAGE_SLUGS) || 
                      in_array($current_page_id, TARGET_PAGE_IDS);
    
    if (!$is_target_page) {
        return;
    }
    
    // Get current user data
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $display_name = $current_user->display_name;
    $email = $current_user->user_email;
    $phone = get_user_meta($user_id, 'phone_no', true); // Custom meta field
    
    // Check for pending email change
    $pending_email_data = get_user_meta($user_id, '_new_email', true);
    $pending_email = '';
    
    if ($pending_email_data && isset($pending_email_data['newemail'])) {
        $pending_email = $pending_email_data['newemail'];
    }
    
    // Generate nonce
    $nonce = wp_create_nonce('edit_profile_action');
    
    // Output data container
    echo '<div id="user-profile-data" style="display:none;" 
              data-user-id="' . esc_attr($user_id) . '"
              data-nonce="' . esc_attr($nonce) . '"
              data-display-name="' . esc_attr($display_name) . '" 
              data-email="' . esc_attr($email) . '" 
              data-phone="' . esc_attr($phone) . '"
              data-pending-email="' . esc_attr($pending_email) . '"></div>';
});

/**
 * Handle email confirmation from link
 * 
 * Processes the email change confirmation when user
 * clicks the link sent to their new email address.
 * Verifies hash, updates user email,
 * removes pending change meta,
 * and redirects back to profile page.
 * 
 * @since 1.0.0
 */
add_action('init', function() {
    // Handle email confirmation
    if (isset($_GET['newuseremail']) && is_user_logged_in()) {
        $hash = sanitize_text_field($_GET['newuseremail']);
        $current_user_id = get_current_user_id();
        
        // Get pending email data
        $pending_email_data = get_user_meta($current_user_id, '_new_email', true);
        
        if (!$pending_email_data || !isset($pending_email_data['hash'])) {
            wp_die('Invalid or expired confirmation link');
        }
        
        // Verify hash matches
        if ($pending_email_data['hash'] !== $hash) {
            wp_die('Invalid confirmation link');
        }
        
        // Update email
        wp_update_user(array(
            'ID' => $current_user_id,
            'user_email' => $pending_email_data['newemail']
        ));
        
        // Delete pending email data
        delete_user_meta($current_user_id, '_new_email');
        
        // Redirect back to profile page with success indicator
        wp_redirect(home_url('/visitors/?email_updated=1'));
        exit;
    }
});

/**
 * Handle email change cancellation via AJAX
 * 
 * Removes pending email change when user clicks cancel link.
 * Verifies nonce, deletes _new_email user meta, and
 * returns JSON success response.
 * 
 * @since 1.0.0
 */
add_action('admin_post_cancel_email_change', 'handle_cancel_email_change');
add_action('admin_post_nopriv_cancel_email_change', 'handle_cancel_email_change');

function handle_cancel_email_change() {
    if (!is_user_logged_in()) {
        wp_send_json_error('Unauthorized');
    }
    
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'edit_profile_action')) {
        wp_send_json_error('Invalid nonce');
    }
    
    $current_user_id = get_current_user_id();
    delete_user_meta($current_user_id, '_new_email');
    
    wp_send_json_success();
}
PHP
This code snippet is used in the following tutorial(s):

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only