1

I can't get my head round this. The code below works fine at getting the variable "s" from the url and displaying the h1 but I've found it only works is the variable is set as certain characters. It works if the variable is set as either "l", "m" or "n". But it doesn't work in most cases including if the variable is set as "w", "wind", "nn" or even a random collection of letter like "sfda".

if ($_GET["s"]=='l'){?>
    <h1>Services for Companies</h1><?php
}

When it isn't working, Wordpress displays a "Sorry, that page can't be found" page.

2
  • 1
    What are the URLs where your method fails? Commented Jul 15, 2014 at 15:06
  • 1
    How should if ($_GET["s"]=='l') evaluate to true when s is set to something other than l ? Commented Jul 15, 2014 at 15:07

3 Answers 3

3

If you just want to check if a variable exists, you can use isset:

if( isset( $_GET['s'] ) ) {
    echo 'Yes, this variable exists.';
}

But in Wordpress you should use the get_query_var() function to access query parameters. This function has some basic security checks included.

if( get_query_var( 's' ) ) {
    echo 'Yes, this variable exists.';
}

Be aware that there are multiple GET variables, that are already used in Wordpress. For a full list see "WordPress Query Vars".

See also: How to pass extra variables in URL with Wordpress

Sign up to request clarification or add additional context in comments.

Comments

0

you can try :

GET and POST method

if(isset($_REQUEST['s'])){
echo 's ='.$_REQUEST['s'];
}

or only get method

if(isset($_GET['s'])){
echo 's='.$_GET['s'];
}

Comments

0

WordPress uses query variables to perform certain functions. In this case, s is the normal WordPress search query. I would recommend using a very specific url query, like headerquery or titlequery to avoid any conflicts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.