Search code examples
phpwordpressphp-5.2

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?


Solution

  • 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