How do I use PHP to create a content management system (CMS)?

Creating a Basic Content Management System (CMS) with PHP

A Content Management System (CMS) allows users to create, edit, and manage content on their websites without having to dive into the code directly. In this blog post, we will create a simple CMS using PHP and MySQL. We will cover the basics, such as setting up the database, creating an admin panel, and displaying content on the front-end.

Getting Started:

To begin, we’ll need to set up a database to store our content. In this example, we will use MySQL, but you can choose any database that you are comfortable with.

First, create a new database and a table called ‘posts’. You can do this by running the following SQL query:

CREATE DATABASE cms;
USE cms;

CREATE TABLE posts (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Now, let’s set up our PHP files. We will create three main files:

  1. config.php – This file will store our database connection settings.
  2. admin.php – This file will handle the admin panel where we can create and edit posts.
  3. index.php – This file will display the posts on the front-end of the website.

Let’s create config.php with the following code:

<?php
# PHP
$host = 'localhost';
$db_name = 'cms';
$username = 'root';
$password = '';

try {
    $connection = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

Next, let’s create the admin.php file, which will display a form to create and edit posts:

<?php
# PHP
require 'config.php';

if (isset($_POST['submit'])) {
    $title = $_POST['title'];
    $content = $_POST['content'];

    $sql = "INSERT INTO posts (title, content) VALUES (:title, :content)";
    $stmt = $connection->prepare($sql);
    $stmt->bindParam(':title', $title);
    $stmt->bindParam(':content', $content);
    $stmt->execute();
}
?>

<!-- HTML -->
<!DOCTYPE html>
<html>
<head>
    <title>Admin Panel - Simple CMS</title>
</head>
<body>
    <h1>Create a new post</h1>
    <form action="admin.php" method="post">
        <label for="title">Title:</label>
        <input type="text" id="title" name="title" required><br>
        <label for="content">Content:</label>
        <textarea id="content" name="content" rows="10" cols="30" required></textarea><br>
        <input type="submit" name="submit" value="Create Post">
    </form>
</body>
</html>

Lastly, let’s create the index.php file, which will display the posts on the front-end:

<?php
# PHP
require 'config.php';

$sql = "SELECT * FROM posts ORDER BY created_at DESC";
$stmt = $connection->prepare($sql);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!-- HTML -->
<!DOCTYPE html>
<html>
<head>
    <title>Simple CMS</title>
</head>
<body>
    <h1>Blog Posts</h1>
   <?php
# PHP
foreach ($posts as $post) {
    echo '<div>';
    echo '<h2>' . htmlspecialchars($post['title']) . '</h2>';
    echo '<p>' . nl2br(htmlspecialchars($post['content'])) . '</p>';
    echo '<small>Posted on: ' . $post['created_at'] . '</small>';
    echo '</div>';
    echo '<hr>';
}
?>
</body>
</html>

Now that we have our basic CMS set up, you can create new posts using the admin.php page and view them on the index.php page. Keep in mind that this is a very basic implementation and should not be used in a production environment without proper validation, sanitization, and authentication.

To improve this CMS, consider implementing the following features:

  • User authentication and authorization for the admin panel
  • Updating and deleting posts
  • Pagination for displaying a limited number of posts per page
  • Adding a WYSIWYG (What You See Is What You Get) editor for the content textarea

We hope this tutorial has given you a basic understanding of how to create a simple content management system using PHP and MySQL. There are many ways to expand upon this foundation to create a more feature-rich and secure CMS tailored to your specific needs.

Got question?

Submit it here

© All rights reserved.