WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Can somebody please tell me how to use the bloginfo filter? (3 posts)

  1. walterego
    Member
    Posted 6 years ago #

    Hey guys,

    hoping some plugin guru can help me out. I'm just starting out writing plugins and right now I'm doing on that needs to replace the bloginfo('description') with another string. I just can't get around how to do this though.

    <?php
    function replace_description($bloginfo) {
    	$descriptionreplacement = "A string";
    	$bloginfo['description'] = $descriptionreplacement;
    	return $bloginfo;
    }
    ?>

    I get really strange results with this, when using this specific example I get a replaced title and description, but only the first letter of it is changed, it's an A now (Aust another WordPress weblog).

    When simply replace $bloginfo it does replace correctly, but everything is replaced (including title). Anybody can help?

    Walter

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    In a filter, all that usually matters is what you return. You don't have to set $bloginfo[] to anything.

    function your_function($info, $show) {
    // do something to $info
    return $info;
    }
    add_filter('bloginfo','your_function',10,2);

    The $show variable will be what they are trying to get. Like 'description'.

    So your code should be like this:

    <?php
    function replace_description($info, $show) {
      if ($show == 'description') {
        // do something to $info
      }
    return $info;
    }
    add_filter('bloginfo','replace_description',10,2);
    ?>

    Note that some of these bloginfo things have been replaced with options values, and so working there would be a better idea. The 'description' is one of these, it's an option called 'blogdescription'. You could more easily modify it like so:

    function replace_description($description) {
    // do something to $description
    return $description;
    }
    add_filter('option_blogdescription','replace_description');

    No need for any checking of any kind. It's faster too, since it's not called for every single bloginfo call.

  3. walterego
    Member
    Posted 6 years ago #

    Great! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic