← Back to PHP Course | Chapter 15: Web Development | Lesson 4 of 12

PHP WordPress Development

WordPress is a popular tool for making websites without writing everything yourself, and it is built with PHP. Developers can add their own themes and plugins to change how it looks and works.
Syntax
php
add_action("hook_name", "callback_function");
add_filter("hook_name", "callback_function");

if (have_posts()) {
    while (have_posts()) {
        the_post();   // The Loop
    }
}

add_shortcode("tag_name", "callback_function");

What is WordPress?

WordPress is a PHP-based content management system that powers a huge share of the web, giving non-developers a way to manage content while still exposing a full PHP API for custom development.

Example: What is WordPress?

php
<?php
// Print "WordPress: a PHP-based CMS with a full API for custom development" to the output
echo "WordPress: a PHP-based CMS with a full API for custom development";
?>

WordPress Hooks

Hooks are WordPress's extension mechanism: actions let your code run at a specific point in the request lifecycle, while filters let you intercept and modify data before WordPress uses or displays it.

Example: WordPress Hooks

php
<?php
// add_action('init', 'my_custom_function');
// add_filter('the_content', 'modify_content');
echo "Actions run code at a point in the lifecycle; filters modify data";
?>

The Loop

The Loop is the standard block of code every WordPress theme uses to fetch and display a list of posts, iterating through query results and rendering each one according to the theme's markup.

Example: The Loop

php
<?php
// while (have_posts()) { the_post(); the_title(); }
echo "The Loop iterates through queried posts and renders each one";
?>

Custom Post Types

Custom Post Types let you extend WordPress beyond blog posts and pages -- defining structures like products or reviews that get their own admin screens and can be queried just like built-in content.

Example: Custom Post Types

php
<?php
// register_post_type('product', ['public' => true]);
echo "Extends WordPress beyond posts and pages with its own admin screens";
?>

Shortcodes

Shortcodes let content editors trigger PHP functionality from inside the page editor by typing a bracketed tag like [gallery], without needing to touch a template file directly.

Example: Shortcodes

php
<?php
// add_shortcode('gallery', 'render_gallery_shortcode');
echo "Editors type [gallery] to trigger PHP functionality from the editor";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Editing WordPress core files, which are overwritten on update; use hooks or a child theme.
  2. Forgetting to use add_action or add_filter with the right hook name.
  3. Using a WP_Query loop and forgetting wp_reset_postdata() afterwards.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.