WordPress ships with thousands of functions, but a core set appears in almost every theme or plugin. This guide covers the most commonly used ones with short explanations and copy‑paste examples.
Core Utility Functions
These work with site-wide settings stored in the wp_options
table.
get_option( $option, $default )
Retrieve a stored option.
$site_name = get_option( 'blogname' );
echo $site_name; // e.g. My WordPress Site
update_option( $option, $value )
Update or create an option.
update_option( 'blogdescription', 'A new site tagline!' );
delete_option( $option )
Remove an option from the database.
delete_option( 'unused_setting' );
add_option( $option, $value )
Add a new option only if it doesn’t exist.
add_option( 'custom_theme_color', '#3498db' );
Post & Content Functions
Essential for fetching and displaying posts/pages.
the_content()
Display post content in the Loop.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title('<h2>', '</h2>');
the_content();
}
}
the_title()
/ get_the_title()
Echo or retrieve a title.
echo get_the_title( 42 ); // Title of post ID 42
get_post( $id )
Get a post object by ID.
$post = get_post( 42 );
echo $post->post_content;
get_posts( $args )
Retrieve multiple posts.
$recent = get_posts( [ 'numberposts' => 5 ] );
foreach ( $recent as $post ) {
echo $post->post_title . '<br>';
}
wp_insert_post( $args )
Insert a post programmatically.
$new_post = [
'post_title' => 'Hello World Programmatically',
'post_content' => 'This is content added via PHP!',
'post_status' => 'publish'
];
wp_insert_post( $new_post );
wp_update_post( $args )
/ wp_delete_post( $id )
Update or delete existing posts.
// Update
wp_update_post( [ 'ID' => 42, 'post_title' => 'Updated Title' ] );
// Delete
wp_delete_post( 42, true ); // true = force delete (skip trash)
User Functions
Useful for membership sites and custom applications.
wp_get_current_user()
Retrieve the logged-in user object.
$user = wp_get_current_user();
echo 'Welcome, ' . $user->display_name;
get_userdata( $id )
Fetch user info by ID.
$author = get_userdata( 1 );
echo $author->user_email;
wp_create_user( $username, $password, $email )
Create a new user account.
$user_id = wp_create_user( 'newuser', 'securePass123', '[email protected]' );
wp_update_user( $userdata )
/ wp_delete_user( $id )
Update or delete a user.
wp_update_user( [ 'ID' => 1, 'nickname' => 'AdminMaster' ] );
wp_delete_user( 123 ); // Consider reassigning posts first
Theme & Template Functions
Load standard layout parts and reusable partials.
get_header()
/ get_footer()
/ get_sidebar()
get_header();
// Page content here
get_sidebar();
get_footer();
get_template_part( $slug, $name )
Load a reusable template part (e.g., for different content types).
get_template_part( 'template-parts/content', 'page' );
locate_template( $template_names )
Find and include template files in the hierarchy.
$template = locate_template( 'custom-page.php' );
if ( $template ) {
include $template;
}
URL & Path Functions
Generate common links and paths.
home_url()
/ site_url()
echo home_url(); // e.g. https://example.com
echo site_url(); // WordPress root URL
get_permalink( $id )
echo get_permalink( 42 );
wp_login_url()
/ wp_logout_url()
echo '<a href="' . wp_login_url() . '">Login</a>';
echo '<a href="' . wp_logout_url() . '">Logout</a>';
⚡ Database & Query Functions
Use WP_Query
for most custom loops; drop to $wpdb
for direct SQL.
WP_Query
$query = new WP_Query( [
'post_type' => 'product',
'posts_per_page' => 10
] );
while ( $query->have_posts() ) {
$query->the_post();
the_title('<h3>', '</h3>');
}
wp_reset_postdata();
$wpdb
Direct database queries (use carefully and prepare statements where needed).
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID, post_title FROM {$wpdb->posts} WHERE post_status = 'publish' LIMIT 10"
);
foreach ( $results as $row ) {
echo $row->post_title . '<br>';
}
Media Functions
Work with images and attachments.
wp_get_attachment_url( $id )
echo wp_get_attachment_url( 123 );
wp_get_attachment_image( $id, $size )
echo wp_get_attachment_image( 123, 'thumbnail' );
media_handle_upload( $file_id, $post_id )
Handle uploads in custom forms (requires proper form markup and permissions).
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$attachment_id = media_handle_upload( 'my_file_input', 0 );
if ( is_wp_error( $attachment_id ) ) {
echo 'Upload failed.';
} else {
echo 'Uploaded! Attachment ID: ' . $attachment_id;
}
Security & Nonce
Nonces help protect against CSRF; capabilities gate actions by role.
wp_create_nonce( $action )
$nonce = wp_create_nonce( 'my_action' );
echo '<input type="hidden" name="_wpnonce" value="' . esc_attr( $nonce ) . '">';
wp_verify_nonce( $nonce, $action )
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'my_action' ) ) {
echo 'Safe!';
} else {
echo 'Invalid!';
}
current_user_can( $capability )
if ( current_user_can( 'manage_options' ) ) {
echo 'You are an admin!';
}
Hooks & Actions
Hooks are the backbone of WordPress customization.
add_action( $hook, $callback )
/ do_action( $hook )
add_action( 'wp_footer', 'my_custom_footer_message' );
function my_custom_footer_message() {
echo '<p>Custom footer text</p>';
}
add_filter( $hook, $callback )
/ apply_filters( $hook, $value )
add_filter( 'the_title', 'custom_title_prefix' );
function custom_title_prefix( $title ) {
return ' ' . $title;
}
❓ Frequently Asked Questions (FAQs)
What are WordPress functions?
WordPress functions are pre-built PHP tools that developers use to perform common tasks like fetching posts, displaying titles, querying the database, or handling users. They save you from writing repetitive code and ensure compatibility with the core. For example, instead of hand-written SQL to get posts, you can use get_posts()
or WP_Query
.
Which WordPress functions should beginners learn first?
Start with the essentials: the_content()
, the_title()
, get_option()
, get_posts()
, and get_header()
/get_footer()
. Mastering these gives you control over the Loop, templates, and site settings—enough to build basic themes and plugins.
Can I create my own WordPress functions?
Absolutely. Add custom functions to your theme’s functions.php
or a plugin. Combine them with hooks like add_action
and add_filter
to extend WordPress safely without editing core files. Example: a custom function that prints related posts, hooked to the_content
.
What’s the difference between the_title()
and get_the_title()
?
the_title()
echoes the title directly to the page, while get_the_title()
returns it as a string. The latter is useful when you need to manipulate the title before output.
$title = get_the_title();
echo strtoupper( $title ); // Manipulate before printing
When should I use $wpdb
instead of WP_Query
?
Prefer WP_Query
—it respects post types, taxonomies, and permissions. Use $wpdb
only for advanced cases (e.g., custom plugin tables). Always sanitize and prepare SQL to prevent injection vulnerabilities.
What are nonces in WordPress and why are they important?
Nonces (number used once) are security tokens used to verify intent and prevent CSRF attacks. Add a nonce with wp_create_nonce()
to forms or action links and verify it on submission with wp_verify_nonce()
.
Can I use these functions in plugins as well as themes?
Yes. In themes, functions often live in functions.php
and interact with templates. In plugins, wrap logic in hooks (e.g., add_action('init', 'my_plugin_boot')
) so code runs at the proper time and stays portable across sites.
What’s the best way to learn WordPress functions effectively?
Learn by building: create a child theme and a small plugin to practice. Read the WordPress Developer Handbook, inspect code execution with tools like Query Monitor, and review core/theme/plugin source to see best practices in context.
✅ Summary
Mastering these functions lets you build practically any WordPress feature from custom post types and membership flows to eCommerce enhancements. Bookmark this page or paste it into your project docs as a quick reference.