WordPress Developer Reference & Cheatsheet
WordPress Developer Reference & Cheatsheet
DodaTech
Updated Jun 6, 2026
4 min read
Learning Path
flowchart LR
A["Wordpress Overview"] --> B["Core Concepts"]
B --> C["Intermediate Topics"]
C --> D["Advanced Topics"]
D --> E["Practical Applications"]
A --> F["You Are Here"]
style F fill:#f90,color:#fff
This reference page is your quick-lookup guide for the most common WordPress development patterns. Bookmark it, print it, or keep it open in a tab — every snippet here is tested and ready to copy.
What You’ll Find
- Theme template hierarchy — which file loads for each page type
- WP_Query — advanced database queries with meta and tax filters
- Custom post types and taxonomies
- Actions and filters (hooks)
- REST API custom endpoints
- Theme setup and asset loading
- Security hardening snippets
Template Hierarchy
WordPress decides which template file to load based on the query type. It checks from most specific to most generic:
single-{post-type}-{slug}.php → single-product/awesome-widget.php
single-{post-type}.php → single-product.php
single.php → Default single post
singular.php → Fallback for any single content
page.php → Default page
archive.php → Archive pages (category, tag, date)
category.php → Category archive
tag.php → Tag archive
home.php → Blog posts index
front-page.php → Static front page
index.php → Ultimate fallback (always loaded if nothing else matches)Order of specificity: single-{post-type}-{slug}.php > single-{post-type}.php > single.php > singular.php > index.php
WP_Query — Advanced Queries
<?php
// Query products under $100 in the 'featured' category
$query = new WP_Query([
'post_type' => 'product',
'posts_per_page' => 10,
'meta_query' => [
['key' => 'price', 'value' => 100, 'compare' => '<'],
],
'tax_query' => [
['taxonomy' => 'category', 'field' => 'slug', 'terms' => 'featured'],
],
]);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<p>Price: <?php echo get_post_meta(get_the_ID(), 'price', true); ?></p>
<?php endwhile;
wp_reset_postdata();
endif;
?>
Common WP_Query Parameters
| Parameter | Example | Description |
|---|---|---|
post_type | 'product' | Which content type to query |
posts_per_page | 10 | Number of results per page |
meta_query | [['key'=>'price','value'=>100,'compare'=>'<']] | Filter by custom field values |
tax_query | [['taxonomy'=>'category','terms'=>'featured']] | Filter by taxonomy terms |
orderby | 'meta_value_num' | Sort by custom field (numeric) |
s | 'search term' | Search posts by keyword |
Custom Post Types and Taxonomies
<?php
// Register a custom post type: Product
function register_product_post_type() {
register_post_type('product', [
'labels' => [
'name' => 'Products',
'singular_name' => 'Product',
'add_new_item' => 'Add New Product',
],
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail', 'custom-fields', 'revisions'],
'menu_icon' => 'dashicons-cart',
'rewrite' => ['slug' => 'products'],
'show_in_rest' => true, // Enable Gutenberg and REST API
]);
}
add_action('init', 'register_product_post_type');
// Register a custom taxonomy: Brand
function register_brand_taxonomy() {
register_taxonomy('brand', 'product', [
'labels' => [
'name' => 'Brands',
'singular_name' => 'Brand',
],
'hierarchical' => true, // Like categories (false = like tags)
'rewrite' => ['slug' => 'brand'],
'show_in_rest' => true,
]);
}
add_action('init', 'register_brand_taxonomy');
?>
Hooks — Actions and Filters
Hooks are WordPress’s plugin system. Actions let you run custom code at specific points. Filters let you modify data before it’s displayed.
<?php
// === ACTIONS (do something) ===
// Enqueue scripts and styles
add_action('wp_enqueue_scripts', 'my_theme_assets');
// Run code when a post is published
add_action('publish_post', 'notify_on_publish', 10, 2);
// Add a custom dashboard widget
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget');
// === FILTERS (modify something) ===
// Modify the post title
add_filter('the_title', function($title) {
return '★ ' . $title;
});
// Change excerpt length
add_filter('excerpt_length', function() {
return 30;
});
// Add custom classes to the body tag
add_filter('body_class', function($classes) {
$classes[] = 'my-custom-class';
return $classes;
});
?>
REST API — Custom Endpoints
<?php
// Register custom REST endpoint: /myapp/v1/products/{id}
add_action('rest_api_init', function() {
register_rest_route('myapp/v1', '/products/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => function($data) {
$post = get_post($data['id']);
if (!$post || $post->post_type !== 'product') {
return new WP_Error('not_found', 'Product not found', ['status' => 404]);
}
return [
'id' => $post->ID,
'title' => $post->post_title,
'price' => get_post_meta($post->ID, 'price', true),
'brand' => wp_get_post_terms($post->ID, 'brand', ['fields' => 'names']),
'featured_image' => get_the_post_thumbnail_url($post->ID, 'full'),
];
},
'permission_callback' => '__return_true',
]);
});
?>
Theme Development Snippets
<?php
// functions.php — Standard theme setup
function my_theme_setup() {
// Enable features
add_theme_support('post-thumbnails');
add_theme_support('custom-logo');
add_theme_support('html5', ['search-form', 'comment-form', 'comment-list', 'gallery', 'caption']);
add_theme_support('title-tag');
// Register navigation menus
register_nav_menus([
'primary' => 'Primary Menu',
'footer' => 'Footer Menu',
]);
}
add_action('after_setup_theme', 'my_theme_setup');
// Load styles and scripts
function my_theme_assets() {
wp_enqueue_style('main-style', get_stylesheet_uri(), [], wp_get_theme()->get('Version'));
wp_enqueue_script(
'main-js',
get_template_directory_uri() . '/js/app.js',
['jquery'],
wp_get_theme()->get('Version'),
true // Load in footer
);
// Pass data from PHP to JavaScript
wp_localize_script('main-js', 'wpData', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'themeUrl' => get_template_directory_uri(),
]);
}
add_action('wp_enqueue_scripts', 'my_theme_assets');
?>
Security Hardening Snippets
<?php
// wp-config.php additions (place before "That's all, stop editing!")
// Force admin over HTTPS
define('FORCE_SSL_ADMIN', true);
// Disable file editing from dashboard
define('DISALLOW_FILE_EDIT', true);
// Increase memory limit for complex operations
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
// Disable plugin/theme installation from dashboard (for locked-down sites)
define('DISALLOW_FILE_MODS', true);
?>
Common Mistakes
- Assuming all features work identically — always check browser/version compatibility.
- Skipping documentation — reference docs exist for a reason; consult them.
- Not testing edge cases — your setup may differ from tutorials.
- Overlooking security — always validate inputs and follow best practices.
- Copy-pasting without understanding — type code yourself to build real knowledge.
What’s Next
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro