How To Add PHP code to the body of a Drupal 7 Block

I just want to make a button on block body. Is that possible?

this link will explain what i want to do [add button]: http://drupal.org/node/1046700

This article is for Drupal 7 developers who have tried to put PHP code in the body of a Custom Block, but can't get it to run.

  • What do you want the button to do?
    – Alex Gill
    Commented Dec 11, 2012 at 8:53
  • In that link there is a solution also.
    – niksmac
    Commented Dec 12, 2012 at 5:57

2 Answers 2


I'm not sure why you need this and you want a similar module of PHP filter which adds a new text format.

If you are familiar with custom moduling and hooks, I would give you a straightforward solution. You could implement this using hook_block_view_alter by defining custom patterns (tokens).

function mymodule_block_view_alter(&$data, $block) {
    $patterns       = array('[add button]'); # more replacement patterns here
    $replacements   = array('<button type="button">Button</button>');
        $content = $data['content'];
        if(is_string($content)) {
            $data['content'] = str_replace($patterns, $replacements, $content);
        elseif(is_array($content)) {

enter image description here

Here is the result.

enter image description here
You may expand the code as you like such as defining more tokens, adding criteria for certain blocks, user roles, etc. You can even create an administration page at admin/config to define token replacements from UI.


Enable the PHP filter in the core modules


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.