Creating Your Own CMS Part 3 – Advanced Features

Posted in Coding, PHP, Tutorials3 years ago • Written by 14 Comments

If you have been following along you should have a simple CMS built that has a place to securely log in and you should be able to create some users and access levels for your site. If you are creating a CMS you will need more than just to be able to log in and create users so here in part three we will get into a few of the things that you can do with your own CMS. A few people have really knocked the idea of creating your own CMS which is fine because we can all have our own opinions so I will just tell you all why I personally like to use my own.

Reasons You Might Build Your Own CMS

First off, the main reason is that you have 100% control. Yes, it can be a lot of work, but if you plan to have multiple sites then it is awesome to have your own CMS that you can easily fully customize to whatever you want. Secondly, I did this to further sharpen my PHP, JS, AJAX, Jquery, CSS, and HTML skills. The company that I work for does not, and will not, ever use open source CMS or even a retail CMS. There are several ups and downs to using a common CMS. The ups are that you can easily find people to help you work on things with it and one of the main downsides is if there is any sort of flaw or security issue then everyone will know about it quickly and you can be vulnerable to attacks until it is fixed.

Adding Content To Your CMS

To kick this off I would like to talk about the front end structure of your site first. If you want to be able to easily add content on the fly to your site then the layout has to allow for it. Unless you create a CMS that builds the entire page’s HTML files. The example that I plan to show you does not show building entire HTML pages, but it can easily be altered to do so. The basic setup of the content manager that I am showing you guys is going to require a layout that is flexible but simple and a little bit of work from the .htaccess.

The Back End For Adding Content

The actual back end itself is very basic but depending on your site and features it can very well be more complex if you like. You simply start with a form in your CMS to add/edit/delete content pages. This form will need at the very least a place for a url and a place to add content. Technically you do not need the url you could simply do a ?id=84 with the 84 just being an auto increment from the database. Obviously this is a pretty ugly way to do it so we will assign URLs to each. Along with a URL and content I always have at least a page title, meta keywords, and meta description but regardless of what you have it is all basically the same.

What You Need On The Front End

If you have made it this far along in the tutorials I assume you probably do not need any help with how to make this form so I will skip those details. However, I will just mention that each of the new content pages that you add with this form will need to be stored in a database. I do it with an auto increment id field for the PRIMARY key and also add an INDEX on the url field because we will be using that field to look up pages. From there you can handle the front end in a few different ways. You can go back to my flat file caching and cache out each content page as an array using the url as the key for each element so that it is easy to reference. I have done things like this in the past but don’t usually anymore as the benefits are really more opinion/preference than performance in many situations. Another option, and the one I most commonly use, would be to simply take the url and look up the page in the database then display the content. When done this way I like to have a header.php, header.init.php, and of course my index.php and footer.php. The header.init will be where I have the code to parse out the URL and try to find the content page in the database. The header is mainly just some html that contains the header area of my website and the index is the middle content area of my site that will display content per the given URL. You will also have a .htaccess file to handle redirects.

.htaccess

<br />
RewriteEngine On</p>
<p>RewriteCond %{HTTP_HOST} ^bbillman.com$ [NC]<br />
RewriteRule ^(.*)$ http://www.bbillman.com/$1 [R=301,L]<br />
RewriteCond %{HTTP_HOST} ^([www\.]?)bbillman.com$ [NC]<br />
RewriteRule ^(.*)$ http://www.bbillman.com/$1 [R=301,L]</p>
<p>RewriteRule ^tmp(.*)$ /admin/tmp_image.php [NC,L]<br />
RewriteRule ^(.*)(\.(jpg|jpeg|gif|png|css|ico|xml|txt))$	-	[NC,L]<br />
RewriteRule ^$	-	[NC,L]</p>
<p>#RewriteCond %{DOCUMENT_ROOT}/$1 !-f<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_URI} !(.*)/$<br />
RewriteRule ^(.*)$	/$1/	[NC,L,R=301]</p>
<p>RewriteCond %{DOCUMENT_ROOT}/$1 !-f<br />
RewriteRule ^(.*)$	index.php?uri=$1	[QSA]<br />

The first line is obvious, it turns the engine on, the next few will make sure www is always at the front of the url, the third block will basically tell it to ignore the rest of the htaccess if it is an image being accessed, the fourth is saying to first look for the specified file, and the end is like a catch-all where it will send anything to index.php with a query string of uri=$1 ($1 will be whatever is after the / in the url).

header.init.php

<br />
 $v)<br />
			{<br />
				if(is_array($v))<br />
					return is_empty($v);<br />
				elseif(isset($v) || $v!='')<br />
					return false;<br />
			}<br />
		}<br />
		else<br />
		{<br />
			if(isset($array) || $array!='')<br />
				return false;<br />
			else<br />
				return true;<br />
		}<br />
		return true;<br />
	}</p>
