Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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