Skip to content

Instantly share code, notes, and snippets.

@ibrokemywp
ibrokemywp / enqueue-asset-on-translated-admin-page.php
Created January 3, 2015 12:34
Two ways to enqueue an asset on an admin sub-page when it has been translated.
/**
* Use the `$admin_page_hook[ $page_slug ]` in the `load-{page}` hook
*/
global $admin_page_hooks;
if ( !empty( $admin_page_hooks['myplugin-parent'] ) ) {
add_action( 'load-' . $admin_page_hooks['myplugin-parent'] . '_page_myplugin-subpage', 'enqueue_myplugin_assets' );
}
/**
* Or add `$admin_page_hook[ $page_slug ]` when checking the current
@ibrokemywp
ibrokemywp / enqueue-asset-on-admin-subpage.php
Created January 3, 2015 12:22
Two ways to enqueue an asset on an admin sub-page.
/**
* You can hook directly into the `load-{page}` hook for a sub-page with
* an action like this.
*/
add_action( 'load-myplugin-parent_page_myplugin-subpage', 'enqueue_myplugin_assets' );
/**
* Or you can hook into `admin_enqueue_scripts` and check the current
* screen to see if we're on the right page.
*/
@ibrokemywp
ibrokemywp / add-menu-page.php
Last active July 6, 2018 11:50
Adding a top-level admin page and sub-menu page.
// Create a top-level page with the slug `myplugin-parent`
add_menu_page(
__( 'My Plugin', 'my-plugin' ), // page title
__( 'MyPlugin-Parent', 'my-plugin' ), // menu title
'manage_options',
'myplugin-parent', // page slug
'myplugin_callback',
);
// Create a sub-page under the parent page
@ibrokemywp
ibrokemywp / reuse-dom-selections.js
Created November 22, 2014 14:25
Store dom selections in a var for reuse
/**
* You've probably done this before. I know I have.
*/
var text = $( '#myid .select a > element' ).text();
newtext = doSomeStuff( text );
$( '#myid .select a > element' ).text( newtext );
$( '#myid .select a > element' ).addClass( 'someClass' );
$( '#myid .select a > element' ).fadeIn();
/**
@ibrokemywp
ibrokemywp / do-selected-example.php
Last active August 29, 2015 14:10
The less tedious way to add a selected attribute
<select name="less_tedious">
<?php foreach( $options as $value => $label ) : ?>
<option value="<?php echo $value; ?>" <?php selected( $value, $current_value ); ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
@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>
@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 / 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 / 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 / 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',