Form action unfamiliar
Question
I am refactoring some code in the header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » Blog Archive <?php } ?> <?php wp_title(); ?></title>
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->
<?php wp_head(); ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
</head>
<body <?php body_class(); ?>>
<div id="wrap">
<div id="header">
<a href="http://threegreenbirds.org" class="hlink"><h1><?php bloginfo('name'); ?></h1><div id="tagline"><?php bloginfo('description'); ?></div>
</a>
<!--Main Navigation-->
<div id="mainNav">
<ul>
<li><a href="http://threegreenbirds.org">Home</a></li>
<?php /* EDIT THE MENU BY CHANGING ONLY THE NUMBERS AFTER INCLUDE*/ wp_list_pages('sort_column=menu_order&include=8,14,10,12,16,6&title_li='); ?>
</ul>
<div id="search">
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" class="searchbox" size="20" />
<input type="submit" id="searchsubmit" value="Search" class="searchbutton" />
</form>
</div>
</div>
<!--end main navigation-->
</div>
One of the requirements is for me to change this to a mobile responsive website which I started to in the file above with this piece of code:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
I also added the body_class function and so where I have a question is when I looked at the form action, I see this:
action="<?php bloginfo('url'); ?>/"
I have never seen anything like that before, but keep in mind I don't have 10+ years doing this. Typically speaking this is how I would develop the form action:
action="<?php esc_url(home_url('/')); ?>"
So my question being, should I change it or is there a legitimate best practice being implemented here that I am not familiar with? I understand that the best practice is to go with esc_url() function.
Solution
Generally, they will return the same URL, as bloginfo( 'url' )
will call home_url
internally with any arguments, and the default first argument ($path
) of home_url
is the empty string. However, bloginfo( 'url' )
will apply an additional filter to it, namely the home_url
filter.
Common pratice is to use home_url
, indeed with esc_url
(even though home_url()
returning an invalid URL will yield bigger problems anyway :-)). In WordPress core, bloginfo( 'url' )
is practically never used anymore. Instead, home_url( '/' )
is used.
So, summarizing: use esc_url( home_url( '/' ) )
.