1

I have block with social sharing buttons. Need add 'og' and 'twitter' meta tags on page where block exists. I know that can add it throw drupal_add_html_head() inside block content render, but if block is cached meta tags not appear. How do this?

1

Instead of adding php code in the block you could add the code in a preprocess function for the block.

/**
 * Implements template_preprocess_block().
 */
function MYTHEME_preprocess_block(&$variables) {
  if ($variables['block']->bid == 'MYBLOCKID') {
    $og_title = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'og:title', 
        'content' => "My example title",
      ),
    );
    drupal_add_html_head($og_title, 'MYMODULE_og_title');
  }
}
  • Thanks! It works :) But, better use delta param. – FriOne Jul 31 '14 at 12:41
  • @FriOne could you update my answer or post the suggested change as a comment so I can update? – Pontus Nilsson Jul 31 '14 at 13:23
  • I edited your answer. Also one question, I thought that preprocess is not called too when cached, why drupal not cache after preprocess? – FriOne Jul 31 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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