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

Read More
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?

Related posts

Leave a Reply

5 comments

  1. 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.

  2. 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

    1. Creates a custom post type Event
    2. Creates a custom taxonomy City
    3. Adds custom metaboxes to Event
    4. 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
    
    1. SITE_URL/cities/CITY would yield a page of all EVENT posts in that CITY
    2. 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.

    Screenshot

    Then, go to your Ce Events -> Cities admin screen.

    screenshot 2

    hover over one of your terms (in my case new york city) and click view.

    screenshot 3

    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

    test event is not part of city it is a post categorized by city

  3. 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.

  4. 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 .