I am working in a Wordpress website and I need to pass the url of a page as a argument of a function. In this case I am using
bloginfo('url');
to retrieve the base url of the site.
So my code is
<?php
$portfolio_link = bloginfo('url');
if ( ! function_exists( 'ow_nav' ) ) {
function ow_nav( $nav_position ) {
?>
<nav id="ow-nav-<?php echo $nav_position; ?>" class="ow-nav clearfix ow-nav-<?php echo $nav_position; ?>">
<ul class="ow-nav-list">
<?php ow_nav_item( '#home', 'Home' ); ?>
<?php ow_nav_item( '#about', 'About' ); ?>
<?php ow_nav_item( '#criteria', 'Investment Criteria' ); ?>
<?php ow_nav_item( '#approach', 'Approach' ); ?>
<?php ow_nav_item( '#team', 'Team' ); ?>
<?php ow_nav_item( $portfolio_link, 'Portfolio' ); ?>
</ul>
</nav>
<?php
}
}
if ( ! function_exists( 'ow_nav_item' ) ) {
function ow_nav_item( $link, $text ) {
?>
<li>
<a href="<?php echo $link; ?>">
<span class="ow-txt"><?php echo $text; ?></span>
<span class="ow-dot"></span>
</a>
</li>
<?php
}
}
But when I do that, that variable gets leaked in my HTML, displaying the url in a random place in the DOM.
Any ideas?
bloginfo()
automatically echoes the returned value, as you have seen. To return the value and store it in a variable, you need to use get_bloginfo()
instead, e.g.:
$portfolio_link = get_bloginfo('url');
bloginfo
ref: https://developer.wordpress.org/reference/functions/bloginfo/get_bloginfo
ref: https://developer.wordpress.org/reference/functions/get_bloginfo/
Note: You will come across many Wordpress functions are like this, where one version will automatically display the value and a second version (usually preceded by get_
) return the value e.g. the_title
/ get_the_title