0

I'm hacking WordPress quite a lot for a current project. I'm storing data input by the user into two custom database tables (still inside the wp database though).

Along with the information I need to collect, I'm also saving the wp user id into the database. I've generated a URL I'd like each user to be able to visit, to see the results of their input. The structure of this URL is simple http://domainname/username/searchname

Now obviously, I can output this as a URL and it can be clicked, but WordPress obviously just spits out the 404 template instead.

I kind of need to emulate the same functionality that the post pages have in terms of being able to visit this link and have data spit out based on a template.

I realise that custom post types sound perfect for this kind of thing. But I need users to be able to submit data from the front end, I'm not sure this is possible with Custom Post Types?

2
  • why you are tell from hacking? this isn't hacking... this is editing, because you can see & edit the code ;) Commented Apr 22, 2013 at 15:16
  • Hacking as in a general term for messing around with some code to make it do stuff it wouldn't normally be doing lol :) Commented Apr 23, 2013 at 10:43

2 Answers 2

3

I think WP_Rewrite class is what you're looking for:

WP_Rewrite is WordPress' class for managing the rewrite rules that allow you to use Pretty Permalinks feature. It has several methods that generate the rewrite rules from values in the database. It is used internally when updating the rewrite rules, and also to find the URL of a specific post, Page, category archive, etc..

There are several examples in its documentation page. For a more hands-on tutorial check this blog article.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising! I will have a good look through this in a couple of days to see if it works and give the bounty :)
2
+50

From what I've read so far, using custom database tables should be reserved to situations where the default tools can't handle the task.

The benefit of the default tools is that you don't have to create custom functionality (and all the details that come with it) to search, manipulate and display the data.

You could have a CPT called User Feedback, configured as hierarchical, each parent post corresponds to one user and the child posts would be the user input data.
Or it could be non-hierarchical and a custom taxonomy would make the bridge between posts and users.

After submission on front-end, you simply use wp_insert_post to add the info to the database as the post type (use title and content as holding fields if needed) and its associated meta data (other fields).

If you set the CPT supports argument to false, it won't show anything in the editing screen but the Publish meta box. And all the submitted information stored as meta data can be displayed by custom meta boxes.

After having this setup, if you need to fine tune the URLs, it's time to use WP_Rewrite.

That's how, in general terms1, JetPack manages the feedback from its Contact Form.

1 Checking the code, there's actually some interesting techniques used and worth replicating.


Related posts:

3 Comments

Hmm, interesting! This also sounds good. More reading before I decide which route to go! Thanks :)
My concern is, if I were to make each user search a post, inside those searches will be an unknown number of results, which need to be output on the results page in their own div, with extra information such as keyword matches and some stats about each search result. I imagine I could put them into a custom field as an array and then pull them out on the results page?
For "searchability", it's better not to use an array as post meta. For the fields that won't be searched, they can be all inside a single post meta.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.