31. Programmatic SEO Analysis | SEO Forge - Rank Higher with AI-Powered SEO
Download Log in

31. Programmatic SEO Analysis

Developer Guide

Trigger analysis, read results, perform bulk operations, and integrate scoring into custom workflows.

Triggering Analysis

php
$analyzer = SEOFORGE_Analyzer::instance();

// Run full analysis
$result = $analyzer->analyze( $post_id );
// Returns: [ 'score' => 72, 'issues' => [...], 'suggestions' => [...] ]

// Get cached result (no re-analysis)
$cached = $analyzer->get_cached( $post_id );

Bulk Analysis

php
$post_ids = get_posts( [
    'post_type' => [ 'post', 'page' ], 'post_status' => 'publish',
    'posts_per_page' => -1, 'fields' => 'ids',
] );

$analyzer = SEOFORGE_Analyzer::instance();
$good = $medium = $bad = 0;

foreach ( $post_ids as $pid ) {
    $result = $analyzer->analyze( $pid );
    $score  = $result['score'];
    if ( $score >= 80 ) { $good++; }
    elseif ( $score >= 50 ) { $medium++; }
    else { $bad++; }
}

echo "{$good} good, {$medium} medium, {$bad} badn";

Bulk Operations (Programmatic)

php
// Bulk-set focus keywords from a mapping array
$keywords = [
    42 => 'wordpress seo',
    55 => 'woocommerce optimization',
    78 => 'google search console setup',
];

foreach ( $keywords as $post_id => $keyword ) {
    update_post_meta( $post_id, '_seoforge_focus_keyword', sanitize_text_field( $keyword ) );
    SEOFORGE_Analyzer::instance()->analyze( $post_id );
}

// Bulk-generate AI meta for all posts missing descriptions (PRO)
if ( SEOFORGE_License::is_pro() ) {
    $missing = get_posts( [
        'post_type' => 'post', 'post_status' => 'publish',
        'posts_per_page' => -1, 'fields' => 'ids',
        'meta_query' => [
            'relation' => 'OR',
            [ 'key' => '_seoforge_meta_description', 'compare' => 'NOT EXISTS' ],
            [ 'key' => '_seoforge_meta_description', 'value' => '' ],
        ],
    ] );

    foreach ( $missing as $pid ) {
        $request  = new WP_REST_Request( 'POST', '/seoforge/v1/meta/generate' );
        $request->set_param( 'post_id', $pid );
        $response = rest_do_request( $request );
        $data     = $response->get_data();

        if ( ! empty( $data['title'] ) ) {
            update_post_meta( $pid, '_seoforge_meta_title', $data['title'] );
        }
        if ( ! empty( $data['description'] ) ) {
            update_post_meta( $pid, '_seoforge_meta_description', $data['description'] );
        }
    }
}

Export Analysis Data as CSV

php
$post_ids = get_posts( [
    'post_type' => 'post', 'post_status' => 'publish',
    'posts_per_page' => -1, 'fields' => 'ids',
] );

$csv = "Post ID,Title,Score,Issues Count,Analyzed Atn";
$analyzer = SEOFORGE_Analyzer::instance();

foreach ( $post_ids as $pid ) {
    $cached = $analyzer->get_cached( $pid );
    if ( ! $cached ) { continue; }
    $issues = json_decode( $cached->issues, true );
    $csv .= sprintf( "%d,"%s",%d,%d,%sn",
        $pid, str_replace( '"', '""', get_the_title( $pid ) ),
        (int) $cached->score, is_array( $issues ) ? count( $issues ) : 0, $cached->analyzed_at
    );
}

file_put_contents( WP_CONTENT_DIR . '/seo-scores-export.csv', $csv );

Forge AI Assistant Online

Hi! I'm the SEO Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs