Functions

Actions & Filters — Hooks
hook
add_action()
Hooks a function to a specific action. The backbone of WP’s event-driven system.
add_action( ‘init’, ‘my_custom_init’ ); function my_custom_init() { // runs on init }
hook
add_filter()
Hooks a function to modify data before it’s used by WordPress or returned to the browser.
add_filter( ‘the_title’, ‘prefix_title’ ); function prefix_title( $title ) { return ‘★ ‘ . $title; }
hook
do_action()
Executes all functions attached to a given action hook.
do_action( ‘my_plugin_loaded’ );
hook
apply_filters()
Calls all functions attached to a filter hook and returns the modified value.
$value = apply_filters( ‘my_plugin_value’, $raw );
hook
remove_action()
Removes a function from a specified action hook.
remove_action( ‘wp_head’, ‘wp_generator’ );
hook
has_action()
Checks if any function has been registered for an action.
if ( has_action( ‘init’, ‘my_fn’ ) ) { // it’s registered }
Queries & Posts
query
WP_Query()
The main class for fetching posts with full control over args, tax, meta, and ordering.
$q = new WP_Query([ ‘post_type’ => ‘post’, ‘posts_per_page’ => 5, ]); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); } } wp_reset_postdata();
query
get_posts()
Returns an array of post objects matching given criteria. Simpler alternative to WP_Query.
$posts = get_posts([ ‘post_type’ => ‘page’, ‘numberposts’ => 10, ]);
query
get_post()
Retrieves post data given a post ID, object, or array.
$post = get_post( 42 ); echo $post->post_title;
query
get_the_ID()
Retrieves the ID of the current post in The Loop.
$id = get_the_ID();
query
wp_insert_post()
Insert or update a post in the database.
$id = wp_insert_post([ ‘post_title’ => ‘Hello’, ‘post_status’ => ‘publish’, ‘post_content’ => ‘World!’, ]);
query
wp_delete_post()
Trashes or permanently deletes a post, and all associated data.
wp_delete_post( $post_id, true );
Theme & Template
theme
get_template_part()
Loads a template part into a template. Enables modular, reusable template components.
get_template_part( ‘template-parts/content’, ‘single’ );
theme
wp_enqueue_script()
Safely enqueue a JavaScript file. Handles dependencies and version control.
wp_enqueue_script( ‘my-script’, get_template_directory_uri() . ‘/js/app.js’, [‘jquery’], ‘1.0’, true );
theme
wp_enqueue_style()
Safely enqueue a CSS stylesheet with dependency management.
wp_enqueue_style( ‘my-style’, get_stylesheet_uri() );
theme
add_theme_support()
Registers support for various theme features like post thumbnails, custom logo, etc.
add_theme_support( ‘post-thumbnails’ ); add_theme_support( ‘custom-logo’ );
theme
register_nav_menus()
Registers navigation menu locations for a theme.
register_nav_menus([ ‘primary’ => ‘Primary Menu’, ‘footer’ => ‘Footer Menu’, ]);
theme
the_post_thumbnail()
Displays the post’s featured image, with optional size arg.
the_post_thumbnail( ‘medium_large’ );
Utilities & Options
utility
get_option()
Retrieves an option value from the options table.
$email = get_option( ‘admin_email’, ‘[email protected]’ );
utility
update_option()
Updates the value of an option in the options table.
update_option( ‘my_key’, ‘my_value’ );
utility
wp_redirect()
Redirects the user to a specified URL. Always follow with exit.
wp_redirect( home_url(‘/thanks/’) ); exit;
utility
sanitize_text_field()
Checks and cleans a string from user input. Removes tags, extra whitespace, invalid UTF-8.
$clean = sanitize_text_field( $_POST[‘name’] );
utility
esc_html()
Escapes HTML for safe output. Prevents XSS by encoding special characters.
echo esc_html( $user_input );
utility
wp_nonce_field()
Generates a hidden nonce field for form security verification.
wp_nonce_field( ‘my_action’, ‘my_nonce_field’ );
Post Meta & Taxonomies
meta
get_post_meta()
Retrieves a post’s meta field value. Pass true as the third arg for a single value.
$val = get_post_meta( $post_id, ‘_my_key’, true );
meta
update_post_meta()
Updates a post meta field. Creates it if it doesn’t exist.
update_post_meta( $post_id, ‘_my_key’, $value );
meta
register_post_type()
Registers a custom post type with all its labels, capabilities, and settings.
register_post_type( ‘project’, [ ‘label’ => ‘Projects’, ‘public’ => true, ‘supports’ => [‘title’,’editor’], ]);
meta
register_taxonomy()
Registers a custom taxonomy and links it to one or more post types.
register_taxonomy( ‘genre’, ‘project’, [ ‘label’ => ‘Genres’, ‘public’ => true ] );
meta
get_the_terms()
Returns terms attached to the given post for a given taxonomy.
$genres = get_the_terms( $post_id, ‘genre’ );
meta
wp_set_post_terms()
Assigns terms to a post for a specified taxonomy.
wp_set_post_terms( $post_id, [12, 15], ‘genre’ );
Users & Authentication
user
get_current_user_id()
Returns the ID of the currently logged-in user, or 0 if not logged in.
$uid = get_current_user_id(); if ( $uid ) { /* logged in */ }
user
current_user_can()
Checks if the current user has a specific capability or role.
if ( current_user_can( ‘edit_posts’ ) ) { // user can edit }
user
wp_get_current_user()
Returns a WP_User object for the current user.
$user = wp_get_current_user(); echo $user->user_email;
user
get_user_meta()
Retrieves metadata for the specified user.
$city = get_user_meta( $user_id, ‘city’, true );

⚠ Pro Tips

  • Always use wp_reset_postdata() after a custom WP_Query loop to restore the global $post object.
  • Never trust user input — always sanitize on input (sanitize_*()) and escape on output (esc_*()).
  • Use nonces (wp_create_nonce() / check_admin_referer()) for all forms and AJAX calls.
  • Avoid direct database queries — use the WP functions and WP_Query first for caching and hook benefits.
  • Use wp_schedule_event() for recurring background tasks instead of cron jobs where possible.
Helpful Resources & Links