Our site www.viart.com site is operated by latest Viart Shop 5 with default Clear design
Printer Friendly Tell a Friend

How to add php code in custom blocks

You can add PHP code to a custom block with the help of PHP tags, and work the contents using $block_body variable. This variable consists of all the HTML text you have in your block.

To activate PHP code functionality for custom blocks, please go to System > Global Settings and select where you want to run PHP code. Also, don't forget to switch from the 'WYSIWYG HTML Editor' to 'Textarea Editor' to view the code in your custom blocks correctly. Then add a new custom block in CMS > Custom Blocks and insert the code in the 'Block Content' area.

The basic code will look like this:

Feel free to add any text here <br> Something <?php echo "1";>
// simple php code

or like this:

Feel free to add any text here <br> Something <?php $block_body = str_replace("Something", "Anything", $block_body); ?>
// php code with a variable replacement

Here is an example of code that can be used in the footer body:

©CopyRight 2004 - {current_year} - All Rights Reserved
<?php
$block_body = str_replace("{current_year}", date("Y"), $block_body);
?>

But please note, executing PHP in admin panel is not very secure that's why from version 4.0 we implemented a new way of adding blocks in CMS - create a PHP script and place it in "blocks_custom" folder then add a new block in CMS > CMS Blocks where specify the name of your script. Then you can select your new block in CMS > Pages Layouts. Also you can specify HTML template for block in CMS > Pages Layouts > new block settings (click on 'tools' icon near block).