Handling custom table data in WordPress - php

I'm making a plugin for WordPress and it requires some custom tables of mine (or the data in them, to be specific). These tables are simple. They only contain an id and a varchar field. Do not mention meta fields. I am using custom tables for a reason. The problem is, I need to allow the user to edit the data in these tables. To insert, update and delete rows in the tables. How do I handle this in WordPress?
I can make my own custom pages and create a whole system for editing, inserting and updating them but that's a workaround since none of that would be native to WordPress.
The only thing I came up with is making them into custom post types and having WordPress let me use its native adding, editing and deleting menu (the same one used in posts, pages, etc.). Though then new problems would arise. I need to connect the post ids to the rows in the custom tables and be very careful when editing, updating and deleting to make sure that both the posts and my custom table data remain consistent with each other. And that's a whole new layer of complexity.
So I was wondering if there's an elegant way to handle these sorts of simple tables. If there isn't I'll find my own workaround.
EDIT:
The plugin I'm making is supposed to turn your WordPress site into a kind of an ad posting site. And I need the custom tables to provide details about the ads.

refer this codex Wp_list_table. you can play with custom table data. for more you can go through an example plugin.

Related

Implement Custom Search Plugin for Joomla

I would like to implement a Custom Search Plugin for Joomla 3.5.1. I am new to Joomla and still busy getting my feet together.
I recently implemented a plugin that adds custom/extra fields (5 fields, free text inputs) to an article in Joomla 3.5.1. I followed their tutorial on how to get this right and so far so good. This plugin saves the extra/custom fields in the #__user_profiles table as per their tutorial. I could obviously save it to a different table but since I am learning, I kept it simple. This plugin works perfectly and I can capture the extra fields and saves successfully and the data is saved along with the article id being edited/created.
My next task is to create another site/front-end "search" (or smart search) plugin. This plugin must group the 4 field's values and provide them to the user in the front-end as selects/dropdowns.
Is there someone that can give me a lead on how I can archive this. Must I implement it as a search or smart search plugin? Is it even possible to archive what I am trying to archive?
This search plugin will only appear on one page but that I know I can config correctly once I've a plugin. Once user searches, the system must post back the selected options and I will search the "#__user_profiles" for any matching profile_key vs profile_value and collect all user_id (article_id) and get these articles from contents table and return this list of articles to the front-end as search results. Anyone can help me into the right direction?
I tried their tutorial of creating a search plugin but I seem lost, mostly I don't know how to dynamically provide my selects/dropdowns with that info saved in the #__user_profiles table.
Please assist in anyway possible...
I don't think that this might be done without modifying core 'search' task. My advise is to advise search view and include extra filtering, then write own 'search' task which allow you to read this data and provide additional results.
By writing just search plugin you can only extend search results by data which is stored outside default Joomla! tables but nothing more.

Wordpress page to populate WP database

I have a custom page on my WP instance which I want to interact with my database instance, ideally the standard table WP uses for posts.
This page pages retrieves an online stream from a set of social network channels, and I do want to persist this stream into the database so to form a sort of "archive".
So I have two questions:
a) Would it better creating a separate table, or is it more convenient adding everything in the wp-post table (or whatever it is called), classifying the item as social or something else
b) How can I interact with my WP database using WP Api so to not reinvent the wheel?
Thanks
Normally, we should only create our own tables if WordPress features and organisation are not enough to deal with our data.
Nowadays, Custom Post Types (wp_posts db table) and Custom Fields (wp_postmeta db table) are used in a variety of plugins (from Advanced Custom Fields to WooCommerce).
Then we use, among others, WP_Query to retrieve data and wp_update_post() and update_post_meta() to insert data.
In any case, data validation and security checks are always important.

Wordpress Custom Table or Custom Post Types?

I am using wordpress to make a site which has job posts, and I want to know the best way to go about storing company details, as each job post must be linked to a company.
Normally I would have just set up a companies table in my database, and put a company ID within each job post, then joined to get company details.
However I have often come across the idea that it's not always the best option in Wordpress development to create new tables (such as here). Instead I would be better using custom post types.
But how would this work to store company details? Would it be right to create a custom post type for companies, and then add a new post for each company? And then use custom fields to store data about each?
Or would I be best off creating new database tables? What is better and why?
I suppose it is the best to use custom post types with post meta in conjuction with meta boxes. This way, you will be able to manage in ease your companies, and yet, you will be able to operate custom queries on companies via the WP_Query class.
Currently I am building a business directory for a my client, so I decide to use the custom post types because of flexibility. Also I made a custom script for managing company information in conjuction with post meta.
Take a look in the screen shot to undestraint what I mean :
Note that whenever I like to add new data to companies, I am free to just add some extra meta boxes, and then save the extra data as meta data for the post. This way is matter of minutes to extend a standart data entity.
What you need:
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Function_Reference/add_meta_box
http://codex.wordpress.org/Function_Reference/register_taxonomy (optionaly)
http://codex.wordpress.org/Function_Reference/get_post_meta
http://codex.wordpress.org/Function_Reference/add_post_meta
I hope you this will help you :)
Create an Custom post type
add the custom fields to that custom post type for different fields you need,
There is no need of creating table manually,
http://wordpress.org/plugins/custom-post-type-ui/
http://wordpress.org/plugins/advanced-custom-fields/
and you can apply the sorting on some custom field you added to the post type,

Creating WP custom post types with custom fields from database

I'm working on a Wordpress website that uses an external system to sell tickets. This ticket system provides a WordPress plugin that automatically creates and updates two new WordPress database tables: events and shows. That's great, but that's all this plugin does.
I have used the Database Browser plugin to test if the database tables created by the ticket system plugin were there and that was the case. I can see all the data there too. So far so good.
I now need to create custom post types (events and shows) with custom fields using the data from these database tables. I've searched, but I can't seem to find out how to create custom post types that pull their data from existing database tables.
Maybe I'm looking at this all wrong. Any help to point me in the right direction?
To create custom post refer this http://codex.wordpress.org/Post_Types#Custom_Post_Types
And use this plugin to create custom field http://wordpress.org/plugins/advanced-custom-fields/
i'm assuming you realise that you'll need to write code to query db...there are no plugins to query a db (that i know of).
it also sounds like you are going to duplicate data ie pull data from custom table events and shows, then add to this data to custom fields. i'm not sure this is the best approach.
also, if data in these custom tables change, will it be reflected in your custom fields data?
have i assumed too much? let me know.
To query db, use the wp class wpdb. I havent really used it but it has enough features to do what you want.
acf plugin has already been mentioned. custom post type ui plugin is easy for creating custom post types with no code. it even prints out the actual code for you so you can copy it, place in functions.php, then disable plugin, nice.

Moving data from custom blog to Wordpress?

Ok so i have this custom blog that i now need to move over to wordpress. I tried using the import features in wordpress but that didnt work as planned. The custom blog is written in php and has a userscomments table, authors table, and posts table. Is there an easy way to move all these posts over to wordpress and preserve all the comments per post.
If the custom blog doesn't have an export function that will dump its data into a format WordPress can read, then it may be that you'll have to roll your own. There are 2 approaches:
Do a database-only import, using code or SQL tools along with information from the WordPress Codex entry on the WP database. This will probably be fairly simple from a coding standpoint, but you have to make sure you get it right.
Use the WordPress API to create the posts based on the output from the old system. This will involve a bit more learning, but it's the Right Way™ to do it, and means that you learn the WordPress API rather than the database structure.
If I were doing it myself, I'd choose option #2.

Categories