Defining dynamic pages in wordpress plugin - php

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 .

Related

Wordpress - Hide specific fields to certain user role on back end

Is there a way in which I can disable/hide specific fields from the "page form" in the back-end? I have currently test two plugins but none of them meet the requirement that I have, like for example this plugin "User Role Editor by Members – Best User, Role and Capability Management Plugin for WordPress":
I'm sorry that the options are in spanish but the capabilities options are for example: "edit pages", "edit other people's pages", "publish pages", "delete pages"... What I want is to hide certain sections to the "editor" user role from the page form:
Is this even possible?, and if not, what alternatives do I have?, the thing is that I'm afraid that whoever is in charge of editing will end up messing up with the pages...
Use Adminimize plugin It will resolve problem easily.
This plugin changes the administration back-end and gives you the power to assign rights on certain parts.
In most cases, the plugin "Adminimize" that Tech Sourav mentioned will work just fine, but since this page I'm working with is using some annoying custom content editor and other theme tools, the workaround that I ended up doing was:
Create a custom post type with the "Custom Post Type UI" plugin.
In this case I will only and always have 3 pages that have this "modifiable" price field, so once the custom post type is added I added 3 entries to this custom post type (here I will show you just one).
I'm also using the "advanced custom fields pro" plugin in order to create this price field into my entries.
When the custom field has been created there's this option inside the same previous form that allows me to show that specific field just created into specific post types entries, so I made up the rules in order to show this field in the created custom post type entries:
In this example I'm just showing you the rule for one entry, in order to make it appear on the other 2 entries, I would just click in the "agregar grupo de reglas" and select one by one.
Once this custom field and rules are configured correctly, I should see now the custom field "precio" inside my specified entries form:
After saving the entry with the updated custom field value, I will now check the post id and save it for later (we're going to need it):
According to the documentation of Advanced Custom Fields plugin I can show the saved value of certain input within certain post with the shortcode: [acf field="{$field_name}" post_id="{$post_id}"], in this case I will change the values to: [acf field="precio" post_id="1372"] and paste the code into the section of the page that I will show this:
The builder that this page is using has an element called "code block" but you can use it in the equivalent element that allows you to paste code from your theme.
After saving the changes, now the only thing that I have left is to restrict the ability to edit pages to a user with the "editor" role, for this I will use the plugin that I mentioned before in my question called "User Role Editor by Members – Best User, Role and Capability Management Plugin for WordPress":
As you can see, I will not let editor users to mess up with the pages post types, so with this, they will only be able to edit the custom post type that I created, which even if they write something inside the content of that custom post type entry, the website won't be affected at all. This may be a little bit hacky but it works...

WordPress multiple content sections

Alright I am completely lost on how to do this so I am just going to throw the question out there and hope somebody can help me in some way or another.
WordPress offers the option to create custom Taxonomies so I got the idea of pulling something out of my hat and creating a custom content section apart from the content section wordpress currently offers.
The website shown above is the only website I have been able to find which currently does this and is on a WordPress platform, as you can see they have the SoundCloud audio player pushed over to one side with the actual content placed to the other side creating that flawless look.
I guess the question here is how can I turn <?php the_content(); ?> into something completely custom pulling from a completely different form in the back end so the same information is not shared.
There are numerous ways to achieve what you want.
Shortcodes
Custom page templates
Custom meta fields to store values like the soundcloud embed url, to afterwards use it in your code.
You can always also enter HTML in the page editor, but for obvious reasons not recommended.
Custom post types allow you to define a new type of content. It will have its own section in admin where you add and edit data separate from normal Wordpress posts. It can have its own templates allowing different layouts, and you can set up custom fields so that it can store different data.
I also recommend taking a look at the Pods Framework. This plugin allows you to define custom content types very easily in the admin interface. You can set up all your fields and data types from there. All that's required then is to create the templates to make it look nice.
The nice thing about these approaches is that while the data is kept separate from standard Wordpress posts, you still have the same familiar UI, so it's easy for none-technical users to update the content.

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.

Wordpress custom post type or custom plugin for recipes?

I need to create a way for users to enter recipe information into a wordpress page that can be displayed globally, on their buddypress user page, import/export based on a defined xml standard and accept reviews for each recipe. In one example the user would be able to enter more than one type of ingredient (i.e. tomatoes) with each item having the same level of detailed fields yet when exported to xml, each tomato would have its own <tomato> tags. Hope that makes sense.
My question is should I build a custom plugin or can custom post types work? If a custom plugin would provide the best solution, can you suggest reading material that would put me on the best path to create such a solution?
In that case, custom post types would work just fine. The Types plugin can automate a good part of it, including the admin interface to edit your recipes.
You could define a new taxonomy for ingredients.

Adding custom content to a Wordpress blog

Is there a way in a Wordpress blog to get the admin side of Wordpress to add custom content in the database, that will not be anything related to posts or pages?
It will just be a little bit of info such as date, title, description, and an image that I will use in a specific part of the front page.
Is wordpress too restrictive on this, will I have to use a different CMS?
Edit:
Could Custom Post Types be what I am looking for?
"It will just be a little bit of info such as date, title, description, and an image that I will use in a specific part of the front page."
So, if I am reading this correctly, you want to create a custom post type, which describing what you are saying - "date, title, description, image" - this is basically a post ;)
You can easily hook into the wordpress query object.
E.g.
$args = array(
'post_type'=>'my_special_post',
'posts_per_page'=>8
);
query_posts($args);
The the normal while posts applies. If you don't want it to be in within the post array, then nothing is stopping you from just creating your own queries which is well explained here: http://codex.wordpress.org/Custom_Queries.
I should add creating post types as well - they are easily configured in your functions.php file without need to add a plugin. Unless you want a more managed solution: http://codex.wordpress.org/Post_Types
You should store these values in custom fields.
Recent WP releases fully support custom types: http://codex.wordpress.org/Post_Types#Custom_Types
This allows you to create a new, full-class data type that will have a new menu option below post and page.
Also, take a look at the More Types plugin (http://wordpress.org/extend/plugins/more-types/), which provides an easy and powerful GUI for doing this. Along with More Fields by the same author (http://wordpress.org/extend/plugins/more-fields/), WP becomes a pretty powerful CMS.

Categories