Current Location: Home> Latest Articles> Embed date_sunset() into WordPress plugin to display local sunset information

Embed date_sunset() into WordPress plugin to display local sunset information

M66 2025-05-31

When developing WordPress plug-ins, we often need to provide dynamic content based on the user's geographical location, such as sunrise and sunset times. PHP's own date_sunset() function can easily calculate the sunset time. However, to display the user's "local" sunset time in real time, some positioning technology is needed.
This article will take you to implement it step by step, embed date_sunset() in the WordPress plug-in and dynamically display the sunset time.

Step 1: Obtain the user's geographical location (latitude and longitude)

PHP runs on the server side and cannot directly access the geographical location of the user device. Therefore, we need to use JavaScript to obtain the user's latitude and longitude, and pass the information back to the server through AJAX.

 // In your plugin main file or specific PHP Add to the file
function m66net_enqueue_location_script() {
    wp_enqueue_script('m66net-location-script', 'https://m66.net/js/location.js', array('jquery'), null, true);
    
    // pass wp_localize_script Bundle AJAX URL Pass to JS
    wp_localize_script('m66net-location-script', 'm66net_ajax_object', array(
        'ajax_url' => admin_url('admin-ajax.php')
    ));
}
add_action('wp_enqueue_scripts', 'm66net_enqueue_location_script');

Example of location.js file (stored in https://m66.net/js/location.js ):

 jQuery(document).ready(function($){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            $.post(m66net_ajax_object.ajax_url, {
                action: 'm66net_send_location',
                latitude: position.coords.latitude,
                longitude: position.coords.longitude
            }, function(response) {
                $('#m66net-sunset-time').html(response);
            });
        });
    } else {
        $('#m66net-sunset-time').html('Unable to obtain your location information');
    }
});

Step 2: Process AJAX request and calculate the sunset time

Next, we create an AJAX interface in WordPress that processes user geolocation information and returns sunset time.

 function m66net_calculate_sunset() {
    if (isset($_POST['latitude']) && isset($_POST['longitude'])) {
        $latitude = floatval($_POST['latitude']);
        $longitude = floatval($_POST['longitude']);
        
        $timestamp = time();
        
        $sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, 90.583333, date('Z') / 3600);
        
        echo 'Your local sunset time is:' . $sunset;
    } else {
        echo 'Unable to calculate the sunset time';
    }
    wp_die(); // important:termination AJAX deal with
}

add_action('wp_ajax_m66net_send_location', 'm66net_calculate_sunset');
add_action('wp_ajax_nopriv_m66net_send_location', 'm66net_calculate_sunset');

Let's explain:

  • SUNFUNCS_RET_STRING let date_sunset() return the formatted time string, such as 19:21 .

  • 90.583333 is the standard sea level air pressure angle, which is usually used by default.

  • date('Z') / 3600 will be corrected according to the server's time zone.

Step 3: Add a location to the page that shows the sunset time

Finally, add a piece of HTML where you need to display the sunset time (such as an article, page, or gadget):

 <div id="m66net-sunset-time">Getting sunset time...</div>

As long as the user visits the page, JavaScript will be triggered, the user's location will be automatically detected, and the exact sunset time will be displayed!

Tips

  • If you want to support HTTPS, make sure your m66.net resources also go to HTTPS, otherwise the browser will block mixed content requests.

  • The result of date_sunset() is the server time standard. If the plug-in wants to adapt to different server regions, it also requires more refined time zone processing.

  • Please add reasonable error prompts to your plug-in, such as when the user refuses to share location information.