Skip to main content

Managing Page Content using PHP and Summernote Text Editor Tutorial

Screenshot
Body

This is simple Tutorial Code that tackles about Managing Page Content on a Web Application. This tutorial will help you to understand and have an idea of how to create a Page Content Management Feature for a CMS Website. This kind of feature can give your end-users a way to create or manage page content through the front-end or without managing the actual source code files. This can be useful for those who are planning to develop a Content Management System (CMS) using PHP Language.

Below, I provided a source code for a simple web-based application where end users can created a new page content. The source code below uses the following libraries:

Since we will be using PHP Language for our back-end codes, kindly download and install also a local web server such as XAMPP to run the PHP Scripts.

Creating the Interfaces

The below scripts are the PHP Files which contains the HTML Scripts for the interface and PHP Scripts. Save the following files according to the filename written above each scripts.

index.php

This file is the main page of the application which list all the created page content.

  1. <?php session_start(); ?>
  2.  
  3. <!DOCTYPE html>
  4. <html lang="en">
  5.  
  6. <meta charset="UTF-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <title>Creatin Page Content using Summernote</title>
  10. <link rel="stylesheet" href="./css/bootstrap.min.css">
  11. <link rel="stylesheet" href="./summernote/summernote-lite.css">
  12. <script src="./js/jquery-3.6.0.min.js"></script>
  13. <script src="./summernote/summernote-lite.js"></script>
  14. <script src="./js/bootstrap.min.js"></script>
  15. :root {
  16. --bs-success-rgb: 71, 222, 152 !important;
  17. }
  18.  
  19. html,
  20. body {
  21. height: 100%;
  22. width: 100%;
  23. font-family: Apple Chancery, cursive;
  24. }
  25. .btn-info.text-light:hover,.btn-info.text-light:focus{
  26. background: #000;
  27. }
  28. </style>
  29. </head>
  30.  
  31. <body class="bg-light">
  32. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  33. <div class="container">
  34. <a class="navbar-brand" href="https://sourcecodester.com">
  35. Sourcecodester
  36. </a>
  37. </div>
  38. </nav>
  39. <div class="container py-3" id="page-container">
  40. <h3>Creating Page Content using Summernote Library</h3>
  41. <hr>
  42. <?php
  43. if(isset($_SESSION['msg'])):
  44. ?>
  45. <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
  46. <div class="d-flex w-100">
  47. <div class="col-11"><?php echo $_SESSION['msg']['text'] ?></div>
  48. <div class="col-1 d-flex justify-content-end align-items-center"><button class="btn-close" onclick="$(this).closest('.alert').hide('slow')"></button></div>
  49. </div>
  50. </div>
  51. <?php
  52. unset($_SESSION['msg']);
  53. endif;
  54. ?>
  55. <div class="col-12 my-2">
  56. <a href="manage_page.php" class="btn btn-info text-light text-decoration-none"> + Add New Page Content</a>
  57. </div>
  58. <div class="row row-cols-sm-1 row-cols-md-3 row-cols-xl-4 gx-4 gy-2">
  59. <?php
  60. $pages = scandir('./pages');
  61. asort($pages);
  62. foreach($pages as $page):
  63. if(in_array($page,array('.','..')))
  64. continue;
  65. ?>
  66. <div class="col">
  67. <div class="card border-right border-primary rounded-0">
  68. <div class="card-body">
  69. <div class="col-12 text-truncate"><a href="view_page.php?page=<?php echo urlencode($page) ?>" title="<?php echo $page ?>" class=""><b><?php echo $page ?></b></a></div>
  70. <div class="w-100 d-flex justify-content-end">
  71. <a href="manage_page.php?page=<?php echo urlencode($page) ?>" class="btn btn-sm rounded-0 btn-primary me-2">Edit</a>
  72. <a href="delete_page.php?page=<?php echo urlencode($page) ?>" class="btn btn-sm rounded-0 btn-danger delete_data">Delete</a>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. <?php endforeach; ?>
  78. </div>
  79. </div>
  80. $(function(){
  81. $('.delete_data').click(function(e){
  82. e.preventDefault()
  83. var _conf = confirm("Are you sure to delete this page content?")
  84. if(_conf ===true){
  85. location.href=$(this).attr('href')
  86. }
  87. })
  88. })
  89. </script>
  90. </body>
  91. </html>
manage_page.php

