Template Helper Variables Reference

FanCoolo automatically provides helpful context variables to all block render templates and symbol templates. These variables help you create dynamic, context-aware components without writing repetitive code.

Quick Reference Table

VariableTypeDescriptionExample Values
$isEditorboolEditor vs frontend contexttruefalse
$OSstringOperating system familyDarwinWindowsLinuxBSD
$postTypestringCurrent post typepostpageproductevent
$isSingularboolSingle post/page contexttruefalse
$isAdminboolWordPress admin areatruefalse
$currentUserIdintCurrent user ID (0 = guest)0142
$attributesarrayBlock attributes['title' => 'Hello']
$contentstringBlock inner contentHTML string
$blockWP_BlockWordPress block instanceBlock object
$post_idintCurrent post ID123

1. Editor vs Frontend: $isEditor

Detect if the block is rendering in the Gutenberg editor or on the actual frontend.

When to Use

  • Show preview messages in editor
  • Load different assets for editor vs frontend
  • Disable interactive features in editor preview
  • Show placeholder content in editor

Examples

php
<?php if ( $isEditor ) : ?>
    <div class="editor-preview-notice">
        Preview Mode - This is how your block will look
    </div>
<?php else : ?>
    <div class="live-content">
        <?php echo $content; ?>
    </div>
<?php endif; ?>

2. Operating System: $OS

Detect the server’s operating system. Useful for OS-specific file paths, commands, or features.

Possible Values

  • Darwin – macOS
  • Windows – Windows
  • Linux – Linux
  • BSD – BSD variants
  • Solaris – Solaris
  • Unknown – Fallback

When to Use

  • Show OS-specific keyboard shortcuts
  • Display OS-specific installation instructions
  • Handle OS-specific file paths
  • Customize download links per OS

Examples

php
<!-- OS-specific keyboard shortcuts -->
<div class="shortcuts-help">
    <h3>Keyboard Shortcuts</h3>
    <ul>
        <li>
            <strong>Save:</strong>
            <?php if ( $OS === 'Darwin' ) : ?>
                ⌘ + S
            <?php else : ?>
                Ctrl + S
            <?php endif; ?>
        </li>
        <li>
            <strong>Copy:</strong>
            <?php if ( $OS === 'Darwin' ) : ?>
                ⌘ + C
            <?php else : ?>
                Ctrl + C
            <?php endif; ?>
        </li>
    </ul>
</div>
php
<!-- OS-specific download button -->
<div class="download-section">
    <h2>Download Our App</h2>
    <?php if ( $OS === 'Darwin' ) : ?>
        <a href="/downloads/app-macos.dmg" class="btn">
            Download for macOS
        </a>
    <?php elseif ( $OS === 'Windows' ) : ?>
        <a href="/downloads/app-windows.exe" class="btn">
            Download for Windows
        </a>
    <?php elseif ( $OS === 'Linux' ) : ?>
        <a href="/downloads/app-linux.deb" class="btn">
            Download for Linux
        </a>
    <?php else : ?>
        <a href="/downloads" class="btn">
            View All Downloads
        </a>
    <?php endif; ?>
</div>
php
<!-- Server info display for admins -->
<?php if ( current_user_can('manage_options') ) : ?>
    <div class="server-info">
        <p>Running on: <strong><?php echo esc_html( $OS ); ?></strong></p>
        <p>PHP Version: <?php echo esc_html( PHP_VERSION ); ?></p>
    </div>
<?php endif; ?>

3. Post Type: $postType

Get the current post type (post, page, product, custom post types, etc.).

Common Values

  • post – Blog posts
  • page – Pages
  • attachment – Media files
  • product – WooCommerce products
  • event – Custom event post type
  • team_member – Custom team member post type
  • Any custom post type slug

When to Use

  • Render different layouts per post type
  • Show post-type-specific metadata
  • Conditionally display custom fields
  • Apply different styling classes
  • Show different CTAs per content type

Examples

php
<!-- Different layouts per post type -->
<div class="content-block content-block--<?php echo esc_attr( $postType ); ?>">
    <?php if ( $postType === 'product' ) : ?>
        <div class="product-layout">
            <h2><?php the_title(); ?></h2>
            <p class="price">$<?php echo get_post_meta( $post_id, '_price', true ); ?></p>
            <button class="add-to-cart">Add to Cart</button>
        </div>

    <?php elseif ( $postType === 'event' ) : ?>
        <div class="event-layout">
            <h2><?php the_title(); ?></h2>
            <div class="event-meta">
                <span class="date">
                    <?php echo get_post_meta( $post_id, 'event_date', true ); ?>
                </span>
                <span class="location">
                    <?php echo get_post_meta( $post_id, 'event_location', true ); ?>
                </span>
            </div>
            <button class="register-btn">Register Now</button>
        </div>

    <?php else : ?>
        <div class="default-layout">
            <h2><?php the_title(); ?></h2>
            <?php the_excerpt(); ?>
        </div>
    <?php endif; ?>
