0

I have googled a lot, but so far haven't come close to a proper solution yet. I need help with passing parameters to the homepage of a WordPress website. Here's my setup / requirements:

  1. I use a page template (say, MCTemplate) that is set as the static front page in Settings -> Reading.

  2. I use 'Post name' permalink structure.

  3. I need to pass a single parameter (say, mcdata) to the static homepage (i.e., MCTemplate).

I have used the following code in functions.php, but when I pass the parameter to the homepage (e.g., http://www.example.com/?mcdata=helloword), it show a list of latest posts instead of static front page.

function mc_query_vars( $qvars ) {
    $qvars[] = 'mcdata';
    return $qvars;
}
add_filter( 'query_vars', 'mc_query_vars' );

Using front-page.php comes close to solving this, but in that case I cannot use a static front page. Are there any rewrite rules or anything that I should change to get it working with the above requirements? All help is appreciated.

1
  • I removed the comment and create an answer. Thank you.
    – gmazzap
    Commented Sep 10, 2013 at 14:20

2 Answers 2

3

front-page.php is a static front page (if you rename your current template front-page.php, and don't select nothing in Settings->Reading it works exactly like now.

However, if you want to use the page approach and you don't use mcdata for query, remove the filter you posted, and simply retrieve the mcdata value in your template using $_GET['mcdata'];.

2
  • The confusion was due to two queries: the WordPress loop query (WP_Query) and the URL query string. Thanks for the quick clarification.
    – Vasu Adiga
    Commented Sep 10, 2013 at 14:42
  • Knowing that the site front page was a 'static' page, and then finding this, was the solution for me too. Trying to set a 'query_vars' hook for the query parameters I wanted to look at disrupts existing code using is_front_page(), and prevented the static front page from displaying.
    – jrg
    Commented May 29, 2015 at 15:18
0

To pass parameter on the front page static I use the prefix "ch_" as follow:

http://www.example.com/?ch_mcdata=helloword

in this way it will work.

2
  • 1
    What is the significance of the ch_ prefix?
    – shshaw
    Commented Sep 25, 2014 at 18:01
  • nothing to do specifically with ch_ its because the query_var isn't registered with ch_ in front so WordPress doesn't try and process it. You could use any characters before it to get it to work.
    – Alex Older
    Commented Sep 22, 2016 at 14:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.