WordPress Cheatsheet

1. General Information

  • WordPress Version:

bloginfo('version');
  

  • Site URL:

bloginfo('url');
  

  • Site Name:

bloginfo('name');
  

  • Admin Email:

get_option('admin_email');
  

  • Theme Directory:

bloginfo('template_directory');
  

2. Template Tags

  • Header:

get_header();
  

  • Footer:

get_footer();
  

  • Sidebar:

get_sidebar();
  

  • Search Form:

get_search_form();
  

  • Post Content:

the_content();
  

  • Post Title:

the_title();
  

  • Post Excerpt:

the_excerpt();
  

  • Post Thumbnail:

the_post_thumbnail('thumbnail');
  

  • Permalink:

the_permalink();
  

  • Author Name:

the_author();
  

  • Author Posts URL:

the_author_posts_link();
  

  • Post Date:

the_date();
  

3. Querying Posts

  • Custom Query:

$query = new WP_Query(array(
      'post_type' => 'post',
      'posts_per_page' => 5
  ));
  
  if ($query->have_posts()) : 
      while ($query->have_posts()) : $query->the_post();
          // Loop content
      endwhile;
      wp_reset_postdata();
  endif;
  

  • Loop through Posts:

if (have_posts()) : 
      while (have_posts()) : the_post();
          // Loop content
      endwhile;
  endif;
  

4. Working with Menus

  • Register a Menu:

function register_my_menu() {
      register_nav_menu('header-menu', __('Header Menu'));
  }
  add_action('init', 'register_my_menu');
  

  • Display a Menu:

wp_nav_menu(array(
      'theme_location' => 'header-menu'
  ));
  

5. Widgets

  • Register a Widget Area:

function my_widgets_init() {
      register_sidebar(array(
          'name' => 'Sidebar Widget Area',
          'id' => 'sidebar-1',
          'before_widget' => '
', 'after_widget' => '
', 'before_title' => '

', 'after_title' => '

', )); } add_action('widgets_init', 'my_widgets_init');

  • Display a Widget Area:

if (is_active_sidebar('sidebar-1')) {
      dynamic_sidebar('sidebar-1');
  }
  

6. Custom Post Types

  • Register a Custom Post Type:

function create_post_type() {
      register_post_type('movies',
          array(
              'labels' => array(
                  'name' => __('Movies'),
                  'singular_name' => __('Movie')
              ),
              'public' => true,
              'has_archive' => true,
              'rewrite' => array('slug' => 'movies'),
              'supports' => array('title', 'editor', 'thumbnail')
          )
      );
  }
  add_action('init', 'create_post_type');
  

7. Custom Taxonomies

  • Register a Custom Taxonomy:

function create_movie_taxonomy() {
      register_taxonomy(
          'genre',
          'movies',
          array(
              'label' => __('Genre'),
              'rewrite' => array('slug' => 'genre'),
              'hierarchical' => true,
          )
      );
  }
  add_action('init', 'create_movie_taxonomy');
  

8. Shortcodes

  • Create a Shortcode:

function my_shortcode_function() {
      return 'Hello, this is a shortcode!';
  }
  add_shortcode('myshortcode', 'my_shortcode_function');
  

  • Use a Shortcode:

echo do_shortcode('[myshortcode]');
  

9. Actions and Filters

  • Adding an Action:

function my_custom_action() {
      // Action logic
  }
  add_action('wp_footer', 'my_custom_action');
  

  • Adding a Filter:

function my_custom_filter($content) {
      return $content . ' Extra content added by filter!';
  }
  add_filter('the_content', 'my_custom_filter');
  

10. Enqueueing Scripts and Styles

  • Enqueue a Script:

function my_custom_scripts() {
      wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);
  }
  add_action('wp_enqueue_scripts', 'my_custom_scripts');
  

  • Enqueue a Style:

function my_custom_styles() {
      wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/custom-style.css');
  }
  add_action('wp_enqueue_scripts', 'my_custom_styles');
  

11. User Management

  • Get Current User:

$current_user = wp_get_current_user();
  echo 'Username: ' . $current_user->user_login;
  

  • Check User Role:

if (current_user_can('administrator')) {
      // Do something for admins
  }
  

12. Security

  • Escape Output:

echo esc_html($data);
  

  • Sanitize Input:

$sanitized_data = sanitize_text_field($_POST['data']);
  

  • Nonces:

wp_nonce_field('my_action', 'my_nonce');
  
  if (!isset($_POST['my_nonce']) || !wp_verify_nonce($_POST['my_nonce'], 'my_action')) {
      // Nonce check failed
  }
  

13. Useful Commands

  • WordPress CLI Install:

wp core install --url="example.com" --title="Example Site" --admin_user="admin" --admin_password="password" --admin_email="you@example.com"
  

  • List Plugins:

wp plugin list
  

  • Activate Plugin:

wp plugin activate plugin-name
  

  • Update WordPress Core:

wp core update
  

  • Update Plugins:

wp plugin update --all
  

14. Debugging

  • Enable Debugging:

Add to wp-config.php:

define('WP_DEBUG', true);
  define('WP_DEBUG_LOG', true);
  define('WP_DEBUG_DISPLAY', false);
  

  • View Debug Log:

error_log('Debug message');
  

15. Customizer API

  • Add Customizer Setting:

function my_custom_customizer($wp_customize) {
      $wp_customize->add_setting('my_setting', array(
          'default' => 'Default Value',
      ));
      $wp_customize->add_control('my_setting', array(
          'label' => __('My Setting', 'textdomain'),
          'section' => 'title_tagline',
          'type' => 'text',
      ));
  }
  add_action('customize_register', 'my_custom_customizer');
  

16. WooCommerce Integration

  • Check if WooCommerce is Active:

if (class_exists('WooCommerce')) {
      // WooCommerce is active
  }
  

  • Add Product to Cart:

WC()->cart->add_to_cart($product_id);
  

  • Get Cart Total:

echo WC()->cart->get_cart_total();
  

---

This WordPress cheatsheet should serve as a quick reference guide for WordPress developers. Whether you're working on themes, plugins, or custom solutions, these snippets will help you with common tasks and functions.