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 plugin that creates a table in the database and allows users to add an Item to the table. I would like each item to also create a page for its self so that when the items are listed I can have each one link to there individual pages. Anyone know the proper way of doing this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can use wp_insert_post to insert a physical page, though I would suggest creating one page for the plugin and then using a rewrite rule to give all of your items a URL with that single page as the base.

EDIT

quick rewrite example with added query var:

function wpa55546_rewrites(){
    $page_slug = 'your-page';

    // urls will be in the form
    // /your-page/42/

    add_rewrite_rule(
        $page_slug . '/([0-9]+)/?$',
        'index.php?pagename=' . $page_slug . '&my_id=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpa55546_rewrites' );


function wpa55546_query_vars( $query_vars ){
    $query_vars[] = 'my_id';
    return $query_vars;
}
add_filter( 'query_vars', 'wpa55546_query_vars' );

then in the template you'll have access to the value of your query var:

echo get_query_var( 'my_id' );

you'll also have to flush rewrite rules at the appropriate time.

share|improve this answer
    
could you give or link an example? Not sure what you mean about a "rewrite rule". You mean I create a page, then call the tables I want displayed on that page? –  Kirill Fuchs Jun 17 '12 at 18:37
    
@KirillFuchs - see edit –  Milo Jun 17 '12 at 19:06

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.