Skip to content

WordPress Cheatsheet

https://wphierarchy.com/

Theme Handbook

Code Reference

Core Functions - lists most of the core functions, excluding Template Tags.

Template Tags

Conditional Tags

https://codex.wordpress.org/Conditional_Tags

Check what conditions a page has, archive, custom posts etc

is_page( 42 ) 
is_page( 'contact' )

Site

get_site_url()

Can add an optional path parameter.

<?php echo get_site_url(); ?>

# output:
# https://www.example.com

get_home_url()

Home URL link with optional path appended.

<?php echo get_home_url(); ?>

get_bloginfo()

Get the site title.

<?php $site_title = get_bloginfo( 'name' ); ?>

Pages and Posts

get_page_link()

Get the permalink url for a page.

<a href="<?php echo get_page_link(8); ?>">
  Contact
</a>

Or during a loop, leave blank for the current page.

<a href="<?php echo get_page_link(); ?>">
  <?php echo esc_html( get_the_title() ); ?>
</a>

get_page_by_path

<?php
// Set the page slug
$page_slug = 'your-page-slug';

// Get the page by slug
$page = get_page_by_path($page_slug);

// Check if the page exists
if ($page) {
    // Get the page URL
    $page_url = get_page_link($page->ID);
   
    // Output the page URL
    echo $page_url;
} else {
    // Page not found
    echo 'Page not found.';
}
?>

Example to show a warning message to logged in Admin or Editor if the page does not exist.

<?php
  $page_slug = 'contact';
  $page = get_page_by_path($page_slug);

  if ($page) {
    $page_url = get_page_link($page->ID);
  }

  $user = wp_get_current_user();
  $user_roles = $user->roles;

  if($page): 
?>

    <a href="<?php echo $page_url; ?>" class="underline">
      Contact
    </a>

<?php elseif (in_array('editor', $user_roles) || in_array('administrator', $user_roles)): ?>

    <p class="p-5 text-red-500 outline outline-dotted outline-red-500 outline-2">
      <span class="font-medium">The page '<?php echo $page_slug; ?>' does not exist.</span><br />
      This button will not be linked until this page is created.<br />
      <span class="text-sm text-neutral-500">Only admins and editors can see this warning.</span>
    </p>

<?php endif; ?>

get_the_title()

Get the title of a page or post while outside of the loop. See comments about whether to escape the output or not.

To display the title safely:
<?php echo esc_html( get_the_title() ); ?>

Or if the title needs to include HTML tags:
<?php echo wp_kses_post( get_the_title() ); ?>

the_title()

Display or retrieve the current post title with optional markup. e.g. add h1 markup either side of the output.

<?php the_title( '<h1 class="text-2xl">', '</h1>' ); ?>

get_the_ID()

Retrieve the ID of the current item in the WordPress Loop. When used on the blog homepage it will return the first post, even if outside of the loop. In that case, use get_queried_object_id().

<?php echo get_the_ID(); ?>

the_excerpt()

If the excerpt is empty, WordPress automatically creates an excerpt using the first 55 words of the post. See documentation to change the length, and remove the default [...].

<?php the_excerpt(); ?>

get_the_date()

Set the format under Settings > General.

<?php echo get_the_date(); ?>

// Or control the format independently from the WordPress settings
<?php echo get_the_date( 'D M j' ); ?>

// Or an example when used with metadata
<time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>">
  <?php echo esc_html( get_the_date() ); ?>
</time>

the_content()

Display the post content.

<?php the_content(); ?>

Blog homepage

The template file home.php is used to render the blog posts index, whether it is being used as the front page or on a separate static page. If home.php does not exist, WordPress will use index.php.

Enable thumbnail / feature image support. Add to functions.php.

add_theme_support( 'post-thumbnails' );

We can add editable fields, e.g. for the page heading and intro, with ACF by using the location rule Show this field group if **Page Type** is equal to **Posts Page** .

Example blog posts index page.

<h1>
  <?php the_field('page_heading', get_queried_object_id() ); ?>
</h1>

<div>
  <?php the_field( 'intro', get_queried_object_id() ); ?>
</div>

<?php 
  if ( have_posts() ) : 

    while ( have_posts() ) :
      the_post();

      $myID = get_the_ID();

      // Get the thumbnail image, in this case the "full_2560"
      $img = wp_get_attachment_image_src( get_post_thumbnail_id( $myID ), "full_2560" );
      $url = $img[0];
      $width = $img[1];
      $height = $img[2];
    ?>

      <a href="<?php echo get_permalink(); ?>" class="">

        <img 
          alt="<?php echo get_the_title($myID); ?>"
          src="<?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_2560' )); ?>" 
          srcset="
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_640' )); ?> 640w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_750' )); ?> 750w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_828' )); ?> 828w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_1080' )); ?> 1080w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_1200' )); ?> 1200w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_1400' )); ?> 1400w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_1920' )); ?> 1920w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_2048' )); ?> 2048w,
            <?php echo esc_url(get_the_post_thumbnail_url($myID, 'full_2560' )); ?> 2560w
          "
          sizes="(max-width: 767px) 100vw, (max-width: 1535px) calc(100vw - 80px), calc(100vw - 164px)"
          class="object-cover w-full h-full"
          width="<?php echo $width; ?>"
          height="<?php echo $height; ?>"
        />

        <h2 class="text-2xl">
          <?php the_title(); ?>
        </h2>

        <p class="text-sm">
          <?php the_date(); ?>
        </p>

      </a>

    <?php
    endwhile;

  endif;
?>

Categories

get_categories()

Retrieves a list of category objects. The function only returns categories that have been used by at least one post.

Example to list all the categories in alphabetical order.

<?php
  $categories = get_categories( array(
      'orderby' => 'name',
      'order'   => 'ASC'
  ) );

  foreach ( $categories as $category ) {
    ?>
      <a href="<?php echo get_term_link( $category, 'category' ); ?>" class="">
        <?php echo $category->name; ?>
      </a>
    <?php
  }
?>

Or list them as a bullet point list, wp_list_cats​().

Single post

wp_get_post_categories()

Retrieve the list of categories for a single post.

This example outputs a comma separated list.

<?php
  $postterms = get_the_terms( get_the_ID(), 'category' );

  $mycount = 0;
  foreach ( $postterms as $term ) {
    $mycount++;
?>
    <a href="<?php echo get_term_link( $term, 'category' ); ?>" class="">
      <?php echo $term->name; ?><?php if( $mycount != sizeof($postterms) ){ echo ","; } ?>
    </a>
<?php
  }
?>

Or the same as above can be output using the_category() but with less control, i.e. less ability to further customise the output.

Customiser

The following removes all the customiser sections. Customise as needed (pun intended). Note the priority of 50 which is needed if removing the nav_menus.

function projectX_customize_register( $wp_customize ) {
	$wp_customize->remove_section('title_tagline');
	$wp_customize->remove_section('colors');
	$wp_customize->remove_section('header_image');
	$wp_customize->remove_section('background_image');
	$wp_customize->remove_panel('nav_menus');
	$wp_customize->remove_panel('widgets');
	$wp_customize->remove_section('static_front_page');	 
	$wp_customize->remove_section('custom_css');
}
add_action( 'customize_register', 'projectX_customize_register', 50 );

Shortcode

<?php echo do_shortcode( '[hf_form slug="contact"]' ); ?>

Create own shortcode: https://pagely.com/blog/creating-custom-shortcodes/

Error log

https://wordpress.org/support/article/debugging-in-wordpress/

// Enable WP_DEBUG mode, default is false
define( 'WP_DEBUG', true );

// Enable Debug logging, defaults to /wp-content/debug.log
define( 'WP_DEBUG_LOG', true );

// Or define a location
define( 'WP_DEBUG_LOG', '/tmp/wp-errors.log' );

// Disable display of errors and warnings, default is true
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

Memory Limit

By default, WordPress will use 40MB of memory in PHP.

WP_MEMORY_LIMIT increases PHP Memory for the entire WordPress application.

WP_MAX_MEMORY_LIMIT will override the above setting and defines the maximum memory limit available when in the administration back end. This lets you increase the memory usage for admin users only. The default is 256M (256 megabytes of memory) or the original memory_limit php.ini value is higher.

define( 'WP_MEMORY_LIMIT', '256M' );
define( 'WP_MAX_MEMORY_LIMIT', '512M' );

Include

<?php include dirname(__FILE__).'/../template-parts/call-to-action.php'; ?>

Image from theme folder

<img src="<?php echo get_stylesheet_directory_uri(); ?>/img/example.svg" alt="" width="" height="">

Admin email notifications

