← Integrations

No-code / CMS · PHP

A daily horoscope shortcode for WordPress

Most WordPress sites that show a horoscope want one block per sign, refreshed daily. That is twelve API calls a day, not one per page view - if you cache. Here is a single-file plugin that does exactly that, with the key kept in wp-config.php.

Updated

How this page was checked: The plugin was not executed - there is no PHP runtime on the build machine. The request it makes was sent with curl to the in-memory API (the response below and the lowercase-sign 400 are from those runs).

Keep the key out of the database

Options-page settings are stored in wp_options, visible to every administrator and copied into every database export. Define the key as a constant in wp-config.php, which lives outside the web root on most hosts and never ends up in a backup of content:

php
// wp-config.php, above "That's all, stop editing!"
define( 'ASTRONODE_API_KEY', 'aie_live_...' );

The plugin

wp-content/plugins/astronode-daily/astronode-daily.php:

php
<?php
/**
 * Plugin Name: AstroNode Daily Horoscope
 * Description: [astronode_daily sign="Aries"] - one AstroNode call per sign per day.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

const ASTRONODE_SIGNS = array( 'Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo',
	'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces' );

function astronode_daily( $sign ) {
	$sign = ucfirst( strtolower( trim( $sign ) ) ); // the API wants "Aries", not "aries"
	if ( ! in_array( $sign, ASTRONODE_SIGNS, true ) ) {
		return null;
	}
	$date = wp_date( 'Y-m-d' ); // the site's timezone, so "today" matches your readers
	$key  = 'astronode_daily_' . $sign . '_' . $date;

	$cached = get_transient( $key );
	if ( false !== $cached ) {
		return $cached;
	}

	$url = add_query_arg( 'date', $date, 'https://api.astronode.dev/v1/horoscope/daily/' . $sign );
	$res = wp_remote_get( $url, array(
		'timeout' => 15, // the default is 5 seconds
		'headers' => array( 'Authorization' => 'Bearer ' . ASTRONODE_API_KEY ),
	) );
	if ( is_wp_error( $res ) || 200 !== wp_remote_retrieve_response_code( $res ) ) {
		return null; // render nothing rather than an error on a public page
	}
	$body = json_decode( wp_remote_retrieve_body( $res ), true );
	set_transient( $key, $body['data'], DAY_IN_SECONDS );
	return $body['data'];
}

add_shortcode( 'astronode_daily', function ( $atts ) {
	$atts = shortcode_atts( array( 'sign' => 'Aries' ), $atts, 'astronode_daily' );
	$h    = astronode_daily( $atts['sign'] );
	if ( ! $h ) {
		return '';
	}
	$out  = '<div class="astronode-daily">';
	$out .= '<h3>' . esc_html( $h['sign'] . ' - ' . $h['date'] ) . '</h3>';
	$out .= '<p>' . esc_html( $h['summary'] ) . '</p><ul>';
	foreach ( $h['focus_areas'] as $item ) {
		$out .= '<li>' . esc_html( $item ) . '</li>';
	}
	$out .= '</ul><p class="astronode-disclaimer">' . esc_html( $h['disclaimer'] ) . '</p></div>';
	return $out;
} );

Put [astronode_daily sign="Leo"] in any post, page or shortcode block. The transient key includes the date, so tomorrow's first view fetches tomorrow's reading; the twelve signs cost at most twelve calls a day however busy the site is. With a persistent object cache (Redis, Memcached) transients live there instead of wp_options - no code change.

What the API returned

GET /v1/horoscope/daily/Aries?date=2026-12-01 → data:

json
{
  "type": "daily_horoscope",
  "sign": "Aries",
  "date": "2026-12-01",
  "weekday": "Tuesday",
  "summary": "Energy is forward today. Move on the thing you've been postponing - momentum compounds.",
  "focus_areas": ["First steps over polish", "Honest direct conversations", "Physical activity early in the day"],
  "cautions": ["Snap reactions when challenged", "Taking on more than fits the day"],
  "disclaimer": "Vedic astrology is offered here for cultural and informational purposes only. ..."
}

cautions is there too if your design has room for it. Every string goes through esc_html() - the content is the API's, but the page is yours, and escaping is the WordPress rule for anything that did not originate in your own template.

The mistakes that bite first

  • A lowercase sign. Editors type sign="aries"; the path parameter is case-sensitive and GET /v1/horoscope/daily/aries returned 400 VALIDATION_ERROR listing "Aries" | "Taurus" | .... Normalise with ucfirst( strtolower( ... ) ) as above.
  • No cache. Without the transient, every page view (and every crawler hit) is an API call - a popular page can spend a month's quota in a day.
  • The 5-second default timeout. wp_remote_get gives up after 5 seconds by default; on a cold cache that can blank the block. Set timeout explicitly.
  • POSTing an array. If you extend the plugin to POST endpoints (panchang, birth charts), wp_remote_post form-encodes a PHP array body. Send 'body' => wp_json_encode( $data ) with a Content-Type: application/json header, or every call is a 400.

Questions

Which AstroNode endpoints does the WordPress example call?
1 endpoint: Daily Sun Sign Horoscope (GET /v1/horoscope/daily/:sign).
Was the WordPress code on this page actually run?
The plugin was not executed - there is no PHP runtime on the build machine. The request it makes was sent with curl to the in-memory API (the response below and the lowercase-sign 400 are from those runs).