WordPress at Your Fingertips

Theme Headers style.css

style.css Headers
/**
 * Theme Name:  My theme (required)
 * Template:    The name of the parent theme. E.g. Twenty Seventeen
 * Description: A short description of the theme.
 * 
 * Theme URI:   Subject URL. E.g. http://wordpress.org/themes/twenty
 * Author:      Kama
 * Author URI:  https://wp-kama.com
 *
 * Tags:        black, brown, orange
 * Text Domain: Subject translation domain. E.g. twentythirteen
 *
 * License:     License. E.g. GNU General Public License v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Version:     1.0
 */

Theme Files (include)

Theme files include functions

Theme Files (hierarchy) /themes/THEME/

Theme Files Hierarchy
  • style.cssTheme styles file (required)
  • index.phpAny page without a template file (required)
  • front-page.phpFront (Home) page
  • home.phpPosts page (or Home page)
  • functions.phpA special file for php functions (code)
  • 404.phpPage "not found"
  • comments.phpComments Template (part)
  • header.phpSite Header Template (part)
  • searchform.phpSearch Form Template (part)
  • sidebar.phpSidebar Template (part)
  • footer.phpSite Footer Template (part)
  • single.phpPost page post_type = post
  • single-POST_TYPE.phpPost page post_type = POST_TYPE
  • single-POST_TYPE-POST_NAME.phpPost page with POST_NAME and POST_TYPE
  • singular.phpPost of any post type
  • page.phpPage post_type = page
  • page-POST_NAME.phpPage post_name = POST_NAME
  • page-ID.phpPage ID = ID
  • attachment.phpAny attachment page
  • image.phpImage attachment page
  • archive.phpPage of any archive
  • archive-POST_TYPE.phpPage of post type archive
  • search.phpSearch page
  • category.phpAny Category page
  • category-SLUG.phpCategory page with slug = SLUG
  • category-ID.phpCategory page with term_id = id
  • tag.phpAny Tag page
  • tag-SLUG.phpTag page with slug = SLUG
  • tag-ID.phpTag page with term_id = id
  • taxonomy.phpAny custom taxonomy element page
  • taxonomy-TAXONOMY.phpPage of any term of TAXONOMY taxonomy
  • taxonomy-TAXONOMY-SLUG.phpPage of SLUG term of TAXONOMY taxonomy
  • author.phpPage of Author posts

A post template from any file:

<?php
/*
Template Name: My page template
Template Post Type: post, page, product
*/

// … template file code
Read more about post templates

