WordPress Code Examples (Beginner to Advanced Guide)

rsbloggers • May 1, 2026
Introduction
If you’re working with WordPress—whether as a developer, freelancer, or even a blogger—at some point you’ll need to write or understand code. And let’s be real, WordPress can feel overwhelming when you first dive into PHP functions, hooks, and templates.
But here’s the good news: you don’t need to memorize everything.
With the right WordPress code examples, you can quickly understand how things work and start building real features—custom themes, plugins, widgets, and more.
In this guide, I’ll walk you through practical, real-world WordPress code examples—from beginner basics to advanced usage—so you can actually apply them in your projects.
Why Learn WordPress Code?
Before jumping into code, let’s quickly understand why it matters.
1. Full Control Over Website
You’re no longer limited to themes or plugins—you can customize everything.
2. Build Custom Features
Want a custom form? API integration? Dynamic slider? You can build it.
3. Better Performance
Custom code is often faster than heavy plugins.
4. Career Growth
If you’re a developer, WordPress coding skills open doors to freelance and job opportunities.
Basic WordPress Code Examples
Let’s start with the basics.
1. Display Site Title
<?php
bloginfo('name');
?>
This displays your website name.
2. Display Site URL
<?php
echo home_url();
3. Display Current Year
<?php
echo date('Y');
Useful for footer copyright.
4. Display Post Title and Content
<?php
if (have_posts()) :
while (have_posts()) : the_post();
the_title('<h2>', '</h2>');
the_content();
endwhile;
endif;
?>
This is called The Loop, the core of WordPress.
WordPress Loop Examples
The Loop is how WordPress displays posts.
Basic Loop Example
<?php
if (have_posts()) :
while (have_posts()) : the_post();
?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<?php
endwhile;
endif;
?>
Custom Loop with WP_Query
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
This gives you full control over what posts to display.
Theme Development Code Examples
1. Add Featured Image Support
<?php
function theme_setup() {
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'theme_setup');
2. Register Navigation Menu
<?php
function register_my_menu() {
register_nav_menu('primary', 'Primary Menu');
}
add_action('init', 'register_my_menu');
3. Display Menu in Theme
<?php
wp_nav_menu(array(
'theme_location' => 'primary',
));
?>
Plugin Development Code Examples
1. Create Simple Plugin
<?php
/*
Plugin Name: My Custom Plugin
Description: Simple plugin example
*/
function my_custom_message() {
echo "Hello from plugin!";
}
add_action('wp_footer', 'my_custom_message');
2. Add Custom Shortcode
<?php
function my_shortcode() {
return "This is my shortcode output";
}
add_shortcode('mycode', 'my_shortcode');
Use it like: [mycode]
3. Display Menu in Theme
<?php
wp_nav_menu(array(
'theme_location' => 'primary',
));
?>
WordPress Hooks Examples
Hooks are the backbone of WordPress.
1. Action Hook Example
<?php
add_action('wp_head', function() {
echo '<meta name="custom" content="example">';
});
2. Filter Hook Example
<?php
add_filter('the_title', function($title) {
return $title;
});
Custom Post Type Example
<?php
function create_post_type() {
register_post_type('portfolio',
array(
'labels' => array(
'name' => 'Portfolio',
),
'public' => true,
'has_archive' => true,
)
);
}
add_action('init', 'create_post_type');
Perfect for:
- Projects
- Services
- Case studies
Custom Taxonomy Example
<?php
function create_taxonomy() {
register_taxonomy('project_type', 'portfolio', array(
'label' => 'Project Type',
'hierarchical' => true,
));
}
add_action('init', 'create_taxonomy');
Meta Box Example
<?php
function add_custom_meta_box() {
add_meta_box(
'custom_box',
'Custom Field',
'custom_box_callback',
'post'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function custom_box_callback($post) {
echo '<input type="text" name="custom_field">';
}
AJAX Example in WordPress
JavaScript
jQuery(document).on('click', '#btn', function() {
jQuery.post(ajaxurl, {
action: 'my_ajax_action'
}, function(response) {
console.log(response);
});
});
PHP
<?php
add_action('wp_ajax_my_ajax_action', 'my_ajax_function');
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_function');
function my_ajax_function() {
echo "AJAX Response";
wp_die();
}
Perfect for:
- Projects
- Services
- Case studies
Security Code Examples
1. Sanitize Input
<?php
$name = sanitize_text_field($_POST['name']);
2. Escape Output
<?php
echo esc_html($name);
3. Nonce Verification
<?php
if (!wp_verify_nonce($_POST['_wpnonce'], 'my_nonce')) {
die('Security check failed');
}
Performance Optimization Code
Disable Emojis
<?php
remove_action('wp_head', 'print_emoji_detection_script', 7);
Limit Revisions
<?php
define('WP_POST_REVISIONS', 5);
FAQs
What is WordPress coding?
It involves using PHP, HTML, CSS, and JavaScript to customize WordPress.
Do I need coding for WordPress?
Not mandatory, but highly beneficial.
Is WordPress good for developers?
Yes, it’s widely used and highly customizable.
Which language is used in WordPress?
Mainly PHP, along with HTML, CSS, and JavaScript.