You just have to additionally call hook_block_info_alter() like in the following example. Don't know why. Seems the block needs to be registered first with hook_block_info(). And only after that you can alter its settings to enable and place it programmatically. Please feel free to add any better explanation to this answer in the comments.
The downside of this approach is, that this block ain't configurable through the back-end. I mean, indeed it is, but the new values always will always get overridden by the code below.
/**
* Implements hook_block_info().
*/
function mymodule_block_info() {
$blocks['mymodule_block'] = array(
'info' => t('A Super Duper Custom Block (ASDCB)'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_info_alter().
*/
function mymodule_block_info_alter(&$blocks, $theme, $code_blocks) {
if (!path_is_admin(current_path())) {
if (isset($blocks['mymodule']['mymodule_block'])) {
$blocks['mymodule']['mymodule_block']['status'] = 1;
$blocks['mymodule']['mymodule_block']['region'] = 'Header';
$blocks['mymodule']['mymodule_block']['weight'] = 2;
$blocks['mymodule']['mymodule_block']['visibility'] = BLOCK_VISIBILITY_LISTED;
$blocks['mymodule']['mymodule_block']['pages'] = '<front>';
}
}
}
/**
* Implements hook_block_view().
*/
function mymodule_block_view($delta = '') {
$block['subject'] = t('ASCDB');
$block['content'] = _mymodule_block_content();
return $block;
}
function _mymodule_block_content() {
return t('Hello World');
}