Do you want to Develop Content Management System in PHP?
1

Do you want to Develop Content Management System in PHP?

[adrotate group="5"]

Develop Content Management System in PHP, in this tutorial you will learn about how to create a content management system using PHP & MySQL from scratch.

There are two main things in this content management system frontend and backend. Front end for displaying content and backend for managing content.

We will start with backend by creating these modules. First of all, we need admin login, category crud, content crud.

Then after that, we will build the front end to display content on the index page, also category pages and single article pages.

Then I’ll add pagination in frontend Index and category pages. After that, I’ll add a comment system in the single article page.

Then after that, we will move on to the backend for Managing Comments, then we will work on creating a settings page with few basic settings for our CMS system.

1. Planning & Creating database Tables

Before starting to work on our application we need database tables for our Content Management System. In this step, I’ll plan the database tables that we need in our application and I’ll create them.

We need Users table for admin login, category table for category crud, content table for content crud, comments table for comment system and settings table for CMS settings. These are the table we need in our Content Management System.

Develop Content Management System in PHP

Are You Excited to Build Content Management System on your own?


I've good News for you! Created a Video Course with 5.5 Hours of Content...

In users table, we need id, name to store admin users full name and email address for login, next is password here I’m going to use md5 hashing algorithm use a more secure hashing algorithm that is password_hash for storing passwords. Active column to know whether the user is active or not.

Next is category table, we should add id for category table and name column to store category name and description to store category description in case if you want to add a description for SEO purposes. Image column for adding category image.

In the content table, we need id. Category id for linking articles to a category, title column for the title of the article. Content column for the actual content of the article. URL column for storing clean URL of the article, it is used to make article URL as SEO friendly clean URL. Create date is content created date time. Publish date is Content published date time. Featured image for storing the featured image of the article link in WordPress. And finally, I’m adding userid of the admin user who created this article.

Next comments table, we need id and cid for store content id which this comment will be linked to. Name column for storing the name of the person who is commenting and email column for storing email of the commenter. the subject column is for storing entire comment, submittime is to store comment submitted date time. Status column is for knowing whether the comment is approved or unapproved by admin.

In the setting table, we need id. Name column for storing the setting name and the value column is for storing the value of the specific setting. In this way, we can scale it up add multiple setting values. You will understand more about it while we are working on the settings page.

2. Connecting to Database and Creating Directories

First of all create CMS directory, to hold all our cms project files. Inside this directory create few subdirectories one is config directory to store all the configuration setting and database connection file.

inc directory for frontend template files, we can reuse these files in frontend PHP pages. Create a new directory with the name of img to store all the images that we will be using in frontend like the logo and other images.

Next is admin directory for CMS administration purposes. And we need to create few other PHP files. Here is the tree structure of our entire CMS project files. Just create these files and directories in your file system.

-cms
–admin
—inc
—-footer.php
—-header.php
—-nav.php
—uploads
—addcategory.php
—addcontent.php
—category.php
—comments.php
—commentstatus.php
—content.php
—delcomment.php
—delcontent.php
—edit-profile.php
—editcontent.php
—editcontent.php
—fimagedel.php
—index.php
—logodel.php
—logout.php
—settings.php
—styles.css
–config
—connnect.php
–img
—logo.png
–inc
—footer.php
—header.php
—nav.php
—sidebar.php
–index.php
–category.php
–functionsfe.php
–single.php
–styles.css

If you create all these files in advance it will become easier for you to add code to these pages later.

Next, we should connect to the database to proceed with our application. I’ve created a database with the name of cms, you can create a database with other names, you just have to use the database name you created.

Copy this code and save it in the connect.php file inside config directory.

3. Admin Login in Content Management System

First of all, we should create the login system for Admin user, Here I’ve created a complete article how you can create login system from scratch also created a complete video course.

Here is the complete HTML code to create our Admin login page, save it in login.php inside admin directory.

In the login.php file, we should start the session, because we will create the session after successfully submitting the login credentials. And also loading connect.php file with require_once php function.

Next, we should whether the form is submitted or not, we can check by using this code.

After that we should sanitize the data for ensuring no SQL injection attacks, I’m using mysqli_real_escape_string PHP function.

Next, I’m writing select SQL query to compare username and password exists and matches with the submitted username and password.

We should execute this query and I’m checking the count equals to one using if condition if the count equals to 1 then I’m creating the session and redirecting the user to index.php file in admin directory. Otherwise, I’m displaying an error message to the fmsg variable.

After adding the above code, now you should be able to login to admin area you will be redirected to index.php file.

But in admin index.php we are not checking whether the user is logged in or not? For making sure user is logged in you should add this code in index.php and on all other admin pages

Next, we should be able to log out the admin user. Add this code to the logout.php file in admin directory.

4. Creating Re-Usable Template Files

Previously while creating the files and folders we have created inc directory in frontend and backend for re-usable template files.

Template Files in Admin Area

These are the admin template files, I’m creating 3 template files that are header.php, footer.php and nav.php files.

Save this code in header.php in admin inc directory.

Next is nav.php, save this code in nav.php in admin inc directory

Template Files in Front End

In frontend we need four template files, that are header.php, footer.php, nav.php and sidebar.php

Save this code in header.php in cms inc directory.

Save this code in nav.php in cms inc directory

Save this code in sidebar.php in cms inc directory

5. Category CRUD in CMS

In this step, you will learn how we can create category CRUD in our Content Management System in PHP. If you don’t know how to create CRUD operations check out my article => Simple CRUD Application in PHP & MySQL.

First, we will see creating the categories, displaying category, editing category and deleting categories in the admin section.

