WordPress PHP Functions Cheat Sheet

Boost your WordPress development with this organized reference guide. Quickly find and implement common functions for The Loop, theme development, conditional logic, and more.

The Loop Functions

The core of WordPress for displaying post content. These are used inside the main post loop.

  • have_posts() - Checks if there are any posts to display in the current query.
    <?php if ( have_posts() ) : ?>
  • the_post() - Sets up the current post's data, making it available for use.
    <?php while ( have_posts() ) : the_post(); ?>
  • the_ID() - Displays the numeric ID of the current post.
    <p>Post ID: <?php the_ID(); ?></p>
  • get_the_ID() - Returns the numeric ID of the current post for use in a variable.
    <?php $current_post_id = get_the_ID(); ?>
  • the_title() - Displays the title of the current post.
    <h2><?php the_title(); ?></h2>
  • get_the_title() - Returns the title of the current post for use in a variable.
    <?php $post_title = get_the_title(); ?>
  • the_content() - Displays the full content of the current post.
    <div class="entry-content"><?php the_content(); ?></div>
  • the_excerpt() - Displays a brief summary (excerpt) of the current post.
    <div class="entry-summary"><?php the_excerpt(); ?></div>
  • the_permalink() - Displays the full URL of the current post.
    <a href="<?php the_permalink(); ?>">Read More</a>
  • the_author() - Displays the display name of the current post's author.
    <p>By <?php the_author(); ?></p>
  • the_author_posts_link() - Displays the author's name linked to their post archive.
    <div class="post-author"><?php the_author_posts_link(); ?></div>
  • the_date() - Displays the publication date of the current post.
    <span class="post-date"><?php the_date('F j, Y'); ?></span>
  • the_category() - Displays the categories of the current post as a link.
    <div class="post-categories"><?php the_category(', '); ?></div>
  • the_tags() - Displays the tags of the current post as links.
    <div class="post-tags"><?php the_tags('Tags: ', ', ', ''); ?></div>
  • rewind_posts() - Resets the loop, allowing you to run it again on the same page.
    <?php rewind_posts(); ?>

Theme & Template Functions

Functions for building and managing theme structure and assets.

  • get_header() - Includes the `header.php` template file.
    <?php get_header(); ?>
  • get_footer() - Includes the `footer.php` template file.
    <?php get_footer(); ?>
  • get_sidebar() - Includes the `sidebar.php` template file.
    <?php get_sidebar('primary'); // Includes sidebar-primary.php if it exists ?>
  • get_template_part() - Loads a specified template part file.
    <?php get_template_part('template-parts/content', get_post_type()); ?>
  • get_search_form() - Includes the `searchform.php` template file.
    <?php get_search_form(); ?>
  • wp_head() - Fires the `wp_head` action hook; essential for plugins.
    <?php wp_head(); ?>
  • wp_footer() - Fires the `wp_footer` action hook; essential for plugins.
    <?php wp_footer(); ?>
  • body_class() - Displays CSS classes for the `` tag, based on the current page.
    <body <?php body_class(); ?>>
  • wp_enqueue_style() - Safely adds a CSS stylesheet to the page.
    function my_theme_styles() { wp_enqueue_style( 'my-custom-style', get_template_directory_uri() . '/css/custom.css' ); }
    add_action( 'wp_enqueue_scripts', 'my_theme_styles' );
  • wp_enqueue_script() - Safely adds a JavaScript file to the page.
    function my_theme_scripts() { wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), null, true ); }
    add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
  • get_stylesheet_directory_uri() - Retrieves the URI of the active theme's (child theme) directory.
    <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png">
  • get_template_directory_uri() - Retrieves the URI of the parent theme's directory.
    <script src="<?php echo get_template_directory_uri(); ?>/js/parent-theme.js"></script>
  • add_theme_support() - Registers theme support for a given feature (e.g., post thumbnails, title tag).
    function theme_setup() { add_theme_support( 'post-thumbnails' ); }
    add_action( 'after_setup_theme', 'theme_setup' );
  • register_nav_menus() - Registers multiple navigation menu locations for a theme.
    function register_my_menus() { register_nav_menus( array( 'primary' => __( 'Primary Menu' ), 'footer' => __( 'Footer Menu' ) ) ); }
    add_action( 'init', 'register_my_menus' );
  • wp_nav_menu() - Displays a navigation menu created in the admin dashboard.
    <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
  • register_sidebar() - Registers a widget area (sidebar) for the theme.
    function my_register_sidebars() { register_sidebar(array('id' => 'primary', 'name' => 'Primary Sidebar')); }
    add_action( 'widgets_init', 'my_register_sidebars' );
  • dynamic_sidebar() - Displays the widgets assigned to a specific sidebar.
    <?php if ( is_active_sidebar( 'primary' ) ) : dynamic_sidebar( 'primary' ); endif; ?>

