Toggle

The Toggle field creates an on / off toggle switch input option for the block.

Settings

  • Help Text: Instructions to describe the data needed in the field.
  • Default Value: The default value for this field when adding the block. Checked for yes / true / 1, unchecked for no / false / 0.

PHP API Controls

  • name
  • label
  • control
  • type
  • order
  • location
  • width
  • help
  • default

Template Usage

To display the Toggle field in your template, use the field name you provided.

<?php block_field( 'pets-availability' ); ?>

Example template file /blocks/block-block-lab-example-pets.php

<?php
// Block Lab Example Toggle Field
?>

<h3>Is this pet still available?</h3>
    <p><?php block_field( 'pets-availability' ); ?></p>

To use the block with the Toggle field on your site, locate it in the blocks menu.

Screenshot showing the Block Lab custom block in the menu

It will then display within your post/page.

And on the front-end of your site.

The API will return a Boolean.

<?php
// Block Lab Example Toggle Field

// Have a red border if the pet is still available or no border if it is not available

$border = 'class="container';
if ( block_value( 'pets-availability' ) ) {
    $border .= '" style="border:3px solid red;padding-top:15px;"';
} else {
    $border .= '"';
}
?>

<div <?php echo $border; ?>>
    <h3>Is this pet still available?</h3>
        <p><?php block_field( 'pets-availability' ); ?></p>
</div>