Running Developer

Enquire about this page

Posted on April 3rd, 2013

The idea was to have a link on a page that would allow a user to send a message to me about a specific page/post. So I needed to add to the bottom of each post a contact form with the page link, page title and the user’s comments. I already had a contact page called ‘contact-us’ and therefore thought just forwarding the user to it would be easier than making more another form. This meant having to transport variables about the current page the user was on to the contact page. Specifically the page title and page url so I would know exactly what the user was talking about. To transport variables between pages in PHP I would use GET/POST and Wordpress has methods to implement GET easily. It also uses a url rewrite system to make the links better to read and for SEO. To adjust the rewrite rules Wordpress has an API which you can adjust, look at this blog post

So if you still with me this is what I did: The link below each post needed to be as follows, It would first take the user to the contact page (/contact-us) and hand the two variables (/page_name/page_url) - ‘page_name’ and ‘page_url’ to the contact form. To get the link working I needed the page name and page url. I decided to use the page ID rather than send the entire url as a variable. I added the following to the bottom of the page/post templates:

<?php $post_data = get_post ( $post---> ID, ARRAY_A);
$id = $post_data ['ID' ];
$title = $post_data ['post_title' ];

echo '<a href="' . get_bloginfo( 'url' ) . '/contact/'. $title . '/'. $id . '">find out more about this page</a>';

FYI - get_bloginfo(‘url’) returns the base url of the website. Now I had the link, I needed to add a new rewrite rule to Wordpress to handle this type of link. I added the following to the functions.php file:

  <?php function my_rewrite_rules() {
                add_rewrite_rule (
                        'contact/([^/]*)/([^0-9]*)' ,
              'index.php?pagename=contact-us&mypage=$matches[1]&mypageid=$matches[2]' ,
  add_action ( 'init', 'my_rewrite_rules' );

The add_rewrite_rule in this case works as follows: ‘contact/([^/])/([^0-9])’ , the rule - my website can now handle the following link format‘a string’/‘a number’ ‘index.php?pagename=contact-us&mypage=$matches[1]&mypageid=$matches[2]’ , the rewrite - is what the rule will be transformed into - go to the ‘contact us’ page with the variable ‘mypage’ and ‘mypageid’ ‘top’ , the position - where to put the new rule above all the others After adding this if you run the site you will probably get a 404 page, you need to ‘flush the rules’. I just used a plugin called ‘Rewrite Rules Inspector‘ which had a nice button to ‘flush’ and showed all the Wordpress rules. Now the link below each post went to the contact page, but how would you get the variables? To get Wordpress to recognize the two custom variables you need to add a rewrite tag as follows in functions.php:

<?php function my_rewrite_tag() {
              add_rewrite_tag ( '%mypage%', '([^/]*)');
        add_rewrite_tag ( '%mypageid%', '([^0-9]*)');
add_action( 'init', 'my_rewrite_tag' );

I could now use get_query_var(‘mypage’) to get the variable ‘mypage’ which can only be a sting thanks to the regex validation, ‘([^/]*)’.
On the contact-us template page I added the following to get the values of the variables:

$mypage = $wp_query ->query_vars ['mypage'];
$mypageurl = get_permalink($wp_query ->query_vars ['mypageid']) ;

I used get_permalink to get the actual url of the page from the page id. Finally all I did was add some hidden inputs and an addition to the query part of the contact form like this:

<input type="hidden" value ="<?php echo $mypage; ?>" maxlength ="30" required name="contacts[]" />
<input type="hidden" value ="<?php echo $mypageurl ; ?> " maxlength="30" required name="contacts[]" />

<label> Query</ label>
<textarea maxlength="1000" rows="10" required name="contacts[]"><?php
   if(isset ($mypage)){
        echo 'Please tell me more about this page: ' . $mypage;

        echo 'message...';


Nothing major to explain, unless someone wants a post about the contact page? I just have some text to show the user that they are querying a page. So let me know if this helped you as it is my first big dev post.