</div>
php
<!-- Conditional CTA based on post type -->
<?php if ( in_array( $postType, ['product', 'service'], true ) ) : ?>
    <a href="/contact" class="cta-button">Get a Quote</a>
<?php elseif ( $postType === 'post' ) : ?>
    <a href="/subscribe" class="cta-button">Subscribe to Blog</a>
<?php elseif ( $postType === 'event' ) : ?>
    <a href="<?php the_permalink(); ?>#register" class="cta-button">Register</a>
<?php endif; ?>
php
<!-- Show custom taxonomy terms for specific post types -->
<?php if ( $postType === 'portfolio' ) : ?>
    <div class="portfolio-categories">
        <?php
        $terms = get_the_terms( $post_id, 'portfolio_category' );
        if ( $terms && !is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                echo '<span class="category-tag">' . esc_html( $term->name ) . '</span>';
            }
        }
        ?>
    </div>
<?php endif; ?>
php
<!-- Exclude specific post types from certain features -->
<?php if ( !in_array( $postType, ['attachment', 'revision', 'nav_menu_item'], true ) ) : ?>
    <div class="social-share">
        <ShareButtons />
    </div>
<?php endif; ?>

4. Singular Context: $isSingular

Check if you’re viewing a single post/page (vs archive, search, home).

When to Use

  • Show full content on single posts, excerpts on archives
  • Display different metadata on singles vs archives
  • Show author bio only on single posts
  • Enable comments only on singular views
  • Load heavy scripts only on single post pages

Examples

php
<?php if ( $isSingular ) : ?>
    <!-- Full single post view -->
    <article class="full-post">
        <h1><?php the_title(); ?></h1>
        <div class="post-meta">
            <span class="author"><?php the_author(); ?></span>
            <span class="date"><?php the_date(); ?></span>
            <span class="reading-time">5 min read</span>
        </div>
        <div class="post-content">
            <?php the_content(); ?>
        </div>
        <div class="author-bio">
            <?php echo get_the_author_meta('description'); ?>
        </div>
    </article>
<?php else : ?>
    <!-- Archive excerpt view -->
    <article class="post-excerpt">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="excerpt">
            <?php the_excerpt(); ?>
        </div>
        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
    </article>
<?php endif; ?>
php
<!-- Related posts only on single posts -->
<?php if ( $isSingular && $postType === 'post' ) : ?>
    <div class="related-posts">
        <h3>You Might Also Like</h3>
        <RelatedPosts />
    </div>
<?php endif; ?>

5. Admin Area: $isAdmin

Check if the current page is in the WordPress admin dashboard.

Note: This is different from $isEditor:

  • $isAdmin = true: Any admin dashboard page (Posts, Settings, etc.)
  • $isEditor = true: Gutenberg editor making REST API requests

When to Use

  • Show admin-only debugging info
  • Display different content in admin vs frontend
  • Disable frontend-only features in admin

Examples

php
<?php if ( $isAdmin && current_user_can('manage_options') ) : ?>
    <div class="admin-debug-info">
        <h4>Debug Info (Admin Only)</h4>
        <ul>
            <li>Post ID: <?php echo $post_id; ?></li>
            <li>Post Type: <?php echo esc_html( $postType ); ?></li>
            <li>OS: <?php echo esc_html( $OS ); ?></li>
            <li>Is Editor: <?php echo $isEditor ? 'Yes' : 'No'; ?></li>
        </ul>
    </div>
<?php endif; ?>
php
<!-- Only load frontend analytics on actual frontend -->
<?php if ( !$isAdmin && !$isEditor ) : ?>
    <script>
        // Load Google Analytics, tracking pixels, etc.
        gtag('config', 'UA-XXXXXX');
    </script>
<?php endif; ?>

6. Current User: $currentUserId

Get the currently logged-in user’s ID. Returns 0 if user is not logged in (guest).

When to Use

  • Show personalized content for logged-in users
  • Display member-only sections
  • Show login prompt for guests
  • Customize CTAs based on login status
  • Check user-specific permissions

Examples

