WordPress Cheatsheet
1. General Information
bloginfo('version');
bloginfo('url');
bloginfo('name');
get_option('admin_email');
bloginfo('template_directory');
2. Template Tags
get_header();
get_footer();
get_sidebar();
get_search_form();
the_content();
the_title();
the_excerpt();
the_post_thumbnail('thumbnail');
the_permalink();
the_author();
the_author_posts_link();
the_date();
3. Querying Posts
$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;
if (have_posts()) :
while (have_posts()) : the_post();
// Loop content
endwhile;
endif;
4. Working with Menus
function register_my_menu() {
register_nav_menu('header-menu', __('Header Menu'));
}
add_action('init', 'register_my_menu');
wp_nav_menu(array(
'theme_location' => 'header-menu'
));
5. Widgets
function my_widgets_init() {
register_sidebar(array(
'name' => 'Sidebar Widget Area',
'id' => 'sidebar-1',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
add_action('widgets_init', 'my_widgets_init');
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
function my_shortcode_function() {
return 'Hello, this is a shortcode!';
}
add_shortcode('myshortcode', 'my_shortcode_function');
echo do_shortcode('[myshortcode]');
9. Actions and Filters
function my_custom_action() {
// Action logic
}
add_action('wp_footer', 'my_custom_action');
function my_custom_filter($content) {
return $content . ' Extra content added by filter!';
}
add_filter('the_content', 'my_custom_filter');
10. Enqueueing Scripts and Styles
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');
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
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login;
if (current_user_can('administrator')) {
// Do something for admins
}
12. Security
echo esc_html($data);
$sanitized_data = sanitize_text_field($_POST['data']);
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
wp core install --url="example.com" --title="Example Site" --admin_user="admin" --admin_password="password" --admin_email="you@example.com"
wp plugin list
wp plugin activate plugin-name
wp core update
wp plugin update --all
14. Debugging
Add to wp-config.php
:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
error_log('Debug message');
15. Customizer API
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
}
WC()->cart->add_to_cart($product_id);
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.