<p>	if(is_array($parts) &amp;amp;&amp;amp; !is_empty($parts)){<br />
		$rparts = array_reverse($parts);<br />
		foreach($rparts as $v){<br />
			$p_q = $db-&amp;gt;query(&amp;quot;SELECT url,content,meta_title,meta_keywords,meta_description,parent_id FROM content WHERE seo_url='$v' AND live='1' LIMIT 1&amp;quot;);<br />
			if($db-&amp;gt;num_rows($p_q)){<br />
				$_PAGE = $db-&amp;gt;fetch_assoc($p_q);<br />
				if(is_array($_PAGE)){<br />
					foreach($_PAGE as $k =&amp;gt; $sv)<br />
						$_PAGE[$k] = stripslashes($sv);<br />
				}<br />
				break;<br />
			}<br />
		}<br />
	}else{<br />
		$p_q = $db-&amp;gt;query(&amp;quot;SELECT url,content,meta_title,meta_keywords,meta_description FROM content WHERE seo_url='' AND live='1' LIMIT 1&amp;quot;);<br />
		$_PAGE = $db-&amp;gt;fetch_assoc($p_q);<br />
		if(is_array($_PAGE)){<br />
			foreach($_PAGE as $k =&amp;gt; $sv)<br />
				$_PAGE[$k] = stripslashes($sv);<br />
		}<br />
	}</p>
<p>	if(!is_array($_PAGE)){<br />
		//404<br />
	}<br />
?&amp;gt;<br />

The code is pretty simple. It just takes the uri and breaks it up into an array and starting from the back it searches the database for a matching url. If the url exists then it will put all of the contents for that page into an array $_PAGE and if not then it will try to grab the home page (where url=”) and if there is no page then you can have it do a 404. In the header of course you would set the meta data and have whatever other design stuff you need for your site in there.

index.php

<br />
&amp;lt;?php<br />
include'includes/header.php';<br />
echo $_PAGE['content'];<br />
include'includes/footer.php';<br />
?&amp;gt;<br />

Using this method your index.php could be as simple as this. It will include the header and footer and just output the content of the requested URL in the middle.

Conclusion To Creating Your Own CMS Part 3

In conclusion, this is an easy and effective way to make adding content quick and easy. This can also be done for blogs actually and done almost exactly the same way. One thing is you would have to add a rule in your htaccess to send URLs with /blog/ to a separate blog.php or add some more code to your index.php, but I prefer to have them separate for readability and such. Likewise this can be used for events or pretty much any other content that you would like to add to your site. Adding new content is obviously important as it will help you to get some SEO built up because the internet is all about information.

Other parts of the tutorial:

7 Written ArticlesWebsite

I am a web developer by trade but originally went to school for Information Technology - Network Engineering Technology at Purdue University. Getting into web development as a student web developer I developed a passion for it that left networking seem a bit boring. Even though I finished up my networking degree I stuck with web development lately I have been a WP7 advocate. My Blog.