Post & Page Functions

Functions for retrieving and managing data about posts, pages, and custom post types.

  • get_post() - Retrieves post data for a given post ID.
    <?php $my_post = get_post(123); echo $my_post->post_title; ?>
  • get_posts() - Retrieves an array of post objects based on specified criteria.
    <?php $args = array( 'posts_per_page' => 5 ); $latest_posts = get_posts( $args ); ?>
  • wp_insert_post() - Inserts a new post into the database.
    <?php $new_post = array('post_title' => 'My New Post', 'post_content' => 'This is the content.', 'post_status' => 'publish');
    $post_id = wp_insert_post($new_post); ?>
  • has_post_thumbnail() - Checks if the current post has a featured image.
    <?php if ( has_post_thumbnail() ) { // Display the thumbnail } ?>
  • the_post_thumbnail() - Displays the featured image of the current post.
    <?php the_post_thumbnail('thumbnail'); // Options: thumbnail, medium, large, full ?>
  • get_the_post_thumbnail_url() - Returns the URL of the featured image.
    <?php $image_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); ?>
  • get_post_meta() - Retrieves a custom field (post meta) value for a post.
    <?php $custom_field = get_post_meta(get_the_ID(), 'my_custom_field_key', true); ?>
  • update_post_meta() - Updates a custom field value for a post.
    <?php update_post_meta(123, 'my_custom_field_key', 'New Value'); ?>
  • get_permalink() - Retrieves the permalink for a post by its ID.
    <?php echo get_permalink(123); ?>
  • next_post_link() - Displays a link to the next post on a single post page.
    <div class="nav-next"><?php next_post_link( '%link', 'Next Post »' ); ?></div>
  • previous_post_link() - Displays a link to the previous post on a single post page.
    <div class="nav-previous"><?php previous_post_link( '%link', '« Previous Post' ); ?></div>

Conditional Tags

Functions that check for a specific condition and return TRUE or FALSE. Often used with `if` statements.

  • is_front_page() - Checks if the main front page is being displayed.
    <?php if ( is_front_page() ) { /* Front page specific code */ } ?>
  • is_home() - Checks if the blog posts index is being displayed.
    <?php if ( is_home() && ! is_front_page() ) { /* Blog page specific code */ } ?>
  • is_single() - Checks if a single post of any post type is being displayed.
    <?php if ( is_single() ) { /* Single post specific code */ } ?>
  • is_page() - Checks if a specific static Page is being displayed.
    <?php if ( is_page('contact-us') ) { /* Contact page specific code */ } ?>
  • is_category() - Checks if a category archive page is being displayed.
    <?php if ( is_category('news') ) { /* News category specific code */ } ?>
  • is_tag() - Checks if a tag archive page is being displayed.
    <?php if ( is_tag() ) { /* Any tag archive page */ } ?>
  • is_author() - Checks if an author archive page is being displayed.
    <?php if ( is_author(4) ) { /* Code for author with ID 4 */ } ?>
  • is_archive() - Checks if any type of archive page is being displayed.
    <?php if ( is_archive() ) { /* General archive page code */ } ?>
  • is_search() - Checks if a search results page is being displayed.
    <?php if ( is_search() ) { /* Search results page code */ } ?>
  • is_404() - Checks if a "404 Not Found" error page is being displayed.
    <?php if ( is_404() ) { /* 404 page specific code */ } ?>
  • is_admin() - Checks if the administration dashboard is being displayed.
    <?php if ( ! is_admin() ) { /* Code that runs only on the front-end */ } ?>
  • has_tag() - Checks if the current post has a specific tag.
    <?php if ( has_tag('featured') ) { /* Featured post code */ } ?>
  • has_category() - Checks if the current post has a specific category.
    <?php if ( has_category('tutorials') ) { /* Tutorial post code */ } ?>
  • is_active_sidebar() - Checks if a sidebar has active widgets.
    <?php if ( is_active_sidebar( 'primary' ) ) { /* Code to display sidebar container */ } ?>

User Functions

Functions for managing and retrieving user data.

  • is_user_logged_in() - Checks if the current visitor is a logged-in user.
    <?php if ( is_user_logged_in() ) { echo 'Welcome back!'; } else { echo 'Welcome, guest!'; } ?>
  • wp_get_current_user() - Retrieves the complete user object for the current user.
    <?php $current_user = wp_get_current_user(); echo 'Username: ' . $current_user->user_login; ?>
  • get_current_user_id() - Returns the numeric ID of the current user.
    <?php $user_id = get_current_user_id(); ?>
  • current_user_can() - Checks if the current user has a specific capability or role.
    <?php if ( current_user_can('edit_posts') ) { echo 'You can edit posts.'; } ?>
  • get_avatar() - Displays the avatar for a user.
    <?php echo get_avatar( get_the_author_meta('ID'), 64 ); // 64 is the size in pixels ?>
  • get_the_author_meta() - Retrieves a specific field from the author's profile.
    <?php echo 'Author bio: ' . get_the_author_meta('description'); ?>

URL Functions

Functions for generating and retrieving URLs within WordPress.

  • home_url() - Retrieves the URL for the site's home page.
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Go Home</a>
  • site_url() - Retrieves the URL for the current site (where WordPress core files are located).
    <?php echo site_url(); ?>
  • admin_url() - Retrieves the URL to the admin area.
    <a href="<?php echo esc_url( admin_url( 'profile.php' ) ); ?>">Edit Profile</a>
  • includes_url() - Retrieves the URL to the includes directory.
    <?php echo includes_url(); ?>
  • content_url() - Retrieves the URL to the content directory (`wp-content`).
    <?php echo content_url(); ?>
  • plugins_url() - Retrieves the URL to the plugins directory.
    <img src="<?php echo plugins_url( 'my-plugin/images/icon.png' ); ?>">

Database & Options Functions

Functions for interacting directly with the WordPress database and the options table.

  • get_option() - Retrieves a value from the `wp_options` table.
    <?php $blog_description = get_option( 'blogdescription' ); ?>
  • update_option() - Updates a value in the `wp_options` table.
    <?php update_option( 'my_custom_setting', 'new_value' ); ?>
  • get_bloginfo() - Retrieves information about the current site, like its name or URL.
    <h1><?php bloginfo('name'); ?></h1>
  • $wpdb - The global WordPress database object for custom database queries.
    global $wpdb;
    $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");

Hooks: Actions and Filters

Functions that allow you to "hook" your custom code into WordPress execution at specific points.

  • add_action() - Hooks a function to a specific action.
    // In functions.php:
    function my_custom_function() { /* do something */ }
    add_action('wp_head', 'my_custom_function');
  • add_filter() - Hooks a function to a specific filter to modify data.
    // In functions.php:
    function change_excerpt_length($length) { return 20; } // 20 words
    add_filter('excerpt_length', 'change_excerpt_length');
  • do_action() - Executes all functions hooked to a specific action.
    <?php do_action('my_custom_action_before_footer'); ?>
  • apply_filters() - Applies all filters hooked to a specific tag.
    // In a plugin or theme:
    $title = apply_filters('my_custom_title', 'Default Title');