Wordpress create custom pages with permalinks from custom table data - php

I have 1 custom table (not wp core table) where are specific products imported. I've set up custom wp page where these products are listed, custom filter created etc. In this step so far everything is OK - products are listed and filtering works, pagination works...
But what I want to extend further now - open each product page (like /product-listing-page/product-name-1) when clicking on product names in product list page... How can be this achieved "out of wp_posts" table?
Why I'm not using wp_posts table: web site have static pages and dynamic posts as news and product table is truncated and re-imported once in a day every night! So, can not really use wp_posts page when product list is dynamic and totally truncated before each import...
Any ideas? Maybe anybody already did such stuff?;-)

I don't know if you developed a plugin to handle all the custom table and code you created, but if you didn't, I strongly encourage you to do so.
Because by making your code as a plugin, you have the oportunity to use all the composants of wordpress, like the rewrite rules system, which, I think, could help you to achieve your goal.
I created a plugin some times ago for a website I have the charge and I needed a custom page to be displayed within the website (worpdress) url system, which is what you want to do if I correctly understand your question.
Unfortunately the plugin has evolved since and I didn't keep a backup of that code, but if I remember correctly how I did it back when, here is the rough approach I followed:
I create a plugin which handle :
custom db table(s)
custom php objects
...
AND (this is the intersting part) add rewrite rule to wordpress rewrite rules system as follow:
set rewrite tag (init action) (if needed)
add rewrite rule (init action)
intercept url and parse it to get the params i need in my custom
page (parse_request action)
The following functions may help you with that :
add_rewrite_tag
add_rewrite_rule
And you need to hook in :
init
parse_request
Hope it helps you start.

the solution was quite quick and easy! For my needs it was enough just to register global query var:
function register_new_query_var($vars) {
$vars[] = 'product_id';
return $vars;
add_filter('query_vars', 'register_new_query_var');
and then I changed theme template for page product-listing-page to work for product listing and individual products with using
if(!get_query_var('product_id')). So, if query empty - do listing stuff, if not empty - do individual product stuff!
For me this is quite enough and I do not need even seo friendly urls (because product list changes every day, so - every day will be new products, links, and there will be to much 404 errors)...
and I can use http://example.com/product-listing-page?product_id=1 and page displays with date which is from custom table! ;-)

Related

PHP add new product function, create new subfolder to the product

I have an e-commerce website, and in the admin menu, I want to create a function so the authorized users can add new products to the site. I have the form page and the PHP script that uploads the data to the MYSQL Database, but thats it.
If I want to see the data of the product, I have created a single product.php file which gets the id using the $_GET['id'] method, and a PHP script will show you all the information about the product with the selected id.
Now it would work perferctly for me, but since this is a webshop, the SEO ranking is a essential, but you can't get ranking to a single page (I mean it won't appear in the search engines if you search for a specific product which avalaible in my webshop). So I was wondering what would be the best way to add new products to the website and increase the SEO ranking.
My idea is to create dynamically a subfolder for all the added products, and put the connected files in that folder, so every products would have a unique folder, with a unique index.php page so they could get SEO ranking.
I need your opinions about this, because I have read a bunch of articles about this topic, but I haven't found an acceptable answer so far. So would my idea be acceptable, or do you guys know any better way to approach this situation?

Creating a relationship between two custom types in WordPress

This is perhaps a very basic Wordpress development question. I am aware about creating custom types in Wordpress. So I created a Product type and a Version type and I would like to be able to keep a one-to-many relationship between the two i.e. for every Product, I would like to be able to associate one or more Version items from the admin editor. What is the way to do this? I am not in favor of using plugins, so I was thinking, I would use some kind of a custom field array of IDs, to connect the two. Am I on the right way? If I have to use a plugin, which one should it be?
The easy way to do this would be to add a meta box on your product admin page. That way you can create a drop down list on your product page with your versions. You can get more on adding meta boxes using add_meta_box here.
You add all required functions to your themes' functions.php, but to summarise you will need the following wp functions amongst others.
add_action
add_meta_box
update_post_meta
Looking up these functions in the Wordpress Codex (using google) will get you on your way. If you get stuck, post some code of what you have tried and take it from there. There are a number of tutorials/examples around. If you can do enough to create custom types this should not take you long.

list all products and link each category grid with category box in opencart

I want to load all categories grids on one page and want all products to be available on scroll down. currently I need to click on each category and the page refreshes for each category. This is the site I am working on(ahmad.esy.es). I want this type of functionality upon clicking the category(http://www.just-eat.co.uk/restaurants-hertsplaice-en11/menu#2254). kindly suggest me what changes should I make in view files (product.tpl, category.tpl) or controller files(product.php, category.php). how can I load all the grids of categories in one page
I didn't look at the site you have posted above - if you cannot describe your issue by words or by submitting a code or image it should be improved.
From what I understand you just want an infinite scroll to display all the products you have in your eshop. It is not mentioned whether you want to preserve the default functionality (category tree with products attached to certain categories) or whether you want only this one listing - basically it does not matter - so I will describe in short how to add such new functionality.
The model
you do not need to touch any model - you can reuse the method ModelCatalogProduct::getProducts()
this will give you the possibility to sort or filter the products except you will have to omit the filter_category_id to load all the products
The controller
feel free to create a new controller (e.g. ControllerProductInfinite) or just to reuse one of the already existing - either ControllerProductCategory or ControllerProductProduct
all you need is basically the same as in ControllerProductCategory::index() except adding filter_category_id into a array passed when calling ModelCatalogProduct::getProducts() and a new template for rendering
The template
create a new template that will meet your expectations and infinite scroll requirements
stick to any infinite scroll plugin/implementation you like (may Google be with you)

Calling active wp_posts row

Is it possible to echo a value from the active post in wordpress?
Example: If I make a row called: post_numberofreads and I want to echo this value for each post, by inserting a line of code in the single post template page (single.php) that calls any given posts 'post_numberofreads'. How would I go about doing this?
Please notice that this is an example. The important part is that I want to echo something from the active row - not just get the number of reads.
Thanks for your time.
While the ideal solution is to create a wordpress plugin with your custom functionalities but it require good understanding of wordpress and plugin development.
The other solution more can be to inject your custom code in the wordpress core manually. I is comparatbiliy easy and often unsecure and get broke frequently.
For second option you can:
A) Create required custom columns in wp-posts in wordpress database using phpmyadmin or your favourite tool.
B) Inject your code into the relevant files. Generally these files are:
wp-content/themes/your_theme/single.php #For blog post
wp-content/themes/your_theme/page.php #For pages
C) Then you can using built in wordpress functions like get_the_ID to get current post ID. You can also other wordpress functions to communicate with wordpress database or write your own functions.

