Cennnn
Cennnn

Reputation: 45

How to set Metatitle for a user-entered javascript hashed URL via PHP

I want to set a specific metatitle to a javascript hashed url. I want to set it via PHP so that when google crawls it the metatitle is already there.

An example of a page: https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25

In this url /en/shop/ is the wordpress page, and #!/men+long+sleeve+shirts?q=P25 is set by javascript.

I have this code in the functions.php to try to set the title:

if (is_page(194) and (strpos($url, 'men+long+sleeve+shirts') !== false)) 
{$new_title = "long sleevetitle";}; 
return $new_title;

But how do I get 'men+long+sleeve+shirts' or '?q=P25' in the $url variable?

Is there a way to get the user-entered url in PHP?

Upvotes: -1

Views: 27

Answers (1)

Dako
Dako

Reputation: 51

You can use combination of strpos() and substr();

Like this:

$string = "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
var_dump(substr($string,strpos($string,'?'))); // output '?q=P25'

It will cut whole string and leave only the part from ? to end.

Explode method:

Or you can use alternate, explode method like this, which will ensure that it will target only last element with question mark.

$string= "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
$string2= explode('?',$string);
var_dump(end($string2)); // output 'q=P25'

Edit:Both methods would work with "#" also, just replace '?' with '#'.

Refs:

http://php.net/manual/bg/function.strpos.php

http://php.net/manual/bg/function.substr.php

http://php.net/manual/bg/function.end.php

http://php.net/manual/bg/function.explode.php

Upvotes: -1

Related Questions