Last active
July 6, 2018 07:57
-
-
Save rutcreate/b9e7fd88b05227424be6251478b7ad16 to your computer and use it in GitHub Desktop.
WordPress cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check page is category. | |
*/ | |
if ( is_category() && $term = get_queried_object() ) { | |
print_r($term); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>›</span>', | |
'prev_text' => '<span>‹</span> Prev', | |
'type' => 'array', | |
) ); | |
if ( $links ) { | |
echo '<ul class="pagination"><li>'. implode('</li><li>', $links) .'</li></ul>'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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