14 Comments Best Comments First
  • Ajax

    Thursday, May 12th, 2011 20:56

    6

    Pretty sloppy article. Did anyone even test this stuff out before posting.. ?

    Obviously nobody bothers to check if the infomation in these articles are complete. Instead of wasting everyone’s time trying to follow along with half missing code, you could provide the complete code in a zip file like other more professional site’s do.

    This is a great way to make sure no one ever comes back to this site, just keep posting incomplete articles and dont follow up with the problems you have created.

    And hey, thanks for wasting our time..

    +4
  • Stephen

    Friday, April 29th, 2011 16:16

    5

    Thank you for this great tutorial. This is what I have been looking for ever since I started playing with Dreamweaver. I understand HTML, CSS & some JS, but when I start looking at PHP my head starts to spin. However this well put together tutorial helped me.
    IT’S NOT WORKING!
    I must have missed something but I went through all the tutorials of yours more than once and I just don’t see what I have missed. So I also ask you if you could give us some download for the files. I know that I will be able to find my error but what is more important, you would be helping so many more.

    :)

    +1
  • Bart

    Friday, April 22nd, 2011 11:27

    4

    the code for header.init.php seems incomplete ;)

    +1
  • Fred

    Saturday, July 23rd, 2011 03:30

    8

    Agree with Ajax

    wasted time… not working first part, not 2nd, and this the same….
    :(
    it would be great to look at complete script to clearly understand and try it out :)

    +1
  • Tommy

    Wednesday, May 2nd, 2012 17:10

    14

    This is quite complicated, at least for me. I only got the firs part working the log in and log out. A demo download would be really helpful.

    Thanks.

    0
  • Paul Web

    Thursday, April 19th, 2012 03:39

    12

    I have a dedicated member on my team who deals with CMS, and I had him creating the custom CMS for the website early on. However, I know see the importance of knowing how to create one myself because I prefer to have more control over the content. Doing one myself can definitely save me a lot of money, rather than hiring someone else to do it for me.

    0
    • Dainis Graveris

      Thursday, April 19th, 2012 08:18

      13

      If you know how to do that and enjoy coding, it makes sense to do it yourself for sure!

      0
  • Qaysar

    Monday, April 11th, 2011 12:18

    2

    Hello,

    Thank you for this very nice tutorial. I wanted to know if the files to this exercise will be available for download?

    Thanks

    0
  • Matt Spencer

    Wednesday, April 18th, 2012 00:18

    10

    I think you need quite a lot of experience with HTML, CSS and the like in order to create your own CMS. A beginner like me is satisfied with using Drupal, or by using the simple wordpress interface. Usually I would just hire a freelance web designer to create a custom CMS for me.

    0
    • Dainis Graveris

      Wednesday, April 18th, 2012 07:02

      11

      Actually it’s becoming easier and easier, Matt! There are so good resources teaching you basics and also code is becoming simpler and simpler.

      It’s definitely worth knowing basics, but for something advanced, better don’t waste time, hire somebody with passion :)

      +1
  • Shaun

    Sunday, September 4th, 2011 19:53

    9

    You Must Have Print Articles Option In You Blog

    0
  • Tiago

    Monday, April 18th, 2011 01:00

    3

    I don´t understand the begining of header.init.php. In the first line, “$v)” what is it mean?

    0
  • Brad Billman

    Friday, May 13th, 2011 19:56

    7

    Sorry guys the RTE in wordpress messes code up a lot. It is obnoxious I will take a look and get a post up of what is missing.

    -1
  • Martin Haško

    Monday, April 4th, 2011 21:39

    1

    Hello! I really like your tutorials on their own CMS, but I could ask to contact you? Or at least (I would be grateful) that you somehow put together for other users to download. In early reply thanks in advance! :)

    -1
  • Tommy

    Wednesday, May 2nd, 2012 17:10

    14

    This is quite complicated, at least for me. I only got the firs part working the log in and log out. A demo download would be really helpful.

    Thanks.

    0
  • Paul Web

    Thursday, April 19th, 2012 03:39

    12

    I have a dedicated member on my team who deals with CMS, and I had him creating the custom CMS for the website early on. However, I know see the importance of knowing how to create one myself because I prefer to have more control over the content. Doing one myself can definitely save me a lot of money, rather than hiring someone else to do it for me.

    0
    • Dainis Graveris

      Thursday, April 19th, 2012 08:18

      13

      If you know how to do that and enjoy coding, it makes sense to do it yourself for sure!

      0
  • Matt Spencer

    Wednesday, April 18th, 2012 00:18

    10

    I think you need quite a lot of experience with HTML, CSS and the like in order to create your own CMS. A beginner like me is satisfied with using Drupal, or by using the simple wordpress interface. Usually I would just hire a freelance web designer to create a custom CMS for me.

    0
    • Dainis Graveris

      Wednesday, April 18th, 2012 07:02

      11

      Actually it’s becoming easier and easier, Matt! There are so good resources teaching you basics and also code is becoming simpler and simpler.

      It’s definitely worth knowing basics, but for something advanced, better don’t waste time, hire somebody with passion :)

      +1
  • Shaun

    Sunday, September 4th, 2011 19:53

    9

    You Must Have Print Articles Option In You Blog

    0
  • Fred

    Saturday, July 23rd, 2011 03:30

    8

    Agree with Ajax

    wasted time… not working first part, not 2nd, and this the same….
    :(
    it would be great to look at complete script to clearly understand and try it out :)

    +1
  • Brad Billman

    Friday, May 13th, 2011 19:56

    7

    Sorry guys the RTE in wordpress messes code up a lot. It is obnoxious I will take a look and get a post up of what is missing.

    -1
  • Ajax

    Thursday, May 12th, 2011 20:56

    6

    Pretty sloppy article. Did anyone even test this stuff out before posting.. ?

    Obviously nobody bothers to check if the infomation in these articles are complete. Instead of wasting everyone’s time trying to follow along with half missing code, you could provide the complete code in a zip file like other more professional site’s do.

    This is a great way to make sure no one ever comes back to this site, just keep posting incomplete articles and dont follow up with the problems you have created.

    And hey, thanks for wasting our time..

    +4
  • Stephen

    Friday, April 29th, 2011 16:16

    5

    Thank you for this great tutorial. This is what I have been looking for ever since I started playing with Dreamweaver. I understand HTML, CSS & some JS, but when I start looking at PHP my head starts to spin. However this well put together tutorial helped me.
    IT’S NOT WORKING!
    I must have missed something but I went through all the tutorials of yours more than once and I just don’t see what I have missed. So I also ask you if you could give us some download for the files. I know that I will be able to find my error but what is more important, you would be helping so many more.

    :)

    +1
  • Bart

    Friday, April 22nd, 2011 11:27

    4

    the code for header.init.php seems incomplete ;)

    +1
  • Tiago

    Monday, April 18th, 2011 01:00

    3

    I don´t understand the begining of header.init.php. In the first line, “$v)” what is it mean?

    0
  • Qaysar

    Monday, April 11th, 2011 12:18

    2

    Hello,

    Thank you for this very nice tutorial. I wanted to know if the files to this exercise will be available for download?

    Thanks

    0
  • Martin Haško

    Monday, April 4th, 2011 21:39

    1

    Hello! I really like your tutorials on their own CMS, but I could ask to contact you? Or at least (I would be grateful) that you somehow put together for other users to download. In early reply thanks in advance! :)

    -1

Comments are closed.

54.243.17.133 - unknown - unknown - US