Disable email notifications for when WordPress automatically updates core, plugins or themes.

/* Disable email notifications for core updates */
add_filter('auto_core_update_send_email', 'wpb_stop_update_emails', 10, 4);
 
function wpb_stop_update_emails($send, $type, $core_update, $result)
{
 if (!empty($type) && $type == 'success') {
   return false;
 }
 return true;
}
 
/* Disable email notifications for plugin updates */
add_filter('auto_plugin_update_send_email', '__return_false');
 
 /* Disable email notifications for theme updates */
add_filter('auto_theme_update_send_email', '__return_false');

credit

Admin customisations

/**
 * Remove dashboard widgets and welcome
 */

function remove_dashboard_meta() {
  remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');	
  remove_meta_box('dashboard_plugins', 'dashboard', 'normal');	
  remove_meta_box('dashboard_primary', 'dashboard', 'normal'); 	
  remove_meta_box('dashboard_secondary', 'dashboard', 'normal'); 	
  remove_meta_box('dashboard_quick_press', 'dashboard', 'side'); 	
  remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');	
  remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); 	
  remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); 	
  remove_meta_box('dashboard_activity', 'dashboard', 'normal'); 
}
add_action('admin_init', 'remove_dashboard_meta');

remove_action('welcome_panel', 'wp_welcome_panel');

/**
 * Remove links from Admin Bar
 */
function remove_admin_bar_links() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');          // Remove the Wordpress logo
    $wp_admin_bar->remove_menu('about');            // Remove the about Wordpress link
    $wp_admin_bar->remove_menu('wporg');            // Remove the Wordpress.org link
    $wp_admin_bar->remove_menu('documentation');    // Remove the Wordpress documentation link
    $wp_admin_bar->remove_menu('support-forums');   // Remove the support forums link
    $wp_admin_bar->remove_menu('feedback');         // Remove the feedback link
    // $wp_admin_bar->remove_menu('site-name');        // Remove the site name menu
    $wp_admin_bar->remove_menu('view-site');        // Remove the view site link
    $wp_admin_bar->remove_menu('updates');          // Remove the updates link
    $wp_admin_bar->remove_menu('comments');         // Remove the comments link
    $wp_admin_bar->remove_menu('new-content');      // Remove the content link
    // $wp_admin_bar->remove_menu('w3tc');             // If you use w3 total cache remove the performance link
    // $wp_admin_bar->remove_menu('my-account');       // Remove the user details tab
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

/**
 * Remove Comments in admin menu
 */
function post_remove () {
   remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'post_remove');

/**
 * Remove embed
 */
function my_deregister_scripts(){
  wp_deregister_script( 'wp-embed' );
}
add_action( 'wp_footer', 'my_deregister_scripts' );

/**
 * Remove emoji
 */
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

/**
 * Set image quality (default is 82)
 */
add_filter('jpeg_quality', function($arg){return 95;});

/**
 * Remove block library css if not using Gutenberg
 */
function smartwp_remove_wp_block_library_css(){
  wp_dequeue_style( 'wp-block-library' );
  wp_dequeue_style( 'wp-block-library-theme' );
  wp_dequeue_style( 'wc-blocks-style' ); // Remove WooCommerce block CSS
} 
add_action( 'wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css', 100 );

credit

Remove dashboard widgets and welcome credit and credit

Remove links from Admin Bar credit

Remove wp-embed credit

Remove Emoji credit

Remove Block Library CSS credit

Customise login page

Add company logo. Add following code to functions.php.

Upload logo to media and change url in below code. Logo space is 300 x 100 px but the logo style is set to contain.

function wpb_login_logo() { ?>
  <style type="text/css">
      .login {
        background-color: #4d4d4d;
      }
      
      #login h1 a, .login h1 a {
      background-image: url(<?php echo get_site_url(); ?>/wp-content/uploads/2021/11/logo.png);
      height:100px;
      width:300px;
      background-size: contain;
      background-repeat: no-repeat;
      padding-bottom: 10px;
      }
  </style>
<?php }
add_action( 'login_enqueue_scripts', 'wpb_login_logo' );

function wpb_login_logo_url() {
  return home_url();
}
add_filter( 'login_headerurl', 'wpb_login_logo_url' );

function wpb_login_logo_url_title() {
  return get_bloginfo('name');
}
add_filter( 'login_headertext', 'wpb_login_logo_url_title' );