This file is the interface for the page content form. This is the page where the users create new and update page content file.

  1. <?php session_start(); ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.  
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Creatin Page Content using Summernote</title>
  9. <link rel="stylesheet" href="./css/bootstrap.min.css">
  10. <link rel="stylesheet" href="./summernote/summernote-lite.css">
  11. <script src="./js/jquery-3.6.0.min.js"></script>
  12. <script src="./summernote/summernote-lite.js"></script>
  13. <script src="./js/bootstrap.min.js"></script>
  14. :root {
  15. --bs-success-rgb: 71, 222, 152 !important;
  16. }
  17.  
  18. html,
  19. body {
  20. height: 100%;
  21. width: 100%;
  22. font-family: Apple Chancery, cursive;
  23. }
  24. input.form-control.border-0{
  25. transition:border .3s linear
  26. }
  27. input.form-control.border-0:focus{
  28. box-shadow:unset !important;
  29. border-color:var(--bs-info) !important
  30. }
  31. .note-editor.note-frame .note-editing-area .note-editable, .note-editor.note-airframe .note-editing-area .note-editable {
  32. background: var(--bs-white);
  33. }
  34. </style>
  35. </head>
  36.  
  37. <body class="bg-light">
  38. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  39. <div class="container">
  40. <a class="navbar-brand" href="https://sourcecodester.com">
  41. Sourcecodester
  42. </a>
  43. </div>
  44. </nav>
  45. <div class="container py-3" id="page-container">
  46. <?php
  47. if(isset($_SESSION['msg'])):
  48. ?>
  49. <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
  50. <div class="d-flex w-100">
  51. <div class="col-11"><?php echo $_SESSION['msg']['text'] ?></div>
  52. <div class="col-1"><button class="btn-close" onclick="$(this).closest('.alert').hide('slow')"></button></div>
  53. </div>
  54. </div>
  55. <?php
  56. unset($_SESSION['msg']);
  57. endif;
  58. ?>
  59. <div class="card">
  60. <div class="card-header">
  61. Manage Page Content
  62. </div>
  63. <div class="card-body">
  64. <form action="save_page.php" id="content-form" method="POST">
  65. <input type="hidden" name="filename" value="<?php echo isset($_SESSION['POST']['filename']) ? $_SESSION['POST']['filename'] : (isset($_GET['page']) ? str_replace('.html','',$_GET['page']) : '') ?>">
  66. <div class="form-group col-6">
  67. <label for="fname" class="control-label">File Name <span class="text-info"><small>([a-z0-9A-Z_-])</small></span></label>
  68. <input type="text" pattern="[a-z0-9A-Z_-]+" name="fname" id="fname" autofocus autocomplete="off" class="form-control form-control-sm border-0 border-bottom rounded-0" value="<?php echo isset($_SESSION['POST']['fname']) ? $_SESSION['POST']['fname'] : (isset($_GET['page']) ? str_replace('.html','',$_GET['page']) : '') ?>" required>
  69. <span class="text-info"><small>This will be added with .html file extension upod saving.</small></span>
  70. </div>
  71. <div class="form-group col-12">
  72. <label for="content" class="control-label">Content</label>
  73. <textarea name="content" id="content" class="summernote" required><?php echo isset($_SESSION['POST']['content']) ? $_SESSION['POST']['content'] : (isset($_GET['page']) ? file_get_contents("./pages/{$_GET['page']}") : '') ?></textarea>
  74. </div>
  75. </form>
  76. </div>
  77. <div class="card-footer">
  78. <button class="btn btn-sm rounded-0 btn-primary" type="submit" form="content-form">Save</button>
  79. <a class="btn btn-sm rounded-0 btn-light" href="./">Cancel</a>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. $('.summernote').summernote({
  85. placeholder: 'Create you content here.',
  86. tabsize: 5,
  87. height: '50vh',
  88. toolbar: [
  89. ['style', ['style']],
  90. ['font', ['bold', 'underline', 'clear']],
  91. ['color', ['color']],
  92. ['para', ['ul', 'ol', 'paragraph']],
  93. ['table', ['table']],
  94. ['insert', ['link', 'picture', 'video']],
  95. ['view', ['fullscreen', 'codeview', 'help']]
  96. ]
  97. });
  98. </script>
  99. </body>
  100.  
  101. </html>
view_page.php

