13. Custom Meta Template Variables | SEO Forge - Rank Higher with AI-Powered SEO
Download Log in

13. Custom Meta Template Variables

Developer Guide

You can register custom template variables that become available in the meta template system. This is useful for WooCommerce product data, custom fields, or any dynamic content.

Registering Custom Variables via Filter

SEO Forge resolves variables by calling SEOFORGE_Meta_Templates::resolve(). To inject custom variables, hook into the resolution process by filtering the available variables and providing a replacement callback:

php
// Register a custom {{price}} variable for WooCommerce products
add_action( 'plugins_loaded', function () {
    if ( ! class_exists( 'SEOFORGE_Meta_Templates' ) ) {
        return;
    }

    // Add to the variable list (shown in template editor UI)
    add_filter( 'seoforge_template_variables', function ( $vars ) {
        $vars['{{price}}']    = 'Product price (WooCommerce)';
        $vars['{{sku}}']      = 'Product SKU (WooCommerce)';
        $vars['{{stock}}']    = 'Stock status (In stock / Out of stock)';
        $vars['{{brand}}']    = 'Product brand (custom taxonomy)';
        return $vars;
    } );

    // Resolve the variables when building meta output
    add_filter( 'seoforge_resolve_template', function ( $output, $post_id ) {
        if ( get_post_type( $post_id ) !== 'product' || ! function_exists( 'wc_get_product' ) ) {
            return $output;
        }

        $product = wc_get_product( $post_id );
        if ( ! $product ) {
            return $output;
        }

        $replacements = [
            '{{price}}' => $product->get_price() ? wp_strip_all_tags( wc_price( $product->get_price() ) ) : '',
            '{{sku}}'   => $product->get_sku(),
            '{{stock}}' => $product->is_in_stock() ? 'In stock' : 'Out of stock',
            '{{brand}}' => '',
        ];

        $brands = wp_get_post_terms( $post_id, 'product_brand', [ 'fields' => 'names' ] );
        if ( ! is_wp_error( $brands ) && ! empty( $brands ) ) {
            $replacements['{{brand}}'] = $brands[0];
        }

        return str_replace( array_keys( $replacements ), array_values( $replacements ), $output );
    }, 10, 2 );
}, 10 );

Using Custom Variables in Templates

Once registered, custom variables can be used in the global template settings:

php
update_option( 'seoforge_title_templates', [
    'product' => [
        'title'       => '{{title}} {{separator}} {{price}} {{separator}} {{siteName}}',
        'description' => '{{brand}} {{title}} - {{stock}}. {{excerpt}}',
    ],
] );

ACF Field Variable Example

php
// Register ACF custom fields as template variables
add_filter( 'seoforge_template_variables', function ( $vars ) {
    $vars['{{location}}']   = 'ACF Location field';
    $vars['{{event_date}}'] = 'ACF Event Date field';
    return $vars;
} );

add_filter( 'seoforge_resolve_template', function ( $output, $post_id ) {
    if ( ! function_exists( 'get_field' ) ) {
        return $output;
    }

    $output = str_replace( '{{location}}', get_field( 'location', $post_id ) ?: '', $output );
    $output = str_replace( '{{event_date}}', get_field( 'event_date', $post_id ) ?: '', $output );
    return $output;
}, 10, 2 );

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