Creating Category

In creating the category page that is addcategory.php file add this HTML code.

To display the categories in addcategory.php file add this code

We should check whether form is submitted or not

Here I’m inserting category and subcategories, first, we will add code to create main categories and then we will add code to subcategories.

Here I’m inserting subcategories

Displaying Categories

After creating the categories, we should display these categories inside category.php for other operations like edit and delete. Admin users can edit categories and delete categories from category.php file.

Here is the complete code of category.php and save this code in category.php

Editing Categories

Here I haven’t created edit category page, you can replicate the code form addcategory.php save it as editcategory.php and do the update operation.

Deleting Categories

I’ve not created deleted category functionality you can follow my Simple CRUD application in PHP and do the delete operation for deleting the category.

Develop Content Management System in PHP

Are You Excited to Build Content Management System on your own?


I've good News for you! Created a Video Course with 5.5 Hours of Content...

6. Content CRUD in CMS

In this step, we will create the content and display the content, updating the content and deleting the content.

Creating Content

Here is the HTML code of addcontent.php save it as addcontent.php. Here I’m using tinymce editor.

After form submission, we should check the form submission with an if condition and also sanitizing the user submitted data with the mysqli_real_escape_string PHP function.

After executing the above code, we will insert the record into the database. If the insert query is successful, I’m redirecting the user to editcontet.php file with id or else I’m displaying an error message.

Till now we are able to insert the content into the database and the thing we are missing is the featured image, we should upload the featured image also.

Here is the code for uploading the featured image.

Displaying Content

After inserting the content we should be able to view all the articles from content table. Here I’m going to display all the articles in the content.php file.

Here is the HTML code with while to display the articles in table format.

Before displaying all the articles in table format we should fetch the articles from the database for that I’m going to select records from the database with pagination system.

Next, I’m not displaying the pagination button in the content.php file for displaying pagination buttons and making it work we should add this block of code.

After adding all the above code you should be able to see all the articles from the content table in the content.php file.

Updating Content

In update content page we will display the article data in the form when the user submits the form that article will be updated.

And the HTML for edit page is also similar to add content page.
Here is the code, save it as editcontent.php

Before updating the content we should be able to delete and re-upload the featured image again. For deleting the image I’m using a different file with the name of fimagedel.php. We will see about this file later in this step.

In edit content page, we should fetch the article information. To fetch the article data from database you can use this code.

After form submission, I’ll check for the form is submitted or not, if it’s submitted I’ll sanitize the user submitted data.

Here is the code to do that same, save it in editcontent.php file

After form submission and sanitizing user submitted data we should run the update query, it the query is successful I’ll display a success message or else I’ll display an error message.

In edit content page we missed one thing that is about featured image. I’ll show how we can delete the featured image by just clicking on the link. After deleting the image we should be able re-upload the image again. So that we have added file upload code to editcontent.php

Here is the code to upload the image again in editcontent.php file.

Next is deleting the featured image after clicking on delete button.

Deleting Content

From content.php admin user can edit the article or can delete the content. First, we will fetch the path of the featured image from the content table based on the id passed in URL.

Then we will check whether this image exists in the file system if it exists we will delete the image and the record or else we will delete just the record.

If the id is not passed through the URL, we will redirect the user to content.php without any actions and also in all other conditions.

7. Displaying Articles in Index Page

In previous steps, we finished admin login, category crud, and content crud. Now we are ready and equipped to display the article in the frontend.

First of all, I’ll be displaying the articles in index.php file.

In frontend, I’m using the functionsfe.php file to create functions in frontend for the purpose of code reusability. We should load this functions file and connect.php file for the database connection.

I’m creating a function to set the current page for our pagination on our index page and the good news is we can reuse this code in category page also. That is all these functions we can reuse on the category page.

I’m creating a new function to get the per page results value from settings database table. Here is the function code

And we should fetch the published content from the content table, I’m making this function adoptable to category page also by passing category argument.

I’m creating one more function to select published content with pagination.

Print Pagination function for printing pagination in index.php and also I’ll use this function in category.php file also.

Here is the HTML code of index.php with the while loop to display all the articles.

Here is the code for displaying the article in index page with the above functions.

8. Displaying Category Articles in Category Page

I’m using the same functions as index.php file. And using one extra function to fetch the category name.

Here is the HTML code to display category based articles with the while loop

Before displaying the articles we should add this PHP code to the functions. First I’m checking the id is passed through the URL. It it’s passed then I’m going to proceed with fetching the articles based on category or else I’m redirecting the user to index.php file.

9. Displaying Single Articles in single.php

Displaying Single Articles in the single.php page based on the URL passed through the URL slug.

Here I’m creating PHP function to select single article based on the URL passed. I’m sending that URL slug in this function as an argument.

Here is the HTML code for single.php file

And PHP code to fetch the articles

Develop Content Management System in PHP

Are You Excited to Build Content Management System on your own?


I've good News for you! Created a Video Course with 5.5 Hours of Content...

10. Adding Comment System in CMS Single Article Page

In single.php file, front-end users can submit the comments. I’ve already created an article for building comment system. You can follow this tutorial create comment system in our Content Mangement System

=> Creating Comment System in CMS

Conclusion

This is the starting point for creating powerful Content Management System. You can improve a lot by adding advanced features.

If you are getting any issues, let me know through the form below. And what are all the features you want to add to your content management system?

Hire Me if you have any work.


how to create a crud content management system in php
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 1 comments
ibrahim

this is very helpfull code thanks for sharing this awoseme tutorials vivek vengala keep it up

Reply

Leave a Reply: