8/03/2016

Wordpress: Creating Custom Pages and Posts -- URLs and PHP

Wordpress Overview


Wordpress is a content-management system (CMS) used for blogging and other types of  publishing needs.

One can change the look and functionality of Wordpress web pages by installing a theme. The theme contains a combination of design templates and programming. There's probably 100s of thousands of different WP themes out in the world. And each comes pre-made with tools, settings, widgets, and styles. Some templates are free, some have costs.

Beyond WP themes, more functionality can be added with plug-ins. Again, there's many thousands of WP plug-ins that do all types of simple or complicated things.

But even with all these themes and plug-ins, Wordpress still may not be able to do what you want it to do. You may, for example, have existing code for a special database interaction that Wordpress will not be able to integrate.

But, Wait! Wordpress Probably Can Integrate Your Needs or Your Legacy Code


Wordpress is programmed using a common web server language called PHP. This is a scripting language that is very powerful and can be easily understood by most programmers.

PHP is written in plain code and can be mixed with HTML and CSS in a file. The format is very similar to Javascript language, except PHP code is enclosed in tags that begin and end with an angle bracket and question mark, and also that PHP variable names commonly begin with a dollar sign character ($).

Ex.

<!DOCTYPE html>
<html>
<head>
  <style>
    h1 { color: blue; font: 1.5em arial, sans-serif; }
  </style>
</head>
<body>
<h1>This Is a Test</h1>

<?php

$myMessage = "Hello world";
echo "<p>" . $myMessage . "</p>";

?>
</body>
</html>

OK, How Do I Make My Own Custom PHP Pages Integrate with Wordpress?


Wordpress produces, what is essentially three types of output -- PostsArchives and Pages.

  1. Posts would be like blog entries or news articles, with published dates. 
  2. Archives are lists of Posts, typically associated with an Author or Category or Tag, and Posts are usually sorted by date in the Archives.
  3. Pages are less dynamic as far as dates, and one might use a Page for an "About Us" or list of info that does not need to be archived or listed by date. 
Two other important elements of Wordpress are the way that Pages are classified in the structure of the site -- Categories and Tags -- as mentioned above. 

Each Post or Page will have what is called a slug. This is a combination of letters or characters (without spaces) that are uniquely associated with an entry, and the slug is usually part of the permanent URL of the published Post or Page.

Ex. 

http://www.mysiterighthere.org/2017/12/how-to-customize-a-page-in-wordpress/

Wordpress automatically assigns these slugs based on the ID number of the entry in the database or on the title of the Post or Page. The slug can be customized, though. 

Wordpress Folder Structure

Wordpress is installed on a web server as a group of mostly PHP files which are grouped in folders.

Ex,
/wp-admin
/wp-content
/wp-includes
.htaccess
... and a whole bunch of single PHP files.

The template that you choose for your Wordpress installation is located inside the /wp-content/ folder. 

Ex. 
/wp-content/themes/nameOfYourTheme/

Inside of the theme folder will be more folders and a few files that are common to all Wordpress installations. These files, once activated, sort of take priority over the default pages that come with Wordpress.

Exs.
  404.php
  archive.php
  author.php
  category.php
  footer.php
  functions.php
  header.php
  index.php
  page.php

The category.php and page.php files are of most interest to me for this discussion.

Making a Custom Wordpress Page


So, let's say you have a database locations that you want your visitors to be able to browse. Maybe you had this working on a previous site. How do you integrate this database and unique functionality in Wordpress?

The answer is: You make a duplicate of the page.php file and change the contents and filename.

First, you need to make a Page in the Wordpress system for you custom page.
Dashboard > Pages > [Add New]

  • Enter the Title
  • Upload a default Featured Image for the Page if you like.
  • [Save Draft]
  • Change the permalink to the custom URL that you would like.
  • The part of the link you are changing will be the new slug. (This is important!)
  • Write down the slug that you chose -- "my-custom-slug-wording-here"
  • Whenever you are ready, come back to this admin page and change the Page status from "Draft" to [Publish.]
Okay, now you need to have access to text editor and an FTP client.

  • Log in to your web server.
  • Go to the Wordpress theme folder.
  • Locate the page.php file.
  • Either copy the page.php file to a new filename or create a new file and then copy-n-paste the contents of the page.php file into the new text file.
  • The filename you are going to use is this: page-[your slug].php.
Ex.

   page-my-custom-slug-wording-here.php

Now, because you have created a file with the beginning filename "page," the Wordpress system will look for the custom file each time a visitor goes to the Page that you created earlier in Wordpress.

This is documented here in Wordpress -- Theme Handbook > Page Templates

Once you've done that, it's up to you to figure out how to connect your database programming to the Theme you've chosen. 

Ex. 

My theme contains a line that reads: <?php the_content(); ?>

So, I open the page-my-custom-slug-wording-here.php, and then I find that line, and I replace it with my own customized PHP code to display the database contents.

There's a lot more to this, of course, but this is a basic instruction page.

Creating a Custom Category Arhive Page in Wordpress

The custom Category pages are very similar to the way we created a custom Page. 

You might have a general Entertainment section of your website. This section might be subdivided in to different types of entertainment such as: Art, Film, Theater.

Each of these different sections is defined in the Wordpress system as a Category.

You might need to change one of these Category / Archive pages because they will only list the Posts that are assigned to it. However, your site might also provide more services associated with these section -- Ex. Calendar, Directory of Venues, Link to Special Discounts or Affiliate sites.

To create a Category:

  Go to: Dashboard > Posts > Categories.

Again, you will have a slug and also a ID number for the Category. You can use either of these to create a custom Category file. Write down the slug, and you can get the ID from the browser URL of the category you just created -- tag_ID=[your number]

As noted above, the default category.php file will be located within your theme folder.

You need to:

Duplicate the category.php file.
Save the new file as category-[your slug].php or category-[your category ID].php

Ex.
  category-art.php
or
  category-32.php

Again, you will then open the new custom Category file in a text editor and change the code to incorporate whatever it is that you want you custom page to do.

This is documented here at -- Theme Handbook > Taxonomy Templates

Good luck. Let me know if this was helpful.

3 comments :

  1. What a great post that you have shared here. You make information effortless for us that we get good facts about it. Thanks for this informative blog. website developer Singapore

    ReplyDelete
  2. Great job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us. Affordable Graphic Designer Singapore

    ReplyDelete
  3. Very good information, this information is excellent and essential for everyone. I am thankful to you for providing this kind of information. website designer sydney

    ReplyDelete