Defining dynamic pages in wordpress plugin

I am creating my first wordpress plugin. In it, the user will be able to add new cities and view events in these cities.

My client's requirement is that the url should be like this

SITE_NAME/cities/NY 

or

 SITE_NAME/cities/Califonia 

It was decided that I will create cities in folders, and if the user tries to create a new city, I will create a file in this folder with this city. In addition, I will add an entry to the database. paste the PHP code into the file.

Be new to WP plugins. Is my approach right (for creating files)? Is there another way?

+4
source share
5 answers

maiorano84 wrote a fairly detailed guide on how to configure the material you need. Instead of relying on plugins, I prefer to show you how to write a plugin to register a custom post type and taxonomy. In this regard, I wrote a small plugin that should do everything you need, and it has a lot of comments and links to documents so that you can understand why.

code

https://github.com/fyaconiello/WP_Cities_Events

This plugin does a few things

  • Creates a custom post type.
  • Creates a custom City taxonomy
  • Adds personalized exchanges to the event.
  • Adds a city taxonomy to the event

This plugin does not require the installation of any additional plugins, it is without dependencies and uses only the WP core.

Urls

Regarding the correct URL structure , I would suggest you read this in full: 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 will give a page of all EVENT messages that CITY
  • you need the url: SITE_URL/cities/CITY/EVENT to read a specific event in a specific city.

EDIT as URL:

In the control panel Settings -> Permalinks select: "Mail name" and save.

Screen shot

Then go to the Ce Events -> Cities admin screen.

screenshot 2

hover over one of your conditions (in my case, the city of New York) and click on it.

screenshot 3

it should open this list list (city), and the URL structure looks like this: http: //wp.local/city/new-york-city/

if you need city to read cities , change line 102 of the main plugin file, which I shared with you:

 'rewrite' => array('slug' => 'city'), 

EDIT 2

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

+4
source

Your approach will work if you are not using Wordpress, but I would absolutely not recommend doing what you offer in the Wordpress Framework.

Instead of trying to reinvent the wheel, pretty much everything you want can be achieved with existing plugins. I would absolutely suggest using this route, especially if you do not have much experience programming Wordpress plugins.

Step 1:

Install Wordpress

Intuitive. I can’t do anything without it.

Step 2:

Install Types

Installing Type Plugin will give you a nice interface for registering your own Custom Post Types and many other features to extend the basic features of Wordpress.

Step 3:

Register Custom Post Type

Here, everything starts to get complicated. Using types, register a custom message type called Event. The reason you want each event to be its own Mail is because each event is unique. Cities are designed to encapsulate and define groups of events, which leads us to the next step:

Step 4:

Register a Custom Taxonomy

Think of taxonomy as a way to classify things. Wordpress comes with two default taxonomies: Categories (hierarchical) and tags (non-hierarchical). In your case, you will need to define a non-hierarchical taxonomy called "Cities" for your new type of event message.

Step 5:

Register Custom Fields

Custom fields are the easy part. What determines your event? Maybe some fields that determine the start and end time of this particular event? Checkmark for free drinks? The sky is the limit. What defining characteristics will all the events that make this event have? Add those as custom fields. They will appear in your Event Editor as Meta Boxes. If you don’t see the “Meta-boxes” on the “Edit Page” page for a specific event, be sure to turn it on by clicking “Screen Settings” in the upper right corner of the screen and check the box in which your fields will be located.

Step 6:

Configure Permalinks

Most of this can be done either through the types themselves (when setting up your custom taxonomy), or through .htaccess overwrites, or perhaps even through Permalinks Wordpress settings (although it is rather limited). My suggestion is to first set up custom taxonomy and permanent transfer settings before messing with .htaccess.

And this! I hope this should be enough for you to start everything you need.

+5
source

Why not create a city as a custom post type ? You can then define the “cities” pool and get this result, and add taxonomies, such as categories and tags, to further facilitate navigation.

+2
source

Do not do this. WordPress provides you with the goog API to complete your actions: managing good URLs (slugs), database operations (you don't need to write / read files for this), and the right code workflow for registering and starting up actions.

So, I think you need to start reading the basics of WordPress plugins (its philosophy and API), and then just decide whether you want to use your custom post types (ready to use) or if you want to create a specific type of content.

+2
source

Yes, this is a great approach. Another thing you can do is that instead of adding code inside fie..Pick code from the database. Create a table in the database, create a column with varchar as a data type and paste the common code into it.

0
source

All Articles