This file is the page that displays the page contents created.

  1. <?php session_start(); ?>
  2.  
  3. <!DOCTYPE html>
  4. <html lang="en">
  5.  
  6. <meta charset="UTF-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <title>Creatin Page Content using Summernote</title>
  10. <link rel="stylesheet" href="./css/bootstrap.min.css">
  11. <link rel="stylesheet" href="./summernote/summernote-lite.css">
  12. <script src="./js/jquery-3.6.0.min.js"></script>
  13. <script src="./summernote/summernote-lite.js"></script>
  14. <script src="./js/bootstrap.min.js"></script>
  15. :root {
  16. --bs-success-rgb: 71, 222, 152 !important;
  17. }
  18.  
  19. html,
  20. body {
  21. height: 100%;
  22. width: 100%;
  23. font-family: Apple Chancery, cursive;
  24. }
  25. .btn-info.text-light:hover,.btn-info.text-light:focus{
  26. background: #000;
  27. }
  28. </style>
  29. </head>
  30.  
  31. <body class="bg-light">
  32. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  33. <div class="container">
  34. <a class="navbar-brand" href="https://sourcecodester.com">
  35. Sourcecodester
  36. </a>
  37. </div>
  38. </nav>
  39. <div class="container py-3" id="page-container">
  40. <h3>Creating Page Content using Summernote Library</h3>
  41. <hr>
  42. <?php
  43. if(isset($_SESSION['msg'])):
  44. ?>
  45. <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
  46. <div class="d-flex w-100">
  47. <div class="col-11"><?php echo $_SESSION['msg']['text'] ?></div>
  48. <div class="col-1 d-flex justify-content-end align-items-center"><button class="btn-close" onclick="$(this).closest('.alert').hide('slow')"></button></div>
  49. </div>
  50. </div>
  51. <?php
  52. unset($_SESSION['msg']);
  53. endif;
  54. ?>
  55. <div class="col-12 my-2">
  56. <a href="./" class="btn btn-info text-light text-decoration-none"> Back to List</a>
  57. </div>
  58. <div class="content">
  59. <?php echo isset($_GET['page']) && is_file("./pages/{$_GET['page']}") ? file_get_contents("./pages/{$_GET['page']}") : "<center>Unknown Page Content.</center>" ?>
  60. </div>
  61. </div>
  62. </body>
  63. </html>

Creating the PHP Scripts

The following scripts are the PHP files that contains the codes that saving,updating, and deleting the page content files. Save the following files according to the filename written above each scripts.

save_page.php

This is the script for saving the new page content or updating the existing page content.

  1. <?php
  2. if(isset($_SESSION['msg']))
  3. unset($_SESSION['msg']);
  4. if(isset($_SESSION['POST']))
  5. unset($_SESSION['POST']);
  6. $current_name = $_POST['filename'];
  7. $new_name = $_POST['fname'];
  8. $content = $_POST['content'];
  9. $i = 0;
  10. if(!is_dir("./pages"))
  11. mkdir("./pages");
  12. if($current_name != $new_name){
  13. $nname = $new_name;
  14. while(true){
  15. if(is_file("./pages/{$nname}.html")){
  16. $nname = $new_name."_".($i++);
  17. }else{
  18. break;
  19. }
  20. }
  21. $new_name = $nname;
  22. }
  23. if(!empty($current_name) && $current_name != $new_name){
  24. rename("./pages/{$current_name}.html","./pages/{$new_name}.html");
  25. }
  26. $save = file_put_contents("./pages/{$new_name}.html",$content);
  27. if($save > 0){
  28. $_SESSION['msg']['type'] = 'success';
  29. $_SESSION['msg']['text'] = 'Page Content Successfully Saved.';
  30. header('location:./');
  31. }else{
  32. $_SESSION['msg']['type'] = 'danger';
  33. $_SESSION['msg']['text'] = 'Page Content has failed to save.';
  34. $_SESSION['POST'] = $_POST;
  35. header('location:'.$_SERVER['HTTP_REFERER']);
  36. }
  37. ?>
delete_page.php

This is the script for deleting the existing page content.

  1. <?php
  2. if(isset($_SESSION['msg']))
  3. unset($_SESSION['msg']);
  4. if(isset($_SESSION['POST']))
  5. unset($_SESSION['POST']);
  6.  
  7. $page = $_GET['page'];
  8.  
  9. if(is_file("./pages/{$page}")){
  10. $delete = unlink("./pages/{$page}");
  11. if($delete){
  12. $_SESSION['msg']['type'] = 'success';
  13. $_SESSION['msg']['text'] = 'Page Content Successfully deleted.';
  14. }else{
  15. $_SESSION['msg']['type'] = 'danger';
  16. $_SESSION['msg']['text'] = 'Page Content has failed to delete.';
  17. }
  18. }else{
  19. $_SESSION['msg']['type'] = 'danger';
  20. $_SESSION['msg']['text'] = 'Page Content is unknown.';
  21. }
  22. header('location:'.$_SERVER['HTTP_REFERER']);

There you go. You can now test the web application on your end and see if it works like it was planned to. If you have encountered any errors, kindly review your source code with the source code I provided above. You may also download the working source code I created for this tutorial. The download is located below this article.

DEMO VIDEO

That's the end of this tutorial. I hope this simple PHP Tutorial will help you with what you are looking for and for your future CMS Projects.

Explore on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment