Objective: Understand how WordPress uses PHP to render content dynamically, explore the Loop,
template hierarchy, functions.php, and create your first custom shortcode plugin.
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:
In short, PHP = the engine of WordPress.
Typical theme folder:
/wp-content/themes/my-theme/
β
βββ style.css
βββ functions.php
βββ header.php
βββ footer.php
βββ index.php
βββ single.php
βββ page.php
βββ archive.phpEach .php file plays a role in the template hierarchy β the logic WordPress uses to decide which template to load.
| 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.
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.
functions.php β The Heart of Theme Functionalityfunctions.php is like your themeβs mini plugin. It allows you to add new PHP functionality without editing WordPress core files.
Common uses:
<?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.
function streetgeek_register_menu() {
register_nav_menu( 'main-menu', __( 'Main Menu' ) );
}
add_action( 'init', 'streetgeek_register_menu' );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.
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!
You can move your shortcode into a standalone plugin.
/wp-content/plugins/streetgeek-hello/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.
esc_html() or esc_attr() when outputting data.$wpdb) instead.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.
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.
Follow the earlier plugin steps and make your own version:
[streetgeek_welcome name="YourName"]β
Output a personalized message like:
Welcome back, Yogi β ready for the next level!
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.
| # | 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 |
Objective: Build an upgraded WordPress plugin using PHP functions and shortcode logic.
Requirements:
streetgeek-hello-2streetgeek-hello-2.php<?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.
[streetgeek_thanks] working in theme
Congratulations! Youβve completed the StreetGeek Academy PHP Foundations Course and built the skills
to create plugins, themes, and dynamic apps.
π You now know:
Next steps at StreetGeek Academy: