## General developer forum

### add a block by default to a certain region

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
add a block by default to a certain region

Hi,

I'm a beginner developing with Moodle 2.2.6. I needed a custom header, I created a block and a region in my theme. Now I can place manually the header in th correct region.

I need this block in all pages of my site. how can I define that this block goes by default to that region?

Average of ratings: -
Re: add a block by default to a certain region

What we do is specific the block method "specialization()". You can see that this method exists in the block_base class at blocks/moodleblock.class.php.

This function is called whenever a block is instantiated. You can put some code in that funtion to set the value of "defaultregion" of that block's entry in "block_instances" to force it to be either BLOCK_POS_LEFT or BLOCK_POS_RIGHT. You can also set the defaultweight so you can force it to be on the top or bottom of either columns.

NOTE, you have access to the object from the block_instance table by using $this->instance. So you can do something like: global$DB;$this->instance->defaultregion = BLOCK_POS_LEFT;$this->instance->defaultweight = 0;$DB->update_record('block_instances',$this->instance);

Average of ratings: -