WealthyAdviser Automation Settings
<h2>API Keys</h2>
<table class="form-table">
<tr>
<th>ExchangeRate API Key</th>
<td><input type="text" name="wa_exchangerate_api_key" value="<?php echo esc_attr( get_option('wa_exchangerate_api_key') ); ?>" size="50" />
<p class="description">Free key from <a href="https://www.exchangerate-api.com/" target="_blank">exchangerate-api.com</a></p></td>
</tr>
<tr>
<th>News API Key</th>
<td><input type="text" name="wa_news_api_key" value="<?php echo esc_attr( get_option('wa_news_api_key') ); ?>" size="50" />
<p class="description">Free key from <a href="https://gnews.io/" target="_blank">gnews.io</a></p></td>
</tr>
<tr>
<th>Anthropic API Key</th>
<td><input type="password" name="wa_anthropic_api_key" value="<?php echo esc_attr( get_option('wa_anthropic_api_key') ); ?>" size="50" />
<p class="description">From <a href="https://console.anthropic.com/" target="_blank">console.anthropic.com</a> (pay-per-use, separate from Claude.ai)</p></td>
</tr>
</table>
<h2>Blog Topics Pool</h2>
<p class="description">One topic per line. The daily blog job picks one at random each day and marks it used. Add more anytime.</p>
<textarea name="wa_blog_topics" rows="8" cols="80"><?php echo esc_textarea( get_option('wa_blog_topics') ); ?></textarea>
<h2>Manual Data (no reliable free API — update when rates change)</h2>
<table class="form-table">
<tr>
<th>Top NSS Schemes</th>
<td><textarea name="wa_nss_rates" rows="4" cols="60"><?php echo esc_textarea( get_option('wa_nss_rates') ); ?></textarea></td>
</tr>
<tr>
<th>Top Performing Stocks Today</th>
<td><textarea name="wa_stock_highlights" rows="4" cols="60"><?php echo esc_textarea( get_option('wa_stock_highlights') ); ?></textarea></td>
</tr>
<tr>
<th>Top Bank Plans</th>
<td><textarea name="wa_bank_plans" rows="4" cols="60"><?php echo esc_textarea( get_option('wa_bank_plans') ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<hr>
<h2>Manual Test Triggers</h2>
<p>Use these to test a job right now instead of waiting for the schedule.</p>
<a href="<?php echo admin_url('admin-post.php?action=wa_test_currency'); ?>" class="button">Run Currency Update Now</a>
<a href="<?php echo admin_url('admin-post.php?action=wa_test_news'); ?>" class="button">Run News Update Now</a>
<a href="<?php echo admin_url('admin-post.php?action=wa_test_blog'); ?>" class="button">Generate Blog Draft Now</a>
</div>
<?php
}
// ============================================================
// 2. SCHEDULING — register custom intervals + hook jobs to them
// ============================================================
add_filter( ‘cron_schedules’, function( $schedules ) {
$schedules[‘hourly’] = $schedules[‘hourly’] ?? array( ‘interval’ => 3600, ‘display’ => ‘Every Hour’ );
return $schedules;
});
register_activation_hook( FILE, function() {
if ( ! wp_next_scheduled( ‘wa_hourly_currency_job’ ) ) {
wp_schedule_event( time(), ‘hourly’, ‘wa_hourly_currency_job’ );
}
if ( ! wp_next_scheduled( ‘wa_daily_news_job’ ) ) {
wp_schedule_event( time(), ‘daily’, ‘wa_daily_news_job’ );
}
if ( ! wp_next_scheduled( ‘wa_daily_blog_job’ ) ) {
wp_schedule_event( time(), ‘daily’, ‘wa_daily_blog_job’ );
}
});
register_deactivation_hook( FILE, function() {
wp_clear_scheduled_hook( ‘wa_hourly_currency_job’ );
wp_clear_scheduled_hook( ‘wa_daily_news_job’ );
wp_clear_scheduled_hook( ‘wa_daily_blog_job’ );
});
add_action( ‘wa_hourly_currency_job’, ‘wa_fetch_currency_rates’ );
add_action( ‘wa_daily_news_job’, ‘wa_fetch_business_news’ );
add_action( ‘wa_daily_blog_job’, ‘wa_generate_daily_blog’ );
// Manual test triggers (admin only)
add_action( ‘admin_post_wa_test_currency’, function() { wa_fetch_currency_rates(); wp_redirect( admin_url(‘options-general.php?page=wa-automation&done=currency’) ); exit; });
add_action( ‘admin_post_wa_test_news’, function() { wa_fetch_business_news(); wp_redirect( admin_url(‘options-general.php?page=wa-automation&done=news’) ); exit; });
add_action( ‘admin_post_wa_test_blog’, function() { wa_generate_daily_blog(); wp_redirect( admin_url(‘options-general.php?page=wa-automation&done=blog’) ); exit; });
// ============================================================
// 3. CURRENCY + GOLD RATES (hourly)
// ============================================================
function wa_fetch_currency_rates() {
$api_key = get_option( ‘wa_exchangerate_api_key’ );
if ( ! $api_key ) return;
$response = wp_remote_get( "https://v6.exchangerate-api.com/v6/{$api_key}/latest/USD", array( 'timeout' => 15 ) );
if ( is_wp_error( $response ) ) return;
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $body['conversion_rates']['PKR'] ) ) return;
$usd_to_pkr = $body['conversion_rates']['PKR'];
// Convert a curated list of currencies to PKR
$currencies = array( 'USD', 'GBP', 'EUR', 'SAR', 'AED', 'KWD', 'CAD', 'AUD', 'OMR', 'QAR', 'BHD', 'JPY', 'CNY', 'MYR', 'SGD', 'INR', 'THB', 'BDT' );
$rates_pkr = array();
foreach ( $currencies as $code ) {
if ( isset( $body['conversion_rates'][ $code ] ) && $body['conversion_rates'][ $code ] > 0 ) {
$rates_pkr[ $code ] = round( $usd_to_pkr / $body['conversion_rates'][ $code ], 2 );
}
}
update_option( 'wa_currency_rates', $rates_pkr );
update_option( 'wa_currency_updated_at', current_time( 'mysql' ) );
}
// Shortcode:
- US Dollar: 277.67 PKR
- British Pound: 376.05 PKR
- Euro: 322.35 PKR
- Saudi Riyal: 74.05 PKR
- UAE Dirham: 75.61 PKR
- Kuwaiti Dinar: 899.2 PKR
- Canadian Dollar: 200.3 PKR
- Australian Dollar: 198.91 PKR
- Omani Riyal: 722.17 PKR
- Qatari Riyal: 76.28 PKR
- Bahraini Dinar: 738.5 PKR
- Japanese Yen: 1.74 PKR
- Chinese Yuan: 41.22 PKR
- Malaysian Ringgit: 68.97 PKR
- Singapore Dollar: 218.33 PKR
- Indian Rupee: 2.92 PKR
- Thai Baht: 8.37 PKR
- Bangladeshi Taka: 2.26 PKR
add_shortcode( ‘wa_currency_ticker’, function() {
$rates = get_option( ‘wa_currency_rates’, array() );
$updated = get_option( ‘wa_currency_updated_at’, ” );
if ( empty( $rates ) ) return ‘
Currency rates loading…’;
$labels = array(
'USD' => 'US Dollar', 'GBP' => 'British Pound', 'EUR' => 'Euro', 'SAR' => 'Saudi Riyal',
'AED' => 'UAE Dirham', 'KWD' => 'Kuwaiti Dinar', 'CAD' => 'Canadian Dollar', 'AUD' => 'Australian Dollar',
'OMR' => 'Omani Riyal', 'QAR' => 'Qatari Riyal', 'BHD' => 'Bahraini Dinar', 'JPY' => 'Japanese Yen',
'CNY' => 'Chinese Yuan', 'MYR' => 'Malaysian Ringgit', 'SGD' => 'Singapore Dollar', 'INR' => 'Indian Rupee',
'THB' => 'Thai Baht', 'BDT' => 'Bangladeshi Taka',
);
ob_start();
echo '<div class="wa-ticker"><strong>MARKET HIGHLIGHTS</strong> (updated ' . esc_html( $updated ) . ' PKT)<ul style="list-style:none;padding:0;">';
foreach ( $rates as $code => $val ) {
$label = $labels[ $code ] ?? $code;
echo '<li>' . esc_html( $label ) . ': ' . esc_html( $val ) . ' PKR</li>';
}
echo '</ul></div>';
return ob_get_clean();
});
// ============================================================
// 4. BUSINESS / ECONOMIC NEWS (daily)
// ============================================================
function wa_fetch_business_news() {
$api_key = get_option( ‘wa_news_api_key’ );
if ( ! $api_key ) return;
// Pakistan business news
$pk_response = wp_remote_get( "https://gnews.io/api/v4/search?q=Pakistan%20economy%20OR%20business&lang=en&max=5&apikey={$api_key}", array( 'timeout' => 15 ) );
// Global business news
$global_response = wp_remote_get( "https://gnews.io/api/v4/top-headlines?category=business&lang=en&max=5&apikey={$api_key}", array( 'timeout' => 15 ) );
$stories = array();
if ( ! is_wp_error( $pk_response ) ) {
$data = json_decode( wp_remote_retrieve_body( $pk_response ), true );
if ( ! empty( $data['articles'] ) ) {
foreach ( $data['articles'] as $a ) {
$stories[] = array( 'title' => $a['title'], 'url' => $a['url'], 'source' => $a['source']['name'] ?? '', 'region' => 'Pakistan' );
}
}
}
if ( ! is_wp_error( $global_response ) ) {
$data = json_decode( wp_remote_retrieve_body( $global_response ), true );
if ( ! empty( $data['articles'] ) ) {
foreach ( $data['articles'] as $a ) {
$stories[] = array( 'title' => $a['title'], 'url' => $a['url'], 'source' => $a['source']['name'] ?? '', 'region' => 'Global' );
}
}
}
update_option( 'wa_business_news', $stories );
update_option( 'wa_news_updated_at', current_time( 'mysql' ) );
}
// Shortcode:
- [Pakistan] Govt urged to withdraw petroleum levy — The Nation
- [Pakistan] Pakistan’s stunting crisis threatens economic future as 40 pc of children under five affected — Lokmat Times
- [Pakistan] Dar terms transformation of Pakistan’s image, economy miraculous — The Nation
- [Pakistan] Pakistan's worsening security situation threatens CPEC, Chinese projects — Lokmat Times
- [Pakistan] 'Punjab Data Lake' launched as Maryam Nawaz seeks to boost Pakistan-China technology ties — BOL News
add_shortcode( ‘wa_business_news’, function() {
$stories = get_option( ‘wa_business_news’, array() );
if ( empty( $stories ) ) return ‘
News loading…’;
ob_start();
echo '<div class="wa-news"><strong>Business & Economic News</strong><ul>';
foreach ( $stories as $s ) {
echo '<li><span style="font-weight:600;">[' . esc_html( $s['region'] ) . ']</span> <a href="' . esc_url( $s['url'] ) . '" target="_blank" rel="noopener">' . esc_html( $s['title'] ) . '</a> — <em>' . esc_html( $s['source'] ) . '</em></li>';
}
echo '</ul></div>';
return ob_get_clean();
});
// ============================================================
// 5. DAILY AI BLOG POST (via Anthropic API) — saved as DRAFT for review
// ============================================================
function wa_generate_daily_blog() {
$api_key = get_option( ‘wa_anthropic_api_key’ );
if ( ! $api_key ) return;
// Pick a topic from the pool, prefer ones not yet used
$topics_raw = get_option( 'wa_blog_topics', '' );
$topics = array_filter( array_map( 'trim', explode( "\n", $topics_raw ) ) );
if ( empty( $topics ) ) {
$topics = array( 'How everyday Pakistanis can start saving despite inflation' );
}
$topic = $topics[ array_rand( $topics ) ];
$prompt = "Write a 700-900 word blog post for a Pakistani personal finance website called WealthyAdviser. "
. "Topic: \"{$topic}\". Audience: everyday people in Pakistan making personal finance and savings decisions. "
. "Tone: clear, practical, encouraging, no jargon without explanation. Include a short intro, 3-5 subheadings, "
. "and a brief closing note that this is educational information, not personalized financial advice. "
. "Use PKR where relevant. Return ONLY the blog post in Markdown (## for subheadings), no preamble.";
$response = wp_remote_post( 'https://api.anthropic.com/v1/messages', array(
'timeout' => 60,
'headers' => array(
'x-api-key' => $api_key,
'anthropic-version' => '2023-06-01',
'content-type' => 'application/json',
),
'body' => json_encode( array(
'model' => 'claude-sonnet-4-6',
'max_tokens' => 2000,
'messages' => array(
array( 'role' => 'user', 'content' => $prompt )
),
)),
));
if ( is_wp_error( $response ) ) {
error_log( 'WA Blog Job Error: ' . $response->get_error_message() );
return;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $body['content'][0]['text'] ) ) {
error_log( 'WA Blog Job Error: empty response - ' . wp_remote_retrieve_body( $response ) );
return;
}
$content_md = $body['content'][0]['text'];
// Basic Markdown -> HTML for headings/paragraphs (lightweight, no external library)
$content_html = wa_simple_markdown_to_html( $content_md );
$post_id = wp_insert_post( array(
'post_title' => $topic,
'post_content' => $content_html,
'post_status' => 'draft', // review before publishing — remove this safeguard only once you trust the output
'post_type' => 'post',
'post_author' => 1,
'meta_input' => array( '_wa_ai_generated' => true ),
));
if ( $post_id ) {
update_option( 'wa_last_blog_generated', current_time( 'mysql' ) . ' — ' . $topic );
}
}
function wa_simple_markdown_to_html( $md ) {
$lines = explode( “\n”, $md );
$html = ”;
foreach ( $lines as $line ) {
$line = trim( $line );
if ( $line === ” ) continue;
if ( str_starts_with( $line, ‘## ‘ ) ) {
$html .= ‘
‘ . esc_html( substr( $line, 3 ) ) . ‘
‘;
} elseif ( str_starts_with( $line, ‘# ‘ ) ) {
$html .= ‘
‘ . esc_html( substr( $line, 2 ) ) . ‘
‘;
} else {
$html .= ‘
‘ . esc_html( $line ) . ”;
}
}
return $html;
}