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'm currently developing a plugin to display some custom data on my wordpress site.

For example I have a data named abc. If a visitor try to access localhost/wordpress/abc, it should display the data programatically from code. But if a valid post url is accessed like localhost/wordpress/existing-post it should still display the post.

I've currently looking into trapping 404 redirect using template_redirect action and redirect_canonical filter, but the solution doesn't feel right. Also I'm looking into using custom post type but not sure whether it's suitable.

I've tried by create a post for each data, and use the_content filter to display the detail of the data, but this method generate too many posts so I'm looking for a cleaner alternative.

The data I have is generated in the background over time. So it will grow to thousands or tens of thousands in the future.

Thank you.

EDIT:

I've adopted 404 Tools plugin by toscho. These is the essential part that I've modified and use

add_filter( '404_template', 'myplugin_display_data' );
public function myplugin_display_data( $template )
{
    status_header(200);
    new self;
    //For now forward everything to my own page for testing purpose
    return dirname(__FILE__) . '/myplugin-page.php';
}

Now I notice that the output will have error404 in part of the CSS class of the body, which still render the output like 404. How should I make it like a post or page without manually overriding variables $wp_query, e.g. doing $wp_query->is_page = true;

I'm looking for an elegant way to do this.

EDIT: I've added

    global $wp_query;
    $wp_query->parse_query("/");

After status_header, and now the page is displayed the way I wanted. Thanks.

share|improve this question
2  
What does 'data' mean? Where is it stored? Is it code or content? –  Brian Fegter Mar 1 '12 at 5:31
2  
Why do you need a plugin for that? Isn't editing 404.php easier? –  Wordpressor Mar 1 '12 at 5:39
    
'Data' is content generated based on data is stored in the db. When the redirection reaches 404.php, the header of 404 is already sent. I wanted the "page" to appear as ordinary page which existed. –  faulty Mar 1 '12 at 7:23
    
so this doesn't really have anything to do with the 404 page. You just want a url where the data is displayed? The data is not a WP post but something stored in its own non-wordpress-specific db tables? If so you could make a Page named 'abc' and a page template named 'page-abc.php', and in the page template do all work needed to query the data tables and display the data. –  Natebot Mar 1 '12 at 10:09
    
@Natebot You're right, it's actually has nothing to do with 404. But the string 'abc' is unique to each data, that's why I didn't want to resort to creating page/post for each data. 'abc' is actually the query string or name of the data, so I can retrieve it from the DB –  faulty Mar 2 '12 at 13:58
add comment

1 Answer 1

up vote 1 down vote accepted

You may take a look at my 404 Tools plugin. I run a filter on '404_template' there. The filter function is getting the template URL as an argument, and it is still possible to send HTTP headers.

You could hook into this filter too, prepare some data you may need to print out later, change the template (eg.: to single.php) and add a do_action( 'print_my_custom_data' ); to your single.php.

share|improve this answer
    
Thanks, I'll give it a try later this weekend. –  faulty Mar 2 '12 at 13:59
    
It's close, but I would prefer to intercept even before WP decided this is 404. Which filters comes before 404_template? –  faulty Mar 5 '12 at 12:21
1  
@faulty A lot! :) Try my plugin Plugin All Actions List to find a better one. –  toscho Mar 5 '12 at 16:37
    
Nice one, thanks! –  faulty Mar 5 '12 at 23:03
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.