Lesson 6 – Introduction to PHP Programming and Custom WordPress Plugins, Databases & The Back End of WordPress

Lesson 6 – Introduction to PHP Programming and Custom WordPress Plugins, Databases & The Back End of WordPress

WHAT IS A DATABASE?

A database is a structured collection of data organized for efficient retrieval, storage, and management.

It serves as a central repository for storing and managing information in a way that facilitates easy access and manipulation.

A database management system (DBMS) is used to interact with the database, allowing users to create, update, retrieve, and manage data. MySQL is a popular open-source relational database management system. It is widely used for building and managing databases in various applications and websites. MySQL follows the relational model, employing tables with rows and columns to store and organize data, and it supports SQL (Structured Query Language) for querying and manipulating the data within the database.

WHAT IS PHP?

What is PHP?
PHP is an acronym for PHP: Hypertext Preprocessor
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
PHP is an amazing and popular language!

It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
It is deep enough to run the largest social network (Facebook)!
It is also easy enough to be a beginner’s first server side language!

What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP files have extension .php

What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data

With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side

What does PHP code look like?

PHP code can actually be added into an HTML page, by using the <?php ?> tags, as long as your file ends with .php and your server supports PHP.

<!DOCTYPE html>
<html>
<head>
    <title>Advanced PHP Example</title>
</head>
<body>

<?php
    // PHP code embedded within HTML
    $message = "Hello, World!";
    echo "<h1>$message</h1>";

    // Displaying current date and time
    $currentDateTime = date("Y-m-d H:i:s");
    echo "<p>Current Date and Time: $currentDateTime</p>";
?>

</body>
</html>

What is PHP used for?
https://alexwebdevelop.com/what-is-php-used-for/

W3 Schools PHP Tool:
https://www.w3schools.com/php/default.asp

Where can I learn more?

https://www.w3schools.com/php/php_intro.asp

Demo Part 1: Splitting Up HTML Code with PHP Includes

Demo Part 2: Custom PHP WordPress Plugin

PHP WordPress Plugin creation code:
https://www.dreamhost.com/blog/how-to-create-your-first-wordpress-plugin/

<?php
/**
* Plugin Name: Very First Plugin
* Plugin URI: https://www.yourwebsiteurl.com/
* Description: This is the very first plugin I ever created.
* Version: 1.0
* Author: Your Name Here
* Author URI: http://yourwebsiteurl.com/
**/
?>

This is a link to find the PHP WordPress Plugin Shortcode creation code:
https://torquemag.io/2017/06/custom-shortcode/

function my_shortcode_function() {
return 'Hello world!';
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );

What is a README.txt file?

https://www.mygreatlearning.com/blog/readme-file/

Discussion 6 - What is something that PHP can do?

*Submit Discussion into D2L

Now that we are exploring a bit about PHP, what is something interesting that it can do? Make a few searches on Google What can PHP do? Cool things PHP can do etc and see what you find that you find interesting!

Assignment 6 - Creating Your First WordPress Plugin

*Submit Assignment into D2L

Objective: Create a simple WordPress plugin that introduces you to the basics of plugin development. The plugin will implement a shortcode to display the current date and time on any WordPress page.

Requirements:

  1. Plugin Initialization:
    • Create a new folder in the wp-content/plugins directory of your WordPress installation. Name it something unique and related to your plugin (e.g., current-datetime-plugin).
    • Inside the plugin folder, create a main PHP file (e.g., current-datetime-plugin.php) for your plugin.
  2. Shortcode Implementation:
    • Write PHP code within your main plugin file to define a shortcode. Use the WordPress add_shortcode function to register your shortcode.
    • The shortcode should retrieve the current date and time using the date function with a format of your choice.
    • Output the date and time within an HTML element (e.g., <p>).
  3. Usage on Home Page:
    • Edit your WordPress home page (usually found in the Pages section of the admin dashboard).
    • Add the shortcode you created in the content of the home page to display the current date and time.
    • Save the changes and preview the home page to ensure the shortcode works as expected.
  4. Optional Styling (Bonus):
    • Enhance the appearance of the date and time display by adding basic CSS styling. You can include the styling within your plugin file or add within the Customizer of WordPress.
  5. Documentation:
    • Include comments in your code to explain the purpose of each section and any significant steps.
    • Write a brief README.txt file for your plugin, explaining how to install and use it.

Submit into D2L when complete!

Sample Student Work

Live Chat

Dustin has made it even easier to reach out—our live chat has moved to a text-based system so you can connect with him instantly from anywhere, not just from your browser.

Text “HTC” to 952-248-8883 to start a new conversation and get help right from your phone.

Looking forward to connecting!