Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a new 'page template' and (this is probably not the right way to do this, but this is the way I have got it working), and created a new page using that template. Within the page template (PHP file) I have made, I have put lots of PHP which makes the page function, like it has a simple login an view information from a SQL table.

The problem is, I need to be able to send $_GET data to the page. I can send POST fine, but not GET, and I dont know what the URL is.

For example, I would like to have clean URLs (that is what I have set wordpress to)

i.e.:

domain.com/teaching-portal/mygetdata

The name of the page template is teaching-portal, and I would like to get mygetdata into PHP. But currently anything passed after teaching-portal results in a 404 page.

How can i achieve this?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Assuming you have a page with the slug "teaching-portal", this plugin adds a rewrite rule for that page that allows one part to be added after it in the URL, what you refer to as "mygetdata". I've called it "lesson".

<?php
/**
 * Plugin Name: Custom PHP app on teaching-portal page
 * Plugin URI: http://wordpress.stackexchange.com/questions/75991
 */

// Flush rewrite rules on plugin activation
register_activation_hook(__FILE__, 'wpse75991_activate');
function wpse75991_activate()
{
    wpse75991_add_rewrite_rule();
    flush_rewrite_rules();
}

// Flush rewrite rules on plugin deactivation
register_deactivation_hook(__FILE__, 'wpse75991_deactivate');
function wpse75991_deactivate()
{
    flush_rewrite_rules();
}

// Add a custom rewrite rule for the teaching portal section
add_action('init', 'wpse75991_add_rewrite_rule');
function wpse75991_add_rewrite_rule()
{
    $pagename = 'teaching-portal'; // Slug
    add_rewrite_rule(
        $pagename.'/?([^/]*+)',
        'index.php?pagename='.$pagename.'&lesson=$matches[1]',
        'top'
    );
}

// Register the custom query var so WP recognizes it
add_filter('query_vars', 'wpse75991_add_query_var');
function wpse75991_add_query_var($vars)
{
    $vars[] = 'lesson';
    return $vars;
}

With this plugin activated you should be able to access the lesson variable in your page template. Here's a quick example:

$lesson = get_query_var('lesson');

if (empty($lesson))
{
    // Show all lessons
}
else
{
    // Show a single lesson
    var_dump($lesson);
}

Also see the Rewrite API in the codex.

share|improve this answer
    
Wow, very amazing. That's really helpful, thank you so much. –  Chud37 Dec 13 '12 at 17:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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