PHP Code

Output raw PHP statements in site templates

Template Mode Only

This element is only available when editing Site Templates (tcb_template posts). It does not appear in the Elements panel when editing regular pages or posts.

Overview

The PHP Code element outputs raw PHP statements within your site template. This is useful for executing single-line PHP commands, calling WordPress functions, or adding standalone code that doesn't require a control structure like loops or conditionals.

Unlike PHP If, Foreach, or While elements which wrap content, the PHP Code element simply outputs the code you provide. It's rendered as a <span> in the builder canvas but generates raw PHP in the template output.

Use PHP Code for standalone statements like:

  • Loop control statements (continue;, break;)
  • Variable assignments ($total = 0;)
  • WordPress setup functions (setup_postdata($post);)
  • Output functions (the_content();, echo get_the_date();)

Settings

Configure the PHP Code element using these Inspector options:

Setting Description Default
Code The PHP code to output. Enter the statement without the opening <?php and closing ?> tags — these are added automatically. (empty)

Examples

Loop Control Statements

Use PHP Code inside loops to control iteration flow:

// Skip current iteration
continue;

// Exit loop early
break;

Variable Assignment

Initialize or modify variables within your template:

// Initialize a counter
$total = 0;

// Increment a value
$counter++;

WordPress Setup Functions

Call WordPress functions that set up the environment for subsequent elements:

// Set up post data for a custom query result
setup_postdata($post);

// Reset post data after a custom loop
wp_reset_postdata();

WordPress Output Functions

Output WordPress content using template tags:

// Output the post content
the_content();

// Output the post thumbnail
the_post_thumbnail('large');

// Echo a formatted value
echo get_the_date();

// Output custom field value
echo esc_html(get_field('custom_field'));

Rendered PHP Output

When the template is rendered, PHP Code elements are output as:

<?php the_content(); ?>
Quick Tip

For conditional logic, use the PHP If element. For loops, use PHP Foreach or PHP While. PHP Code is best for standalone statements that don't wrap other content.

Next Steps