php
<?php if ( $currentUserId > 0 ) : ?>
    <!-- Logged in user content -->
    <div class="user-welcome">
        <p>Welcome back, <?php echo esc_html( get_userdata( $currentUserId )->display_name ); ?>!</p>
        <a href="<?php echo admin_url('profile.php'); ?>">View Profile</a>
        <a href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a>
    </div>
<?php else : ?>
    <!-- Guest user content -->
    <div class="login-prompt">
        <p>Please log in to access member features</p>
        <a href="<?php echo wp_login_url( get_permalink() ); ?>" class="btn">Login</a>
        <a href="<?php echo wp_registration_url(); ?>" class="btn">Register</a>
    </div>
<?php endif; ?>
php
<!-- Show different CTA based on login status -->
<div class="premium-content-block">
    <h3>Premium Content</h3>
    <?php if ( $currentUserId > 0 ) : ?>
        <?php if ( user_can( $currentUserId, 'read_premium_content' ) ) : ?>
            <div class="premium-content">
                <?php echo $content; ?>
            </div>
        <?php else : ?>
            <div class="upgrade-prompt">
                <p>Upgrade to access this content</p>
                <a href="/pricing" class="btn">View Plans</a>
            </div>
        <?php endif; ?>
    <?php else : ?>
        <div class="login-wall">
            <p>Login to access premium content</p>
            <a href="<?php echo wp_login_url( get_permalink() ); ?>" class="btn">Login</a>
        </div>
    <?php endif; ?>
</div>
php
<!-- Personalized recommendations -->
<?php if ( $currentUserId > 0 ) : ?>
    <div class="recommendations">
        <h3>Recommended for You</h3>
        <PersonalizedPosts userId="<?php echo $currentUserId; ?>" />
    </div>
<?php else : ?>
    <div class="popular-posts">
        <h3>Popular Posts</h3>
        <PopularPosts />
    </div>
<?php endif; ?>

Context-Aware Component

php
<?php
// Determine component variant based on multiple contexts
$variant = 'default';

if ( $isEditor ) {
    $variant = 'editor-preview';
} elseif ( $postType === 'product' && $isSingular ) {
    $variant = 'product-single';
} elseif ( $postType === 'post' && !$isSingular ) {
    $variant = 'blog-archive';
} elseif ( $currentUserId > 0 ) {
    $variant = 'authenticated';
}

$classes = [
    'content-component',
    'content-component--' . $variant,
    'content-component--' . $postType,
];
?>

<div class="<?php echo esc_attr( implode(' ', $classes) ); ?>">
    <?php echo $content; ?>
</div>

Performance Tips

1. Avoid Redundant Checks

Don’t re-check conditions that are already provided:

php
<!-- ❌ Bad: Re-checking what's already available -->
<?php if ( defined('REST_REQUEST') && REST_REQUEST ) : ?>

<!-- ✅ Good: Use the provided helper -->
<?php if ( $isEditor ) : ?>

2. Cache User Lookups

If you need user data multiple times, cache it:

php
<?php
if ( $currentUserId > 0 ) {
    $user = get_userdata( $currentUserId ); // Cache this
    $displayName = $user->display_name;
    $userEmail = $user->user_email;
    $avatarUrl = get_avatar_url( $currentUserId );
}
?>

3. Combine Conditions

Group related checks to avoid duplicate rendering:

php
<?php
$showCTA = (
    $isSingular &&
    $postType === 'post' &&
    $currentUserId === 0 &&
    !$isEditor
);

if ( $showCTA ) : ?>
    <SubscribeCTA />
<?php endif; ?>

Troubleshooting

Variable is undefined

If you see “Undefined variable” errors, ensure you’re:

  1. Using the variables inside a block’s render.php or symbol template
  2. Not trying to use them in JavaScript (these are PHP-only)
  3. Using the correct variable name (case-sensitive)

Variable has unexpected value

php
<!-- Debug helper variables -->
<?php if ( current_user_can('manage_options') ) : ?>
    <pre><?php
        echo "isEditor: " . var_export($isEditor, true) . "\n";
        echo "OS: " . $OS . "\n";
        echo "postType: " . $postType . "\n";
        echo "isSingular: " . var_export($isSingular, true) . "\n";
        echo "isAdmin: " . var_export($isAdmin, true) . "\n";
        echo "currentUserId: " . $currentUserId . "\n";
    ?></pre>
<?php endif; ?>

OS Detection Returns Unexpected Value

$OS uses PHP_OS_FAMILY which returns the OS family, not specific versions:

  • Darwin is macOS (all versions)
  • Windows is Windows (all versions)
  • Linux includes Ubuntu, Debian, CentOS, etc.