π§© Module 13: Bonus β Intro to PHP in WordPress
Objective: Understand how WordPress uses PHP to render content dynamically, explore the Loop,
template hierarchy, functions.php, and create your first custom shortcode plugin.
πΉ 1. How WordPress Uses PHP
WordPress is built entirely on PHP + MySQL. Every page you visit β whether itβs a blog post or contact page β is dynamically generated using PHP templates and queries to the database.
β PHP in WordPress handles:
- Loading content from the database
- Applying your themeβs design
- Running plugin functions
- Displaying posts, users, menus, and widgets
In short, PHP = the engine of WordPress.
πΉ 2. Understanding the WordPress Theme Structure
Typical theme folder:
/wp-content/themes/my-theme/
β
βββ style.css
βββ functions.php
βββ header.php
βββ footer.php
βββ index.php
βββ single.php
βββ page.php
βββ archive.php
Each .php file plays a role in the template hierarchy β the logic WordPress uses to decide which template to load.
π§© Template Hierarchy Example
| Page Type | Template File Used |
|---|---|
| Homepage | front-page.php or home.php |
| Single Blog Post | single.php |
| Static Page | page.php |
| Category Archive | category.php |
| Fallback Template | index.php |
π§ WordPress looks for the most specific file first. If it doesnβt find it, it βfalls backβ to the next most general one.
πΉ 3. The WordPress Loop
The Loop is how WordPress dynamically displays posts using PHP.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php else : ?>
<p>No posts found.</p>
<?php endif; ?>
β Each post passes through this loop β pulling title, content, date, author, etc., from the database.
πΉ 4. functions.php β The Heart of Theme Functionality
functions.php is like your themeβs mini plugin. It allows you to add new PHP functionality without editing WordPress core files.
Common uses:
- Enqueue scripts and styles
- Add custom image sizes
- Register menus and widgets
- Create shortcodes and custom functions
π§© Example: Enqueue Styles
<?php
function streetgeek_enqueue_styles() {
wp_enqueue_style( 'main-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'streetgeek_enqueue_styles' );
β Adds your CSS stylesheet properly the WordPress way.
π§© Example: Register Menu
function streetgeek_register_menu() {
register_nav_menu( 'main-menu', __( 'Main Menu' ) );
}
add_action( 'init', 'streetgeek_register_menu' );
πΉ 5. Creating a Custom Shortcode
A shortcode is a small PHP function that users can insert into posts or pages like this:
[streetgeek_hello]
Letβs create one inside functions.php:
function streetgeek_hello_shortcode() {
return "<h3>Hello from StreetGeek Academy!</h3>";
}
add_shortcode( 'streetgeek_hello', 'streetgeek_hello_shortcode' );
β
Now you can add [streetgeek_hello] to any WordPress post or page and it will output your message.
Shortcode with Attributes
function streetgeek_greet_shortcode( $atts ) {
$atts = shortcode_atts(
array( 'name' => 'Student' ),
$atts
);
return "<p>Welcome, " . esc_html( $atts['name'] ) . "! Keep learning PHP!</p>";
}
add_shortcode( 'streetgeek_greet', 'streetgeek_greet_shortcode' );
β Example usage:
[streetgeek_greet name="Yogi"]
Outputs:
Welcome, Yogi! Keep learning PHP!
πΉ 6. Building Your First Plugin
You can move your shortcode into a standalone plugin.
Step 1: Create Plugin Folder
/wp-content/plugins/streetgeek-hello/
Step 2: Add File streetgeek-hello.php
<?php
/*
Plugin Name: StreetGeek Hello
Plugin URI: https://streetgeek.academy
Description: A simple WordPress plugin that greets users.
Version: 1.0
Author: Yogi
*/
function streetgeek_hello_shortcode() {
return "<h3>Hello, StreetGeek Developer!</h3>";
}
add_shortcode( 'streetgeek_hello', 'streetgeek_hello_shortcode' );
β
Go to Dashboard β Plugins β StreetGeek Hello β Activate
Now [streetgeek_hello] works across the entire site.
πΉ 7. Security and Best Practices
- Always use
esc_html()oresc_attr()when outputting data. - Use nonces for form verification.
- Never use direct database queries β use the WordPress database API (
$wpdb) instead. - Donβt edit core WordPress files β use themes, child themes, or plugins.
πΉ 8. WordPress Database with PHP
Every WordPress site runs on MySQL. You can interact with it safely using the $wpdb global object.
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM $wpdb->posts WHERE post_status = 'publish'"
);
foreach ( $results as $post ) {
echo "<p>" . esc_html( $post->post_title ) . "</p>";
}
β Retrieves and displays all published posts from your site.
π§© HANDS-ON PRACTICE
Exercise 1: Create a Shortcode
Add this in your functions.php:
function streetgeek_thanks_shortcode() {
return "<p>Thank you for completing the PHP Foundations Course!</p>";
}
add_shortcode( 'streetgeek_thanks', 'streetgeek_thanks_shortcode' );
β
Add [streetgeek_thanks] to a post β it should render your message.
Exercise 2: Create a Plugin
Follow the earlier plugin steps and make your own version:
- Plugin Name: StreetGeek Welcome
- Shortcode:
[streetgeek_welcome name="YourName"]
β
Output a personalized message like:
Welcome back, Yogi β ready for the next level!
Exercise 3: Use The Loop in a Custom Template
Create home.php in your theme:
<?php get_header(); ?>
<h1>Latest StreetGeek Posts</h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo "<h2><a href='" . get_permalink() . "'>" . get_the_title() . "</a></h2>";
echo "<p>" . get_the_excerpt() . "</p>";
endwhile;
else :
echo "<p>No posts found.</p>";
endif;
?>
<?php get_footer(); ?>
β Displays all recent posts with links and excerpts.
π§Ύ Module 13 Quiz
| # | Question | Options | Correct |
|---|---|---|---|
| 1 | What language powers WordPress? | a) Java Β· b) PHP Β· c) Python | b |
| 2 | What file contains WordPress theme functions? | a) functions.php Β· b) main.php Β· c) custom.php | a |
| 3 | What does the Loop do? | a) Loads menus Β· b) Displays posts dynamically Β· c) Logs users out | b |
| 4 | What function registers a shortcode? | a) register_shortcode() Β· b) add_shortcode() Β· c) make_shortcode() | b |
| 5 | What is the correct shortcode syntax? | a) (shortcode) Β· b) [shortcode] Β· c) <shortcode> | b |
πͺ CHALLENGE TASK β StreetGeek Hello Plugin 2.0
Objective: Build an upgraded WordPress plugin using PHP functions and shortcode logic.
Requirements:
- Create plugin folder
streetgeek-hello-2 - Create
streetgeek-hello-2.php - Add the following code:
<?php
/*
Plugin Name: StreetGeek Hello 2.0
Description: Displays a personalized welcome message.
Version: 2.0
Author: StreetGeek Academy
*/
function streetgeek_hello2_shortcode( $atts ) {
$atts = shortcode_atts(
[ 'name' => 'StreetGeek Student' ],
$atts
);
$message = "Welcome back, " . esc_html( $atts['name'] ) . "! Keep building.";
return "<div style='padding:10px; background:#0E477B; color:#fff;'>$message</div>";
}
add_shortcode( 'streetgeek_hello2', 'streetgeek_hello2_shortcode' );
Upload, activate, and use [streetgeek_hello2 name="Yogi"].
β
Output:
Welcome back, Yogi! Keep building.
π§Ύ SUBMISSION CHECKLIST
- β
Shortcode
[streetgeek_thanks]working in theme - β Custom plugin created and activated
- β Loop-based template displaying posts
- β Quiz completed
- β Challenge plugin functioning
π GRADUATION π
Congratulations! Youβve completed the StreetGeek Academy PHP Foundations Course and built the skills
to create plugins, themes, and dynamic apps.
π You now know:
- PHP syntax, logic, arrays, and functions
- File, form, and database integration
- Sessions, authentication, and error handling
- How PHP powers WordPress under the hood
Next steps at StreetGeek Academy:
- Enroll in WordPress Development Foundations
- Advance to JavaScript for WordPress Developers
- Join the StreetGeek Certified Developer Track