Site Information bloginfo

  • bloginfo()Displays information about the site.
  • get_bloginfo()Gets information about the site.
  •  
  • <?php bloginfo('name'); ?>Site name.
  • <?php bloginfo('description'); ?>Short description of the site.
  • <?php bloginfo('template_url'); ?>Theme URL: get_template_directory().
  • <?php bloginfo('template_directory'); ?>Same as template_url.
  • <?php bloginfo('stylesheet_directory'); ?>Theme URL: get_stylesheet_directory_uri().
  • <?php bloginfo('stylesheet_url'); ?>Theme file style.css URL: get_stylesheet_uri().
  • <?php bloginfo('charset'); ?>The site's encoding: UTF-8.
  • <?php bloginfo('html_type'); ?>Content-Type of the page: text/html.
  • <?php bloginfo('language'); ?>Site locale (language): RU.
  • <?php bloginfo('version'); ?>WordPress version: 5.5.3.
  • <?php bloginfo('rss2_url '); ?>URL of the feed: /feed.
  • <?php bloginfo('comments_rss2_url'); ?>URL of the comments feed: /comments/feed.
  • <?php bloginfo('admin_email'); ?>E-mail of Admin.
  •  
  • <?php bloginfo('pingback_url'); ?>Notification URL to the xmlrpc.php file.
  • <?php bloginfo('rdf_url'); ?>RDF/RSS 1.0 feed URL (/feed/rfd).
  • <?php bloginfo('rss_url'); ?>RSS 0.92 feed URL (/feed/rss`).
  • <?php bloginfo('atom_url '); ?>Atom feed URL (/feed/atom).
  • <?php bloginfo('url'); ?>Home page URL. Alias home_url().
  • <?php bloginfo('wpurl'); ?>Admin panel URL. Alias site_url().

Comment Loop

The Loop

The Loop3 ways to Create Loop in WordPress
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <!-- Post output, loop functions: the_title(), etc. -->
<?php endwhile; else: ?>
	No posts.
<?php endif; ?>
<?php if ( have_posts() ){ while ( have_posts() ){ the_post(); ?>
	<!-- Post output, loop functions: the_title(), etc. -->
<?php } } else { ?>
	No posts.
<?php } ?>
<?php while ( have_posts() ){ the_post(); ?>
	<!-- Post output, loop functions: the_title(), etc. -->
<?php } ?>
<?php if ( ! have_posts() ){ ?>
	No posts.
<?php } ?>
<?php
global $post;

$myposts = get_posts([ 
	'numberposts' => 5,
	'offset'      => 1,
	'category'    => 1
]);

if( $myposts ){
	foreach( $myposts as $post ){
		setup_postdata( $post );
		?>
		<!-- Post output, loop functions: the_title(), etc. -->
		<?php 
	}
} else {
	// No posts found
}

wp_reset_postdata(); // Reset $post
?>
<?php		
global $post;

$query = new WP_Query( [
	'posts_per_page' => 5,
	'orderby'        => 'comment_count',
] );

if ( $query->have_posts() ) {
	while ( $query->have_posts() ) {
		$query->the_post();
		?>
		<!-- Post output, loop functions: the_title(), etc. -->
		<?php 
	}
} else {
	// No posts found
}

wp_reset_postdata(); // Reset $post
?>

Template Tags

All Template Tags

Plugin Headers

Plugin Headersreadme.txt file
<?php
	
/**
 * Plugin Name: My Plugin
 * Description: Description of the plugin (140 characters)
 * Plugin URI:  URL to info about plugin 
 * Author URI:  https://wp-kama.com
 * Author:      Kama
 *
 * Text Domain: Translation ID. E.g. my-plugin
 * Domain Path: Path to MO file (relative to plugin folder)
 *
 * Requires PHP: 5.4
 * Requires at least: 2.5
 * 
 * License:     GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * Network:     true - activates the plugin for the whole network
 * Version:     1.0
 */

// Plugin code starts here

Plugin

Plugin Creation

uninstall.php

<?php 
if( ! defined('WP_UNINSTALL_PLUGIN') ) exit;

// the check passed successfully. Starting from here remove all plugin data (options, db tables etc.).
delete_option( 'plug_option' );

Script and Style

Script & Style functions
add_action( 'wp_enqueue_scripts', 'add_my_scripts' );    // Front
add_action( 'admin_enqueue_scripts', 'add_my_scripts' ); // Admin
add_action( 'login_enqueue_scripts', 'add_my_scripts' ); // wp-login.php
function add_my_scripts(){

	if ( ! wp_script_is( 'my-script', 'enqueued' ) ) {
		// The `my-script` is not added to the queue
	}
	
	if ( ! wp_style_is( 'my-style', 'registered' ) ) {
		// Styles `my-script` is not registered
	}
	
	wp_enqueue_script( 'my-script', 'src', ['deps'], '1.0', 'in_footer' );
	wp_enqueue_style(  'my-style',  'src', ['deps'], '1.0', 'all' );
	
	wp_enqueue_style( 'theme-style', get_stylesheet_uri() ); // theme style.css
	
	wp_localize_script( 'my-script', 'myajax', [
		'ajaxurl' => admin_url( 'admin-ajax.php' )
	] );
	
	wp_script_add_data( 'my-script', 'conditional', 'lt IE 9' );
	wp_style_add_data(  'my-style',  'conditional', 'lt IE 9' )
	
	wp_add_inline_script( 'my-scripts', 'alert("Hello!");' );
	wp_add_inline_style( 'my-style', '
		.mycolor{
			background: #fff;
		}
	');
	
	wp_deregister_script( 'my-script' );
	wp_deregister_style( 'my-style' );
}

Hooks Functions

How hooks work in WordPressHooks execution order
function my_filter_function( $str ){
	return 'Hello '. $str;
}

// Let's attach a function to the filter
add_filter( 'my_filter', 'my_filter_function' );

// Call the filter
echo apply_filters( 'my_filter', 'John' ); //> Hello John
// Create a function for the event
function my_action_function( $text ){
	echo 'The `my_action` event has been triggered just now.';
}

// Let's attach the function to the my_action event
add_action( 'my_action', 'my_action_function' );

// Action call
do_action( 'my_action' ); //> The `my_action` event has been triggered just now.

Localization (translation)

Localization FunctionsTranslations in WordPress

Conditional tags (post types and queries)

if( is_single() ){
	// single post type page
}

Conditional tags (others)

All Conditional Tags
if( is_user_logged_in() ){
	// user is authorized
}

WP CLI

Core:

// Download WordPress 
wp core download --locale=ru_RU
// Generate wp-config.php:
wp core config --dbname=NAME --dbuser=USER --dbpass=PASS --dbprefix=wp_
// Create DB (based on wp-config.php)
wp db create
// Install WP to created DB
wp core install --url=example.com --title=Example --admin_user=supervisor [email protected] --admin_password=strongpassword

Post:

// List posts:
wp post list
// Edit post:
wp post edit 1
// Post update:
wp post update 1 --post_title="Your New title..."
// Create posts:
wp post create --post_status=publish --post_title="Second Post" --edit

Post meta:

// See all metas of post 18:
wp post meta list 18
// Get post meta value:
wp post meta get 18 meta_name
// Delete post meta by key:
wp post meta delete 18 meta_name

DB:

// Create DB dump
wp db export - --add-drop-table --default-character-set=utf8mb4 | gzip > ./db_backup-$(date +%Y-%m-%d-%H%M%S).sql.gz
// Insert data from DB dump to DB
wp db import db_backup-2022-01-20.sql
// Login WordPress db:
wp db cli
// Run SQL Query:
wp db query "SELECT user_login, ID FROM wp_users;"
// Optimize db:
wp db optimize

Update:

// Update WordPress
wp core update
// Update all plugins:
wp plugin update --all

Themes & Plugins

// List plugins:
wp plugin list
// Search plugin:
wp plugin search yoast
// Install plugin:
wp plugin install yoast
// List installed themes:
wp theme list
// Install theme:
wp theme install twentyone
// Activate theme:
wp theme activate twentyone

What to add to the cheat sheet?
No comments
    Log In