Function and usage of bloginfo() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T13:57:31+00:00

    The bloginfo() function in WordPress is a powerful tool used to display information about the current site. This function is commonly utilized in WordPress themes to retrieve and display various types of data related to the site. Here's a detailed explanation of its functionality and usage:

    Function:

    bloginfo( string $show = '' )
    

    Purpose:

    • Displays information about the current site.

    Description:

    • bloginfo() is used to output information about your WordPress site, such as the site's name, description, URL, stylesheet directory, and more.
    • It's a simple and efficient way to access and display various pieces of site data without having to delve into the WordPress database.
    • This function directly echoes the information to the page. Therefore, it's typically used within the HTML of WordPress theme files.

    Parameters:

    • $show (string, optional): Specifies the type of site information to display. The default value is an empty string (''), but there are various options you can use, which are detailed below.

    Common $show Values:

    1. 'name': Site title (set in Settings > General).
    2. 'description': Site tagline or description (set in Settings > General).
    3. 'wpurl': WordPress address (URL) (set in Settings > General).
    4. 'url': Site address (URL) (set in Settings > General).
    5. 'admin_email': Admin email (set in Settings > General).
    6. 'stylesheet_url': Primary stylesheet URL (usually style.css of the current theme).
    7. 'stylesheet_directory': Directory path of the current theme's stylesheet.
    8. 'template_directory': Template directory URL of the current theme.
    9. 'atom_url': Atom feed URL.
    10. 'rss2_url': RSS2 feed URL.

    Sample Usage:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="<?php bloginfo('charset'); ?>">
        <title><?php bloginfo('name'); ?> - <?php bloginfo('description'); ?></title>
        <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
    </head>
    <body>
        <header>
            <h1><?php bloginfo('name'); ?></h1>
            <p><?php bloginfo('description'); ?></p>
        </header>
        <!-- More HTML and PHP code -->
    </body>
    </html>
    

    In this example, bloginfo() is used to:

    • Set the character encoding with charset.
    • Dynamically generate the page's title using the site's name and description.
    • Link to the site's primary stylesheet.
    • Display the site's name and tagline in the header section.

    Note:

    • For a full list of possible $show values, it's recommended to refer to the WordPress Codex or Developer Resources.
    • If you need to retrieve information without echoing it, consider using get_bloginfo() instead.

Leave an answer