A WordPress plugin: paste your key under Settings, add [astronode_panchang] to any post, page or widget area, and the site shows today's panchang for the place you choose - cached until that place's midnight, so a busy site makes two API calls a day per location.
unzip wordpress-panchang-plugin.zip
mv wordpress-panchang-plugin wp-content/plugins/astronode-panchang
# Activate it, then Settings > AstroNode: paste the key and press Test connection
# In a post or widget: [astronode_panchang lat="28.61" lng="77.21" tz="Asia/Kolkata" label="Delhi"]
<?php
/**
* Plugin Name: AstroNode Panchang
* Plugin URI: https://www.astronode.dev/templates/wordpress-panchang-plugin
* Description: Today's panchang (tithi, nakshatra, yoga, karana, sunrise, sunset) and Rahu Kaal, Yamaganda and Gulika for any place, from the AstroNode API. Use the [astronode_panchang] shortcode or astronode_panchang_html() in a theme.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 8.1
* Author: AstroNode
* Author URI: https://www.astronode.dev
* Text Domain: astronode-panchang
* Domain Path: /languages
*
* @package AstroNode_Panchang
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'ASTRONODE_PANCHANG_VERSION', '1.0.0' );
define( 'ASTRONODE_PANCHANG_FILE', __FILE__ );
define( 'ASTRONODE_PANCHANG_DIR', plugin_dir_path( __FILE__ ) );
define( 'ASTRONODE_PANCHANG_URL', plugin_dir_url( __FILE__ ) );
/** Option that holds the API key. Never autoloaded. */
define( 'ASTRONODE_PANCHANG_OPTION_KEY', 'astronode_panchang_api_key' );
/** Option listing the transients this plugin created, so they can be flushed and uninstalled. */
define( 'ASTRONODE_PANCHANG_OPTION_CACHE_INDEX', 'astronode_panchang_cache_index' );
require_once ASTRONODE_PANCHANG_DIR . 'includes/class-astronode-panchang-client.php';
require_once ASTRONODE_PANCHANG_DIR . 'includes/class-astronode-panchang-renderer.php';
require_once ASTRONODE_PANCHANG_DIR . 'includes/class-astronode-panchang-settings.php';
/**
* Create the options up front with autoload off, so a later update_option()
* from the Settings API keeps them out of the autoloaded options.
*/
function astronode_panchang_activate(): void {
if ( false === get_option( ASTRONODE_PANCHANG_OPTION_KEY ) ) {
add_option( ASTRONODE_PANCHANG_OPTION_KEY, '', '', false );
}
if ( false === get_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX ) ) {
add_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, array(), '', false );
}
}
register_activation_hook( __FILE__, 'astronode_panchang_activate' );
/**
* Load translations shipped in /languages (WordPress.org language packs load on their own).
*/
function astronode_panchang_load_textdomain(): void {
load_plugin_textdomain( 'astronode-panchang', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'astronode_panchang_load_textdomain' );
/**
* Register (not enqueue) the stylesheet; the renderer enqueues it only on pages that show a panchang.
*/
function astronode_panchang_register_assets(): void {
wp_register_style(
'astronode-panchang',
ASTRONODE_PANCHANG_URL . 'assets/astronode-panchang.css',
array(),
ASTRONODE_PANCHANG_VERSION
);
}
add_action( 'wp_enqueue_scripts', 'astronode_panchang_register_assets' );
add_action( 'admin_enqueue_scripts', 'astronode_panchang_register_assets' );
/**
* Shortcode: [astronode_panchang lat="19.07" lng="72.88" tz="Asia/Kolkata" label="Mumbai"]
*
* @param array|string $atts Shortcode attributes.
* @return string Escaped HTML.
*/
function astronode_panchang_shortcode( $atts ): string {
$atts = shortcode_atts( AstroNode_Panchang_Renderer::defaults(), is_array( $atts ) ? $atts : array(), 'astronode_panchang' );
return AstroNode_Panchang_Renderer::render( $atts );
}
add_shortcode( 'astronode_panchang', 'astronode_panchang_shortcode' );
/**
* Template function, for themes and widget areas without blocks:
*
* echo astronode_panchang_html( array( 'lat' => 28.61, 'lng' => 77.21, 'tz' => 'Asia/Kolkata', 'label' => 'Delhi' ) );
*
* The returned HTML is already escaped.
*
* @param array $args lat, lng, tz, label - same as the shortcode.
* @return string Escaped HTML.
*/
function astronode_panchang_html( array $args = array() ): string {
return AstroNode_Panchang_Renderer::render( wp_parse_args( $args, AstroNode_Panchang_Renderer::defaults() ) );
}
/**
* Echoing variant of astronode_panchang_html().
*
* @param array $args lat, lng, tz, label.
*/
function astronode_the_panchang( array $args = array() ): void {
echo wp_kses_post( astronode_panchang_html( $args ) );
}
if ( is_admin() ) {
AstroNode_Panchang_Settings::init();
}
includes/class-astronode-panchang-client.php
php
<?php
/**
* AstroNode API client: builds the two requests, calls them with
* wp_remote_post(), and caches the day's result in a transient until local
* midnight at the requested place.
*
* @package AstroNode_Panchang
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Calls POST /v1/panchang and POST /v1/muhurta/inauspicious.
*/
final class AstroNode_Panchang_Client {
public const DEFAULT_API_URL = 'https://api.astronode.dev';
/** Failed calls are remembered this long, so a bad key does not cost an API call per page view. */
private const ERROR_TTL = 300;
/** Cap on the transient index; the oldest names drop off (their transients still expire on their own). */
private const INDEX_LIMIT = 500;
/**
* Base URL: the ASTRONODE_API_URL constant (wp-config.php) if defined, else production.
*/
public static function api_url(): string {
$url = ( defined( 'ASTRONODE_API_URL' ) && is_string( ASTRONODE_API_URL ) && '' !== ASTRONODE_API_URL )
? ASTRONODE_API_URL
: self::DEFAULT_API_URL;
/**
* Filters the AstroNode API base URL.
*
* @param string $url Base URL without a trailing slash.
*/
return untrailingslashit( (string) apply_filters( 'astronode_panchang_api_url', untrailingslashit( $url ) ) );
}
/**
* The saved API key, or ''.
*/
public static function api_key(): string {
$key = get_option( ASTRONODE_PANCHANG_OPTION_KEY, '' );
return is_string( $key ) ? $key : '';
}
/**
* Today's date (Y-m-d) at the place, and the UTC offset in minutes in force at local noon that day.
*
* @param DateTimeZone $tz Time zone of the place.
* @param DateTimeImmutable|null $now Override for "now" (tests).
* @return array{date: string, tz_offset_minutes: int}
*/
public static function local_day( DateTimeZone $tz, ?DateTimeImmutable $now = null ): array {
$now = ( $now ?? new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ) )->setTimezone( $tz );
$date = $now->format( 'Y-m-d' );
$noon = new DateTimeImmutable( $date . ' 12:00:00', $tz );
return array(
'date' => $date,
'tz_offset_minutes' => intdiv( $noon->getOffset(), 60 ),
);
}
/**
* The JSON body both endpoints take.
*
* @param float $lat Latitude.
* @param float $lng Longitude.
* @param DateTimeZone $tz Time zone of the place.
* @return array{date: string, tz_offset_minutes: int, lat: float, lng: float}
*/
public static function request_body( float $lat, float $lng, DateTimeZone $tz ): array {
$day = self::local_day( $tz );
return array(
'date' => $day['date'],
'tz_offset_minutes' => $day['tz_offset_minutes'],
'lat' => $lat,
'lng' => $lng,
);
}
/**
* Seconds from now until the next local midnight at the place (at least 60).
*
* @param DateTimeZone $tz Time zone of the place.
*/
public static function seconds_until_midnight( DateTimeZone $tz ): int {
$now = new DateTimeImmutable( 'now', $tz );
$midnight = $now->modify( 'tomorrow' );
return max( 60, $midnight->getTimestamp() - $now->getTimestamp() );
}
/**
* Panchang plus inauspicious windows for today at a place, cached until local midnight.
*
* @param float $lat Latitude.
* @param float $lng Longitude.
* @param DateTimeZone $tz Time zone of the place.
* @return array{panchang: array, inauspicious: array, date: string}|WP_Error
*/
public static function get_day( float $lat, float $lng, DateTimeZone $tz ) {
$key = self::api_key();
if ( '' === $key ) {
return new WP_Error( 'astronode_no_key', __( 'No AstroNode API key is saved. Add one under Settings > AstroNode.', 'astronode-panchang' ) );
}
$body = self::request_body( $lat, $lng, $tz );
$cache_key = 'astronode_panchang_' . md5( implode( '|', array( self::api_url(), $body['date'], $body['tz_offset_minutes'], $lat, $lng, $tz->getName(), md5( $key ) ) ) );
$error_key = $cache_key . '_err';
$cached = get_transient( $cache_key );
if ( is_array( $cached ) && isset( $cached['panchang'], $cached['inauspicious'] ) ) {
return $cached;
}
$recent_error = get_transient( $error_key );
if ( is_array( $recent_error ) && isset( $recent_error['message'] ) ) {
return new WP_Error( (string) $recent_error['code'], (string) $recent_error['message'], $recent_error['data'] ?? array() );
}
$panchang = self::post( '/v1/panchang', $body, $key );
$result = $panchang;
if ( ! is_wp_error( $panchang ) ) {
$inauspicious = self::post( '/v1/muhurta/inauspicious', $body, $key );
$result = is_wp_error( $inauspicious )
? $inauspicious
: array(
'date' => $body['date'],
'panchang' => $panchang,
'inauspicious' => $inauspicious,
);
}
if ( is_wp_error( $result ) ) {
set_transient(
$error_key,
array(
'code' => $result->get_error_code(),
'message' => $result->get_error_message(),
'data' => $result->get_error_data(),
),
self::ERROR_TTL
);
self::remember( $error_key );
return $result;
}
set_transient( $cache_key, $result, self::seconds_until_midnight( $tz ) );
self::remember( $cache_key );
return $result;
}
/**
* POST a JSON body and return the response's `data` object, or a WP_Error
* carrying the API's error message.
*
* @param string $path Endpoint path, e.g. /v1/panchang.
* @param array $body Request body.
* @param string $key API key.
* @return array|WP_Error
*/
public static function post( string $path, array $body, string $key ) {
$response = wp_remote_post(
self::api_url() . $path,
array(
'timeout' => 10,
'headers' => array(
'Authorization' => 'Bearer ' . $key,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
'body' => wp_json_encode( $body ),
'data_format' => 'body',
'user-agent' => 'astronode-panchang-wordpress/' . ASTRONODE_PANCHANG_VERSION . '; ' . home_url( '/' ),
)
);
if ( is_wp_error( $response ) ) {
return new WP_Error(
'astronode_http',
/* translators: %s: the HTTP transport error message. */
sprintf( __( 'Could not reach the AstroNode API: %s', 'astronode-panchang' ), $response->get_error_message() ),
array( 'path' => $path )
);
}
$status = (int) wp_remote_retrieve_response_code( $response );
$json = json_decode( (string) wp_remote_retrieve_body( $response ), true );
if ( $status < 200 || $status >= 300 || ! is_array( $json ) || ! isset( $json['data'] ) || ! is_array( $json['data'] ) ) {
$code = ( is_array( $json ) && isset( $json['error']['code'] ) ) ? (string) $json['error']['code'] : 'HTTP_' . $status;
$message = ( is_array( $json ) && isset( $json['error']['message'] ) )
? (string) $json['error']['message']
/* translators: %d: HTTP status code. */
: sprintf( __( 'Unexpected response (HTTP %d).', 'astronode-panchang' ), $status );
return new WP_Error(
'astronode_api',
$message,
array(
'status' => $status,
'api_code' => $code,
'path' => $path,
'request_id' => ( is_array( $json ) && isset( $json['request_id'] ) ) ? (string) $json['request_id'] : '',
)
);
}
return $json['data'];
}
/**
* Record a transient name so flush() and uninstall.php can find it, even with an object cache.
*
* @param string $name Transient name.
*/
private static function remember( string $name ): void {
$index = get_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, array() );
$index = is_array( $index ) ? $index : array();
if ( in_array( $name, $index, true ) ) {
return;
}
$index[] = $name;
if ( count( $index ) > self::INDEX_LIMIT ) {
$index = array_slice( $index, -self::INDEX_LIMIT );
}
if ( false === get_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX ) ) {
add_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, $index, '', false );
} else {
update_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, $index, false );
}
}
/**
* Delete every cached day and remembered error. Returns how many transients were listed.
*/
public static function flush(): int {
$index = get_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, array() );
$index = is_array( $index ) ? $index : array();
foreach ( $index as $name ) {
delete_transient( (string) $name );
}
update_option( ASTRONODE_PANCHANG_OPTION_CACHE_INDEX, array(), false );
return count( $index );
}
}
What it does
A shortcode [astronode_panchang lat lng tz label] and theme functions astronode_panchang_html() and astronode_the_panchang(); tz takes an IANA zone or a fixed offset, and defaults to the site's zone.
Calls both endpoints with wp_remote_post and caches the result in a transient until local midnight at that place; a failed call is remembered for five minutes so an outage does not hammer the API.
The key is stored through the Settings API with autoload off, never printed back (only its last four characters), and can be tested or removed from the settings page.
Administrators see the API's error message, code and request id; visitors see a neutral line.
Every output is escaped, every form action checks a nonce and manage_options, and uninstall.php removes the option and all cached transients, including on multisite.
Endpoints and plan
API calls the template makes
Endpoint
Used for
Plan
POST /v1/panchang
Tithi, nakshatra, yoga, karana, sunrise, sunset
Free
POST /v1/muhurta/inauspicious
Rahu Kaal
Hobby
It needs a Hobby key or above: at least one endpoint it calls is not on the Free plan. See pricing.
Where things live
File
What it holds
astronode-panchang.php
Plugin header, the shortcode and the theme functions
includes/class-astronode-panchang-client.php
Request bodies, the two API calls, transient caching
includes/class-astronode-panchang-renderer.php
The escaped HTML card
includes/class-astronode-panchang-settings.php
Settings page, Test connection, Clear cache
uninstall.php
Clean removal
readme.txt
WordPress.org readme
Keys and deployment
The API key is read from the server's environment and sent as Authorization: Bearer <key>; it never reaches a browser or an app bundle.
node check.mjs checks the PHP statically - every string in the text domain, every output escaped, nonces on form handlers, the plugin header - and CI runs php -l on every file.
Install like any plugin: copy the folder to wp-content/plugins/astronode-panchang/ or upload it as a zip from Plugins > Add New.
ASTRONODE_API_URL in wp-config.php (or the astronode_panchang_api_url filter) points it at another API host.
Before submitting to WordPress.org, add a licence header and confirm the Tested up to and contributor fields in readme.txt.
Questions
Which plan does the WordPress panchang plugin need?
Hobby or above, for Rahu Kaal. The panchang itself is on Free; the README shows how to drop the Rahu Kaal call to run on a Free key.
Will a busy WordPress site use up the API quota?
No. Each place's panchang is cached until that place's midnight, so the cost is two calls a day per location shown, however many visitors there are.
Can it show the panchang for the visitor's own city?
It shows the place in the shortcode. For per-visitor places, call astronode_panchang_html() with coordinates from your own lookup; each place is cached separately.