Defining dynamic pages in wordpress plugin

I am creating my first wordpress plugin. In it, the user will have the option to add new cities and view events on those cities.
My client requirement is that the URL must be like this
SITE_NAME/cities/NY
or
SITE_NAME/cities/Califonia
What is decided is that i will create a folder cities and If user tries to create a new city i will create a file in that folder with that city, Further more I will add the entry into the database as well.I will insert PHP code into the file as well.
Being new to WP plugins. Is my approach right (for creating files)? Is there any other way?
Your approach would work if you WEREN'T using Wordpress, but I would absolutely not advise doing what you're suggesting within the Wordpress Framework.
Rather than trying to reinvent the wheel, just about everything you want can be achieved with existing Plugins. I would absolutely suggest taking this route, especially if you don't have much experience with programming Wordpress Plugins.
Step 1:Install Wordpress
Self-explanatory. Can't do anything without this.
Step 2:Install Types
Installing the Types Plugin will give you a nice interface for registering your own Custom Post Types and many other features for extending Wordpress' core capabilities.
Step 3:Register your Custom Post Type
This is where things start to get complicated. Using Types, Register a Custom Post Type called Event. The reason why you want to have each event as its own Post is because each Event is unique. Cities are meant to encapsulate and define groups of events, which brings us to our next step:
Step 4:Register your Custom Taxonomy
Think of a Taxonomy as a way to classify things. Wordpress comes with two default Taxonomies: Categories (hierarchical) and Tags (non-hierarchical). In your case, you will want to define for your new Event Post Type a non-hierarchical Taxonomy called "Cities".
Step 5:Register your Custom Fields
Custom Fields are the easy part. What defines your event? Maybe some fields that define the start and end times of that particular event? A checkbox denoting free refreshments? The sky's the limit. What defining characteristics would all Events have that makes it an Event? Add those in the form of Custom Fields. These will show up on your Event Editor in the form of Meta Boxes. If you don't see the Meta Boxes on the Edit Page for a particular event, be sure to enable it by clicking Screen Options in the upper-right-hand corner of your screen and ticking the checkbox where your fields would be located.
Step 6:Configure your Permalinks
Much of this can be done either through Types itself (when configuring your Custom Taxonomy), or through .htaccess rewrites, or perhaps even through the Wordpress Permalinks Settings (though, it's fairly limited). My suggestion is to tweak your Custom Taxonomy and Permalink Settings first before messing with .htaccess.
And that's it! Hopefully this should be enough to get you started on everything you need.
maiorano84 wrote a fairly comprehensive guide to setting up the stuff you need, Rather than relying on plugins though, I prefer to show you how to write a plugin to register the custom post type and taxonomy. To that effect, I wrote a little plugin that should do everything you need and it has plenty of comments and links to the docs so that you can understand the Why of things.
Code
https://github.com/fyaconiello/WP_Cities_Events
This plugin does several things
Creates a custom post type Event
Creates a custom taxonomy City
Adds custom metaboxes to Event
Adds City taxonomy to Event
This plugin does not require any additional plugins to be installed, it is dependency free and only uses WP core.
URLS
As far as getting the correct URL Structure, I would suggest you read this thoroughly: http://codex.wordpress.org/Using_Permalinks.
I do not understand the structure you want
CITY is a single term w/i the taxonomy *cities*
EVENT is the post single
SITE_URL/cities/CITY would yield a page of all EVENT posts in that CITY
you need a url like: SITE_URL/cities/CITY/EVENT to read a specific event in a specific city
EDIT on how to urls:
In your Settings -> Permalinks administration panel select: "Post name" and save.
Then, go to your Ce Events -> Cities admin screen.
hover over one of your terms (in my case new york city) and click view.
it should open up that term(city)'s list view and the url structure looks like so: http://wp.local/city/new-york-city/
if you need city to read cities, modify line 102 of the main plugin file i shared with you:
'rewrite' => array('slug' => 'city'),
EDIT 2
Why not create City as a Custom Post Type? You can then define a slug 'cities' and get that result, and also add taxonomies like categories and tags to further aid navigation.
Don't do that. WordPress gives you a goog API for doing what you want to do: managing pretty URLs (slugs), database operations (you don't need to write/read files for that) and the right code workflow for registering and triggering actions.
So, I think you have to start reading the basics about WordPress plugins (its philosophy and API) and then just decide if you want to use its custom post types (ready to use) or if you want to create a specific content type.
yes this is fine approach.One more thing that you can do is that instead of adding the code inside the fie..Pick the code from DB.
Make a table in DB,create a column with varchar as datatype and insert the common code in it .

Categories