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
| Variable | Type | Description | Example Values |
|---|---|---|---|
| `$isEditor` | `bool` | Editor vs frontend context | `true`, `false` |
| `$OS` | `string` | Operating system family | `Darwin`, `Windows`, `Linux`, `BSD` |
| `$postType` | `string` | Current post type | `post`, `page`, `product`, `event` |
| `$isSingular` | `bool` | Single post/page context | `true`, `false` |
| `$isAdmin` | `bool` | WordPress admin area | `true`, `false` |
| `$currentUserId` | `int` | Current user ID (0 = guest) | `0`, `1`, `42` |
| `$attributes` | `array` | Block attributes | `['title' => 'Hello']` |
| `$content` | `string` | Block inner content | HTML string |
| `$block` | `WP_Block` | WordPress block instance | Block object |
| `$post_id` | `int` | Current post ID | `123` |
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 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– macOSWindows– WindowsLinux– LinuxBSD– BSD variantsSolaris– SolarisUnknown– 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
<!-- 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>
<!-- 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>
<!-- 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 postspage– Pagesattachment– Media filesproduct– WooCommerce productsevent– Custom event post typeteam_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
<!-- 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>
<!-- 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; ?>
<!-- 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; ?>
<!-- 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 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; ?>
<!-- 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 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; ?>
<!-- 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 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; ?>
<!-- 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>
<!-- 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
// 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:
<!-- ❌ 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
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
$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:
- Using the variables inside a block’s
render.phpor symbol template - Not trying to use them in JavaScript (these are PHP-only)
- Using the correct variable name (case-sensitive)
Variable has unexpected value
<!-- Debug helper variables -->
<?php if ( current_user_can('manage_options') ) : ?>
<?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";
?>
<?php endif; ?>
OS Detection Returns Unexpected Value
$OS uses PHP_OS_FAMILY which returns the OS family, not specific versions:
Darwinis macOS (all versions)Windowsis Windows (all versions)Linuxincludes Ubuntu, Debian, CentOS, etc.