You are here:  » Custom Meta on Search Results Pages


Custom Meta on Search Results Pages

Submitted by ChrisNBC on Mon, 2015-06-29 12:07 in

Hi David,

Hope all is going well. I wondered if you may be able to help me with a 'meta' problem...

In summary, I have created in the footer of the site that I'm working on a number of direct links the user can click to view subsets of products. The links were initially created by me copying the url result from searches that I did (the results of which I then refined using the drop down filters already on my search results page). I would like to add custom meta titles, descriptions and keywords to some of these pages.

My initial though was that I would use a modified version of the code in node 5677 to test the page URL and append the meta data if matched. However, since I could end up with quite a number of pages where I want to add custom meta this could get a bit messy!...I wondered if you could tell me if there are any existing solutions for this or if not if you could maybe suggest how feasible it would be to 'call' the custom meta from a new database table containing url to be matched, meta title, meta description and meta keywords. I have seen some old posts in the forum using database driven meta solutions but couldn't spot anything quite suitable for the above...

Thanks in advance.

Regards
Chris

Submitted by support on Mon, 2015-06-29 12:47

Hi Chris,

A URL match is definitely the easiest way to implement this - it can be done in code very easily, based on $_SERVER["REQUEST_URI"] which contains the full request from the leading "/" after the host name, for example, for the URL:

http://www.example.com/search.php?q=Foo&categoryFilter=Bar

...$_SERVER["REQUEST_URI"] would be:

/search.php?q=Foo&categoryFilter=Bar

So to implement custom title / meta description / meta keywords based on a match against this value, if you edit search.php and locate the call to header.php:

  require("html/header.php");

...and REPLACE this with:

  switch($_SERVER["REQUEST_URI"])
  {
    case "{URI 1}":
      $header["title"] = "Custom Title for URI 1";
      $header["meta"]["description"] = "Custom description for URI 1";
      $header["meta"]["keywords"] = "custom, keywords, for ,URI 1";
      break;
    case "{URI 2}":
      $header["title"] = "Custom Title for URI 2";
      $header["meta"]["description"] = "Custom description for URI 2";
      $header["meta"]["keywords"] = "custom, keywords, for ,URI 2";
      break;
  }
  require("html/header.php");

Replace {URI 1} etc. out with the exact $_SERVER["REQUEST_URI"] values to match and edit / add additional case: statements as required.

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2015-06-30 15:13

Hi David,

Thanks for your quick response and the above code...I will give that a go.

Best regards
Chris

Submitted by ChrisNBC on Tue, 2015-06-30 16:00

Hi David,

I realised when I looked at my existing code that I have already used the switch statement detailed in node 5602 for product, merchant and brand...

switch($product["products"][0]["name"])
{
case "Product 1":
$header["meta"]["description"] = "Product 1 Meta Description";
$header["meta"]["keywords"] = "product 1, meta, keywords";
break;
case "Product 2":
$header["meta"]["description"] = "Product 2 Meta Description";
$header["meta"]["keywords"] = "product 2, meta, keywords";
break;
default:
$header["meta"]["description"] = translate("Price search results for")." ".$q;
$header["meta"]["keywords"] = $q;
break;
}

I wondered if you could possibly suggest how I could include the url mod above so that the urls take priority but then the original code above is used as default if no urls are matched?

Thanks in advance.

Regards
Chris

Submitted by support on Tue, 2015-06-30 16:11

Hi Chris,

The section applied last will take priority - the modification from node 5602 occurs earlier in the page generation process, so if you apply the URL match change immediately before the call to require("html/header.php"); it will take absolutely priority...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Wed, 2015-07-01 16:11

Hi David,

Thanks for the clarification. I wondered if you could also tell me if there is a way to set the page title meta for merchant, category and brand so for example they can be in the format

custom text.brand.custom text
custom text.merchant.custom text
custom text.category.custom text

I tried adding the titles to the code provided in node 5602 but this had no effect and the title from header.php was used.

Thanks in advance.

Regards
Chris

Submitted by support on Wed, 2015-07-01 16:23

Hi Chris,

If you locate the default setting of the title to the query value at around line 399:

  $header["title"] = $q;

...and REPLACE with:

  switch($parts[0])
  {
    case "merchant":
      $header["title"] = "Merchant Before ".$parts[1]." Merchant After";
      break;
    case "category":
      $header["title"] = "Category Before ".$parts[1]." Category After";
      break;
    case "brand":
      $header["title"] = "Brand Before ".$parts[1]." Brand After";
      break;
    default:
      $header["title"] = $q;
      break;
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2015-07-02 10:29

Hi David,

Thanks for the 'title' mod ....it works perfectly.

Best regards
Chris