I have a block, and want to print it in the template. I use the following code:

 $block =  module_invoke('block', 'block_view', '6');
 print render($block["content"]);

It works, but in module_invoke() I use the ID of the block in the database, which is very bad. Is it possible to assign the string ID to the block and use it instead?

If it's impossible, how can I use strings instead of numbers?

2 Answers 2


Use this:

<?php print render(block_get_blocks_by_region('YOUR_REGION_NAME')); ?>

put this code in your .tpl file and make sure you add your block in that region.


There's no built-in function for loading a block by string, but you can use a simple SQL query to get the ID of the block using a string.

$block_name = "Your block name";

$block_id = db_query("SELECT bid FROM {block_custom} WHERE info=:info", array(":info"=>$block_name))->fetchField();

$block =  module_invoke('block', 'block_view', $block_id);
print render($block["content"]);

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.