Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Last active July 6, 2018 07:57
Show Gist options
  • Save rutcreate/b9e7fd88b05227424be6251478b7ad16 to your computer and use it in GitHub Desktop.
Save rutcreate/b9e7fd88b05227424be6251478b7ad16 to your computer and use it in GitHub Desktop.
WordPress cheat sheet
<?php
/**
* Check page is category.
*/
if ( is_category() && $term = get_queried_object() ) {
print_r($term);
}
<?php
/**
* Security form.
*/
// Render nonce in form.
wp_nonce_field( basename( __FILE__ ), 'custom_nonce' );
// Check security.
if ( ! wp_verify_nonce( $_POST['custom_nonce'], basename( __FILE__ ) ) ) {
return '<p>Failed.</p>';
}
if ( ! current_user_can( 'manage_options' ) )
return '<p>Failed.</p>';
}
<?php
function themename_admin_menu() {
$parent_slug = 'themename-options';
add_menu_page( 'Theme Options', 'General Options', 'manage_options', $parent_slug, 'themename_menu_options_general' );
add_submenu_page( $parent_slug, 'Submenu', 'Submenu Options', 'manage_options', $parent_slug .'-submenu', 'themename_menu_options_submenu' );
// Change first submenu title to 'General'.
global $submenu;
if ( isset( $submenu[ $parent_slug ] ) && count( $submenu[ $parent_slug ] ) > 0 ) {
$submenu[ $parent_slug ][0][0] = 'General';
}
}
add_action( 'admin_menu', 'themename_admin_menu' );
<?php
/**
* Get posts by custom taxonomy.
*/
$posts = get_posts( array(
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'id', // id, slug
'terms' => array(1, 2, 3), // Can be string ('1,2,3'), int (1), or array
),
),
) );
/**
* Get posts by specific posts.
*/
$post_ids = array(1, 2, 3);
$posts = get_posts( array(
'post__in' => $post_ids,
'orderby' => 'post__in',
'posts_per_page' => count( $post_ids ),
) );
/**
* Save post.
*/
$post = get_post( 1 );
$post->post_title = $post->post_title . ' (Updated)';
wp_update_post( $post );
<?php
/**
* Get posts in template.
*/
query_posts( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'id', // id, slug
'terms' => array(1, 2, 3), // Can be string ('1,2,3'), int (1), or array
),
),
) );
while ( have_posts() ):
the_post(); // This is important to call the_*() especially the_excerpt().
// Do your thing.
endwhile;
/**
* Set variable to template file.
*/
set_query_var( 'variable_name', 'value go here' );
// archive.php
echo $variable_name; // print 'value go here'
/**
* Output pagination.
*/
function the_pagination( $query = null ) {
if ( !$query ) {
global $wp_query;
$query = $wp_query;
}
$big = 999999999;
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $query->max_num_pages,
'next_text' => 'Next <span>&rsaquo;</span>',
'prev_text' => '<span>&lsaquo;</span> Prev',
'type' => 'array',
) );
if ( $links ) {
echo '<ul class="pagination"><li>'. implode('</li><li>', $links) .'</li></ul>';
}
}
<?php
/**
* Get categories by custom taxonomy.
*/
$categories = get_categories( array(
'taxonomy' => 'custom_taxonomy',
'hide_empty' => false,
) );
/**
* Get single term object.
*/
$term = get_term( $id_or_slug );
/**
* Get categories in post.
*/
$categories = get_the_category( $post_id );
/**
* Get custom taxonomny's terms in post.
*/
$terms = get_the_terms( $post_id, 'custom_taxonomy' ); // default taxonomy is 'category'.
/**
* Remove terms from post.
*/
$terms = array(1, 2, 3);
$terms = '1,2,3';
$terms = 1;
wp_remove_object_terms( $post_id, $terms, 'custom_taxonomy' );
/**
* Add categories to post.
*/
$categories = array(1, 2, 3);
$categories = '1,2,3';
$categories = 1;
wp_set_post_categories( $post_id, $categories );
/**
* Add custom taxonomy's terms to post or custom post.
*/
$categories = array(1, 2, 3);
$categories = '1,2,3';
$categories = 1;
wp_set_object_terms( $post_id, $categories );
<?php
/**
* Setup theme.
*/
function your_theme_name_setup() {
// Make theme available for translation.
load_theme_textdomain( 'your_theme_text_domain' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// Auto generate title tag.
add_theme_support( 'title-tag' );
// Enable support for Post Thumbnails on posts and pages.
add_theme_support( 'post-thumbnails' );
add_image_size( 'your-prefix-cover', 960, 720, array( '1', '' ) );
add_image_size( 'your-prefix-large', 540, 405, array( '1', '' ) );
add_image_size( 'your-prefix-thumbnail', 380, 285, array( '1', '' ) );
// This theme uses wp_nav_menu() in two locations.
add_theme_support( 'menus' );
register_nav_menus( array(
'top' => __( 'Top Menu', 'your_theme_text_domain' ),
'footer' => __( 'Footer Menu', 'your_theme_text_domain' ),
) );
add_theme_support( 'widgets' );
}
add_action( 'after_setup_theme', 'your_theme_name_setup' );
<?php
/**
* Prevent from script run individually.
*/
if ( ! defined( 'ABSPATH' ) ) die( 'Nothing for you here.');
/**
* Include file in theme.
*/
require_once get_stylesheet_directory() . '/path/to/file.php';
/**
* Include file in base theme.
*/
require_once get_template_directory() . '/path/to/file.php';
/**
* Output content from wp_editor().
*/
wpautop( $string_from_wp_editor );
/**
* Print sql.
*/
function dump_request( $input ) {
var_dump($input);
return $input;
}
add_filter( 'posts_request', 'dump_request' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment