I have been having this problem for quite some time now and I don't know how to proceed.

I am trying to get a block by invoking it, the simple way:

 $block2 = module_invoke("module", "block_view", "nameofblock");
  print $block2["content"];

and insert it after a specific div with a unique id.

I tried using JavaScript and jquery:




//and all these

(function($) {

$(window).ready(function() {

but still the block is rendered where i originally added it, and if i put php inside of javascript, the new block will return a and close my script prematurely...

so is there a way to show this block in the specific div id by using javascript/jquery or is there a way to use php aray to check for the required id then invoking the block.

this is giving me a headache for quite sometime and i am out of options.

thanks in advance

  • hook_page_alter - Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module's elements Mar 4, 2015 at 3:56
  • Can you expand a bit more on that, how to attach a block invoked to a specific block using hook_page_alter. Mar 5, 2015 at 13:39
  • THis might give you an idea: Using hook_page_alter to move node render items into blocks Mar 5, 2015 at 15:53

1 Answer 1


You can achieve this by creating a custom module which prints block content as shown below:

function mymodule_menu(){
    $items = array();

    $items['ajax/replace/block'] = array(
        'title' => t('Ajax Block'),
        'page callback' => 'ajax_callback',
        'access callback' => true,
        'type' => MENU_NORMAL_ITEM

    return $items;

function ajax_callback(){
    $block2 = module_invoke("block", "block_view", 1);
    print $block2["content"];

Then add following jQuery line in your page to replace the target element with 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.