Skip to content

Instantly share code, notes, and snippets.

@ibrokemywp
ibrokemywp / get_current_screen().php
Last active August 29, 2015 14:09
get_current_screen() returns null in ajax requests
/**
* Let's say you're parsing query args in the admin so
* that you can apply some custom filters for your
* custom post type list table.
*/
add_filter( 'parse_query', 'filter_my_post_type' );
function filter_my_post_type( $query ) {
/**
* Oh good, you've put in that is_admin() check
@ibrokemywp
ibrokemywp / nested_filters.php
Created November 14, 2014 01:05
Applying the_content filter inside the_content filter causes lots of tears
/**
* I'm going to add some content from one post to
* the content of another post. Yay for me.
*/
add_filter( 'the_content', 'my_little_content_addition' );
function my_little_content_addition( $content ) {
if ( is_the_right_content() ) {
/**
@ibrokemywp
ibrokemywp / textdomain-back-compat.php
Created November 16, 2014 13:25
Load a .mo file for the old textdomain if one exists
/**
* Load a .mo file for the old textdomain if one exists
*
* If you have to change your textdomain to comply with upcoming
* plugin and theme repository standards, this function will check to
* see if an old translation file exists and load it if it does, so
* that people don't lose their translations.
*
* h/t: https://github.com/10up/grunt-wp-plugin/issues/21#issuecomment-62003284
*/
@ibrokemywp
ibrokemywp / dont-sort-like-this.php
Last active August 29, 2015 14:09
Don't sort your custom post type via pre_get_posts like this
/**
* Don't sort your custom post type admin lists this way because it will
* override other sorting being executed on this list.
*/
add_action( 'pre_get_posts', array( $this, 'admin_order_posts' ) );
public function admin_order_posts( $query ) {
if( ( is_admin() && $query->is_admin ) && $query->get( 'post_type' ) == 'my_custom_post_type' ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
@ibrokemywp
ibrokemywp / do-sort-like-this.php
Last active August 29, 2015 14:09
Sort your custom post type via pre_get_posts like this
/**
* Check before sorting your custom post type admin lists so you don't
* override other changes.
*/
add_action( 'pre_get_posts', array( $this, 'admin_order_posts' ) );
public function admin_order_posts( $query ) {
if( ( is_admin() && $query->is_admin ) && $query->get( 'post_type' ) == 'my_custom_post_type' ) {
// This prevents other orderby options from breaking.
@ibrokemywp
ibrokemywp / serialize-options.php
Last active October 25, 2020 10:23
Store all of your options in a single serialized array in the database
/**
* Register a single setting to store all of your options in the database
*/
register_setting( 'myslug_option_group', 'myslug', 'myslug_sanitize_options_array' );
/**
* Define each of your settings
*/
add_settings_field(
'name',
@ibrokemywp
ibrokemywp / taxonomy-description.php
Created November 16, 2014 16:03
Show taxonomy descriptions on archive pages
<?php if ( ( is_category() || is_tag() || is_tax() ) && $summary = term_description() ) : ?>
<div class="term-summary">
<?php echo $summary; ?>
</div>
<?php endif; ?>
@ibrokemywp
ibrokemywp / variable-filters.php
Last active August 29, 2015 14:10
Use variable filter names when retrieving your settings
/**
* Set up a global for storing settings in your product
* (if you're using classes, store it in a controller)
*/
global $myprefix_settings;
/**
* Set up your own wrapper for accessing settings in
* your product
*/
@ibrokemywp
ibrokemywp / wp_reset_query.php
Created November 22, 2014 11:00
Reset the loop after you've hijacked it
/**
* Go ahead. Do your fancy shit
*/
$your_query = new WP_query( 'post_type' => 'your_cool_post_type' );
while( $your_query->have_posts() ) {
$your_query->the_post();
/**
* Look at your cool template function.
* That looks just like my template function.
@ibrokemywp
ibrokemywp / dont-selected-example.php
Last active August 29, 2015 14:10
The more tedious way to add a selected attribute
<select name="tedious">
<?php foreach( $options as $value => $label ) : ?>
<option value="<?php echo $value; ?>" <?php if ( $value === $current_value ) { echo 'selected="selected"'; } ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>