11 thoughts on “Using the WordPress bloginfo filter to add HTML markup

    1. Assuming that you haven’t defined a class in your functions.php, you’d just declare normal functions and maybe use a global variable.

      Try this and let me know:

      $slug_in_body = false;
      
      public function slug_action_wp_head_finished() {
          global $slug_in_body;
          $slug_in_body = true;
      }
      add_action( 'wp_head', 'slug_action_wp_head_finished' ), PHP_INT_MAX );
      
      public function slug_action_wp_footer_started() {
          global $slug_in_body;
          $slug_in_body = false;
      }
      add_action( 'wp_footer', 'slug_action_wp_footer_started', 0 );
      
      public function slug_filter_bloginfo( $name, $show = null ) {
          global $slug_in_body;
          if ( 'name' == $show && $slug_in_body ) {
              return "$name";
          } else {
              return $name;
          }
      }
      add_filter( 'bloginfo', 'slug_filter_bloginfo' ), 10, 2 );
      

      Make sure to replace “slug” with whatever slug you’re using to identify your child theme.

  1. After removing some errors and some testings, the next code worked:

    //$twentyseventeen_in_body = "";
    //$twentyseventeen_in_body = false;
    
    function twentyseventeen_action_wp_head_finished() {
        global $twentyseventeen_in_body;
        $twentyseventeen_in_body = true;
    }
    add_action( 'wp_head', 'twentyseventeen_action_wp_head_finished', PHP_INT_MAX );
    
    function twentyseventeen_action_wp_footer_started() {
        global $twentyseventeen_in_body;
        $twentyseventeen_in_body = false;
    }
    //add_action( 'wp_footer', 'twentyseventeen_action_wp_footer_started', 0 );
    
    function twentyseventeen_filter_bloginfo( $name, $show = null ) {
        global $twentyseventeen_in_body;
        if ( 'name' == $show && $twentyseventeen_in_body ) {
    		$name = "Info" . "Psi" . ".md";
            return "$name";
        } else {
            return $name;
        }
    }
    add_filter( 'bloginfo', 'twentyseventeen_filter_bloginfo', 10, 2 );
    

    I commented the add_action( 'wp_footer', 'twentyseventeen_action_wp_footer_started', 0 ); and this nothing changed. It is needed?

    1. You probably don’t need to know when the body tag closes, unless your theme or plugins somehow use the bloginfo function afterwards. I just included the footer hook for completeness, in most cases you can get away without it I guess.

      You can format your code with standard html <pre> and <code> tags. I’ve edited your reply to add those.

      As a sidenote, I see that you use twentyseventeen as your slug. While this will work OK, keep in mind that this is the slug of your parent theme. Ideally you should have your own slug for your child theme. This is common practice. The idea here is to avoid any naming conflicts with other components, whether they are other plugins or themes or even your parent theme. If there was a twentyseventeen_filter_bloginfo function in the twentyseventeen theme, you’d be in trouble.

  2. Alex, thank you very much! Again, for completeness, this is the final code I have used in my child theme’s functions.php (I hope the tag will work):


    $twentyseventeen_child_in_body = false;

    function twentyseventeen_child_action_wp_head_finished() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = true;
    }
    add_action( 'wp_head', 'twentyseventeen_child_action_wp_head_finished', PHP_INT_MAX );

    function twentyseventeen_child_action_wp_footer_started() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = false;
    }
    add_action( 'wp_footer', 'twentyseventeen_child_action_wp_footer_started', 0 );

    function twentyseventeen_child_filter_bloginfo( $name, $show = null ) {
    global $twentyseventeen_child_in_body;
    if ( 'name' == $show && $twentyseventeen_child_in_body ) {
    $name = "<span class="info-style">Info</span>" . "<span class="psi-style">Psi</span>" . "<span class="md-style">.md</span>";
    return "$name";
    } else {
    return $name;
    }
    }
    add_filter( 'bloginfo', 'twentyseventeen_child_filter_bloginfo', 10, 2 );

  3. The <pre> tag don’t worked as I expected, probably it must be used together with the the <code> tag. Anyway, I solved my problem. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *