WordPress: Generating pages from custom db table - mysql

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?

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.

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:
How to integrate custom database table in Wordpress and using Wordpress functions
WordPress 3.0 and Custom Post Types
When to (not) use a Custom Taxonomy

Related

Using a database to store and get html pages for website

I've got a lack of understanding at the moment. I'm developing a website with many articles and instead of creating a .html page for every article, I thought about storing the text into a database and get it from there (somehow) again.
I'm totally unsure if it is the common way to store text in a database. How do all of the "big" websites handle the mass of articles they publish? They won't create single pages neither but instead using a database, I guess.
But how can I achieve this? How can I store whole html files with divs and jquery and stuff into a database and get them when clicking on a link? Might XML be a keyword?
First of all, you need to clearly understand how things should work.
Clearly the approach of creating a page per article cannot work for multiple reasons:
if you have a huge number of articles you'll need to have a huge number of pages
if you need to change something small in design, you'll need to make that change for every single stored article
What you need to do is to create a more generic page, which has all the common stuff for all articles in it (a place for title, a place for content). The articles themselves can be stored in a database. When opening a page for a specific article, your application should place the title and content in the right place in that page.
This approach is universal _ it will work for any number of articles.
The keywords you are looking for are : Dynamic, Content Management.
In order to achieve this, you should learn a scripting language, PHP for example.
You will find a lot of tutorials to get started and how to make your website a bit more dynamic.
But you were right about the database part, most blogging systems and other content providers use databases to store all of this in data tables. PHP (and some other languages) would allow you to interface the database and the content you provide to your users.
You should look into using a web development framework like ruby on rails. Rails has templating that essentially let's you define variables inside of your html (e.g. "text of article").
As for storing the text of the article, the way I do things like that is to store them in a file on my server and then fetch that file using AJAX and then insert into an html file.
Most sites accomplish this by having templates, in which the common-to-every-page html is stored in a file. Page-specific data (article text, etc.) is stored in the database and "inserted" into the relevant parts of the template before returning to the client.
download word press and check how it work! it will help you
http://wordpress.org/download/

Transfer All Custom Field Data From Custom Post Type to User Accounts - Wordpress

Current Situation
I currently have a custom post type. This custom post type has numerous custom fields. There are many posts of this type, all with the custom fields filled out.
Currently, there is ONE POST of this custom post type that corresponds to each author on the blog. No author has more than one of these posts. The method in which it corresponds is simple, they are the author of their own post.
I used the Advanced Custom Fields plugin to set up and manage my custom fields, and I plan to continue doing that.
What I Want to Do
Instead of having these custom fields entered in each author's corresponding post, I would like them to be listed directly in their user meta. I can set up the custom fields easily in there using Advanced Custom Fields, even give them the same names and ids.
The the tricky part would be transferring all this data over. I COULD do it manually, but it would take ages. There is a lot of data to deal with.
Can anyone think of a way to automate this? It might be something as simple as renaming an entire table in the database. I have no idea, I don't know much about mysql databases.
WHY I want to do this, for those interested
Originally I just needed a way to list a bunch of people, and have a bunch of information about them listed on a page. It was not immediately clear to me that these people were all going to be USERS on the site, so at the time it made sense to just make a custom post type, and just enter each person into that with all their meta data.
But then they all became users on the site, and also authors with their own blog posts. And now I need to be able to query meta data about them FROM a blog post. But I can't figure out any way to do that, since their meta data isn't directly connected to their user account. It would appear that it would just be way easier (and more properly done) to tie all that data directly to their account.
Alternate Solution? Might be easier
There could be another way to make this work too. If someone can figure out a way to tie that custom post type post directly to its author, to the extent that you could somehow query it from anywhere as long as you knew which author you were querying, that would work and actually be better.
Possible ways to do that (these are just ideas) could include:
-Manually adding the custom post ID to a custom author meta field, so that you could get the ID of the corresponding post from the author's meta (not ideal, since I would have to enter this manually for every new author)
-That was actually my only idea. I thought of somehow using the username and hoping you can mess with that until you get the corresponding custom post... But there is no way to do that I think without tons of bugs.
Take a look to the excellent Posts 2 Posts plugin. In the official Wiki you can find a specific topic that seems to fit your needs: it's called Posts 2 Users. I hope this will be helpful for you.

HTML: How to design profile page that is used for different users

I want to make a website for my Android social networking app and need some assistance with a basic concept regarding user profile pages. So right now I have a native Android app where users have their own profile page displaying their profile stats, e.g. followers etc. and I would like to do something similar in HTML. My question is how can I design a generic profile.html page that displays the current user's username/stats and that can be used for different users? For example, see how Facebook are using the same profile page for each user but they replace the name/bio/stats fields with the data for the current user, how is that done - at the server side (by replacing the appropriate fields with the data for the current user), or at the client side (by loading them with a separate script after the page has loaded)?
Also, I am using a Tomcat servlet to deal with the requests if anyone is interested.
Thanks for any assistance and sorry if the question is silly :)
You would have to use PHP. Think of webpages as a mathmatical equation where HTML is 3+4=12 but PHP is 3+X=12. HTML is already defined, you can't change it. Where PHP you can code the entire page but get the bio or username from a database.
But you should be warned that this is no simple task, you would have to create a large mySQL database, possibly more to handle logging in and out and use the PHP to define what you are calling for form the mySQL database.
It is possible, but not in a day.

Preemptively getting pages with HTML5 offline manifest or just their data

Background
I have a (glorified) CRUD application that I'd like to enable HTML5 offline support with. The cache-manifest system looks simple yet powerful, but I'm curious about how I can allow users to access data while offline.
For example, suppose I have these pages for the entity "Case" (i.e. this is CRM case-management software):
http://myapplication.com/Case
http://myapplication.com/Case/{id}
http://myapplication.com/Case/Create
The first URI contains a paged listing of all cases, using the querystring parameters pageIndex and pageSize, e.g. /Case?pageIndex=2&pageSize=20.
The second URI is the template for editing individual cases, e.g. /Case/1 or /Case/56.
Finally, /Case/Create is the form used to create cases.
The Problem
I would like all three to be available offline.
/Case
The simple way would be to add /Case to the cache-manifest, however that would break paging (as the links wouldn't work).
I think I could instead add something like /Case/AllData which is an XML resource, which is cached and if offline then a script on /Case would use this XML data to populate the list and provide for pagination.
If I go for the latter, how can I have this XML data stored in the in-browser SQL database instead of as a cached resource? I think using the SQL database would be more resilient.
/Case/{id}
This is more complicated. There is the simple solution of manually adding /Case/1, /Case/2, /Case/3 etc... to /Case/1234, but there can be hundreds or even thousands of cases so this isn't very practical.
I think the system should provide access to the 30 most recent cases, for example. As above, how can I store this data in the database?
Also, how would this work? If I don't explicitly add /Case/34 to the manifest and the user clicks on to /Case/34 how can I get the browser to load a page that my JavaScript will populate based on the browser's SQL database data and not display the offline message?
/Case/Create
This one is more simple - as it's just an empty page and on the <form>'s submit action my script would detect if it's offline, and if it is offline then it would add it to the browser's SQL database. Does this sound okay?
Thanks!
I think you need to be looking at a LocalStorage database (though it does have some downsides), but there are other alternatives such as WebSQL and IndexedDB.
Also I don't think you should be using numeric Id's if you are allowing people to create as you will get Primary Key conflicts, it is probably best to use something like a GUID.
Another thing you need is the ability to push those new cases onto the server. there could be multiple...
Can they be edited? If they can I think you really need to be thinking about synchronization and conflict resolution hard very hard if that is the case.
Shameless self promotion, I have a project that is designed to handle these very issues, though it's not done, it's close. You can see it (with an ugly but very functional) demo at https://github.com/forbesmyester/SyncIt

How can I get my content from a Joomla blog into Blogger or Wordpress?

I have an old joomla site (v 1.0.11) that I would really like to move over to Blogger or Wordpress. The commenting on the site uses AkoComment which was an add on module. Basically, I want to transfer:
posts
comments
users
css
I haven't found a good tool out there to do this. What tools would be best to begin to work with this data? The joomla database is MySql. I would prefer to use blogger as you don't have to pay to host sites on it. That means though that I don't have direct access to the database. Blogger takes Atom feeds as import. The default Atom feed on Joomla will only list the last five posts though, not all, and won't include comments.
Any help here would be appreciated!
There's no easy way. All your user data in particular will be impossible to migrate due to the completely different ways the two systems would handle passwords and so on.
For the posts though, you probably can migrate them. You'll have to study the format of each platform's database tables and create a query or two to migrate data. For example, something like the below would work. Most of the variables listed here are not the actual ones, you'll need to replace them with whatever's appropriate. The 1 is whatever your ID is in the new system.
INSERT INTO wordpress_db.wp_posts ( `wp_author`, `wp_post`, `wp_date` )
SELECT 1, `content`, `posted` FROM joomla_db.jos_content
Write an HTTP bot to submit the data from your database or build a megafeed with all your Joombla blog entries. I personally enjoy coding this kind of scripts.