Back to Top

Get incoming Search Keywords to your Website/Blog

Previous Post:

Get incoming Search Keywords to your Website/Blog

Nowadays, with the help of professional tools like Google Analytics, you can easily track all the keywords that bring visitors to your WordPress blog. However, you will need more than that to actually benefit from those keywords.

Using a simple PHP snippet you can easily extract all search terms people use to find your website, and then use those terms to display some fancy stuff to them on their landing pages.

Let’s start by firing up your theme’s functions.php file and put in the snippet below:

function bwp_get_search_keywords($url = '')
	// Get the referrer
	$referrer = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
	$referrer = (!empty($url)) ? $url : $referrer;
	if (empty($referrer))
		return false;

	// Parse the referrer URL
	$parsed_url = parse_url($referrer);
	if (empty($parsed_url['host']))
		return false;
	$host = $parsed_url['host'];
	$query_str = (!empty($parsed_url['query'])) ? $parsed_url['query'] : '';
	$query_str = (empty($query_str) && !empty($parsed_url['fragment'])) ? $parsed_url['fragment'] : $query_str;
	if (empty($query_str))
		return false;

	// Parse the query string into a query array
	parse_str($query_str, $query);

	// Check some major search engines to get the correct query var
	$search_engines = array(
		'q' => 'alltheweb|aol|ask|ask|bing|google',
		'p' => 'yahoo',
		'wd' => 'baidu'
	foreach ($search_engines as $query_var => $se)
		$se = trim($se);
		preg_match('/(' . $se . ')\./', $host, $matches);
		if (!empty($matches[1]) && !empty($query[$query_var]))
			return $query[$query_var];
	return false;

Now in a global theme file (i.e. a file that is included on every page), for example header.php, you might have this:

<div id="content">
	$keywords = bwp_get_search_keywords();
	// do some fancy stuff here, for example:
	if ($keywords)
		echo sprintf(__('You have searched for %s.'), $keywords);

To test a specific referrer URL you can pass a $url variable to bwp_get_search_keywords(), for example this:

echo bwp_get_search_keywords('');

will give you this:

By default this function supports a few search engines, but you can add more if you want. For example, to add Yandex (a Russian search engine) you must first check the query var that it uses, which is ‘text’, not ‘p’ or ‘q’ (try searching for something like ‘wordpress’ and you will get this URL:

After you have determined the query var, simply append it to the $search_engines array appropriately, like so:

$search_engines = array(
		'q' => 'alltheweb|aol|ask|ask|bing|google',
		'p' => 'yahoo',
		'wd' => 'baidu',
		'text' => 'yandex'

If there are more than one search engines that use the same query var, split their domain names using the vertical bar ( | ) characters (just like what I have done with the ‘q’ query var).

Notes: If the fancy stuff you are going to do involve the use of WordPress database (for example you get some related posts based on the search terms your visitors use), please remember to escape all the terms to avoid malicious queries.

Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS
 Sponsor   Themes by Elegant Themes - Unlimited access to all themes for $39!

34 Opinions for Get incoming Search Keywords to your Website/Blog

  1. User's Gravatar
    King July 4, 2013 at 11:22 am – Permalink

    Attractive section of content. I just stumbled upon your site and in accession capital to assert that I get actually
    enjoyed account your blog posts. Anyway I will be subscribing
    to your feeds and even I achievement you access consistently

  2. User's Gravatar
    Iulian January 3, 2014 at 9:15 pm – Permalink

    Nice solution, but is till working after recent Google updates?

  3. User's Gravatar
    Reynaldo April 2, 2014 at 11:28 pm – Permalink

    Appreciating the dedication you put into your blog and detailed information you present.
    It’s nice to come across a blog every once in a while that isn’t the same unwanted
    rehashed material. Excellent read! I’ve bookmarked your site and I’m
    adding your RSS feeds to my Google account.

  4. User's Gravatar
    home improvements ideas May 21, 2014 at 1:12 pm – Permalink

    I am sure this paragraph has touched all the internet visitors,
    its really really pleasant post on building up new weblog.

  5. User's Gravatar
    ben August 4, 2014 at 8:36 pm – Permalink

    yes, most of them are pretty simple… but there are different patterns for different search engines

  6. User's Gravatar
    mann August 30, 2014 at 12:37 am – Permalink

    I want to know, is there any plugin exist which show incoming search terms for a post, like tags at the end of the post ?


  7. User's Gravatar
    sachin krishna January 9, 2015 at 10:42 pm – Permalink

    How can i detect the search terms country wise and store it in a database ?

  8. User's Gravatar
    7 Speed Electric bike January 19, 2015 at 3:48 am – Permalink

    Leading 10 Electric Bikes

  9. User's Gravatar
    izlasci split January 27, 2015 at 11:11 pm – Permalink

    It’s in fact very difficult in this full of activity life to listen news on Television, therefore I only use web for that purpose, and obtain the newest news.

  10. User's Gravatar
    smarty February 20, 2015 at 5:27 am – Permalink

    hi i need the same script for php . how i can convert it for my php site?

  11. User's Gravatar
    Free Documentary Online June 21, 2015 at 2:49 pm – Permalink

    It would be awesome if someone can take the code from here and make an WordPress plugin because some of us are not good with coding. I will try to install this in my WordPress and see whether it still works after google changed its search engine behavior to hide keywords.

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: