Getting a complete clean URL - php

I'm trying to figure out to get a complete clean url for my blog. Right now I have a decent url that looks like this:
http://example.com/post/44/post-name
Here "44" is the Post ID which I use to get the correct post from the database.
But I have seen wordpress blog having url's like this:
http://example.com/post-name
How can this be achieved? as I need the post ID / $_GET['id'] to be able to get the post.

You don't necessarily need the id to retrieve the Post. You only need an indexed attribute with unique values.
To achieve this, your Post model needs to have a "slug" attribute. That slug attribute can be based on any other attribute or combination of attributes you choose, but it's usually based on a "name" or "title" attribute.
So to make all this work, when you create a new Post, you need to create a slug from the name/title and store that in the DB, along with the other Post model data. In your controller action you will then retrieve the /slug from the URL and query the db Post data for a Post with the same slug.
You'll also need to figure out to handle duplicate slugs as they need to be unique. Perhaps something like this would work: /slug-2

Make a database table mapping slugs to IDs, then lookup the ID from the slug in the URL.
When a blog is created,
INSERT INTO slugs_postids (slug, id) VALUES (?, ?);
Then when you do a lookup,
SELECT id FROM slugs_postids WHERE slug = ?;

Related

RESTful convention to manage items and types of items

I have a database with two tables: Type and Item.
Each Item has a Type (so in the Item table there is a field named type_id).
I need to create an REST api to manage those items but I'm struggling to define the URL structure for it.
My initial approach is:
POST /api/type Creates a new type.
PUT /api/type/id Updates an existing type.
Then for the items:
POST /api/items/typeId To create a new item and relate it to the type.
PUT /api/items/typeId/itemId To update the item.
The problem that I can see with this is, after an item is created, it looks like I cannot change its type, so I changed it to this:
POST api/type/typeId/item To create a new item.
PUT api/items/itemId To update an existing item.
But it doesn't seem right (lack of consistency?).
Any help, please? What is the convention to manage parent/children items?
Just because item is related to type, it does not live in a hierarchical structure below type, especially if the relation can be changed. See it as a flat structure:
POST /api/item
PUT /api/item/{itemId}
where the item's typeID is just one of the POST/PUT parameters.
Where it makes sense to use hierarchical URLs is for retrieving data. For example
GET /api/type/{typeId}/items
lists all items of this type.
Note: I used the singular form item instead of items in POST/PUT for consistency reasons. You only send one item at a time. While when retrieving, you retrieve multiple items.

How to URL Slug without id?

I've been thinking about how does people make a special link like
www.domain.com/this-is-a-link
then i checked how to make a slug URL and it was good, But i was wondering, Do i make a special column for SLUG URL inside the database and call the entire row using the SLUG instead of the id? Is that how all people do it? Or is there a better way?
[id title content slug]
[1 TheTitle LoremIpus the-title]
$query = "SELECT * FROM posts WHERE slug = $slug";
What you are looking for is URL rewriting. Please search on that topic. You will find articles like These, which has information about the URL rewriting!

URL input field in a HTML form

I am creating a form to allow admin users to create new 'News' Items. I am using PHP Codeigniter. All works perfect at the moment storing and retrieving data from MYSQL database.
I have now realised I would like the option to add external URL links that relate to each news item. Possibility to add multiple links for each. I was thinking of adding a ´links´table to the database, foreign key relation with News table. Something like below:
tbl_news:
news_id,
title,
descrip,
...etc
tbl_link:
link_id,
title,
URL,
news_id
This will take a bit of processing as it is 2 input fields per link on the form and could become messy if the user adds multiple links per news item. Just wondering is there a better way to allow for URL input in a HTML form?

WordPress - Taxonomy MetaData RETRIEVE

I am using https://github.com/bainternet/My-Meta-Box to create MetaData in my taxonomy "group"
How do I retrieve that data? As of right now I have each group ID, but i have no idea how to retrieve the MetaData.
Just figured it out. Unfortunately, https://github.com/bainternet/My-Meta-Box Uses the WP_Options table in the database, not the Meta table, like it should.
Here is how to get the information.
<?$meta = get_option('tax_meta_'.$category->term_id)?>

how wordpress can un-slug a title

i still , don't understand , how wordpress can understand what is this url refer to :
www.mysite.com/about-me/
they are using no identifier
if they using slug functions so how they can retain story information or in other word , how they change back the slugged title to select from database
It processes the "pretty" URL and queries the database with that data. Of course slugs are checked to be unique on creation. Details are in the function url_to_postid() in the file wp-includes/rewrite.php.
If you want to directly obtain the id from the slug you can query the database:
SELECT ID
FROM wp_posts
WHERE post_name = '$slug'
you might need to check wp_posts which is the default name, but it depends on the installation.
This is just a guess:
My guess is that they store the titles in a database, and make sure every title is unique. That way, they can do a look-up by title and know which item is coupled to that.

Categories