WP: add_role() remove_role()
// Only need to load a page once to get this into db, then comment out (or place in theme/plugin activation)
// WP Codex: http://codex.wordpress.org/Roles_and_Capabilities
$result = add_role(
'basic_contributor',
__( 'Basic Contributor' ),
array(
'read' => true, // true allows this capability
'edit_posts' => true,
'delete_posts' => false, // Use false to explicitly deny
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
} else {
echo 'Oh... the basic_contributor role already exists.';
}
// Or remove a role
remove_role( 'basic_contributor' );
WP: Shortcode
function callback( $atts, $content = '' ) {
extract( shortcode_atts( array(
'text' => '',
'link' => '',
), $atts ) );
return '';
}
add_shortcode( 'name', 'callback' );
jQuery: Resize Images Proportionally
// TODO: rework this
$('.container img').each(function() {
var maxWidth = 100, // Max width for the image
maxHeight = 100, // Max height for the image
ratio = 0, // Used for aspect ratio
w = $(this).width(), // Current image width
h = $(this).height(); // Current image height
// Check if the current width is larger than the max
if(width > maxWidth){
ratio = maxWidth / width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(this).css("height", height * ratio); // Scale height based on ratio
h = h * ratio; // Reset height to match scaled image
w = w * ratio; // Reset width to match scaled image
}
// Check if current height is larger than max
if(height > maxHeight){
ratio = maxHeight / height; // get ratio for scaling image
$(this).css("height", maxHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
w = w * ratio; // Reset width to match scaled image
h = h * ratio; // Reset height to match scaled image
}
});
$('.container img').each(function() {
var mw = 100, // Max width
mh = 100, // Max height
r = 1, // Aspect ratio
w = $(this).width(), // Current width
h = $(this).height(); // Current height
// set ratio
r = w > mw ? mw/w : mh/h;
// Check if current width is larger than the max
if( w > mw ){
// r = maxWidth / width; // get ratio for scaling image
$(this).css("width", mw); // Set new width
$(this).css("height", h * r); // Scale height based on ratio
}
// Check if current height is larger than max
if( h > mh ){
// r = maxHeight / height; // get ratio for scaling image
$(this).css("width", w * r); // Scale width based on ratio
$(this).css("height", mh); // Set new height
}
w = w * ratio; // Reset width to match scaled image
h = h * ratio; // Reset height to match scaled image
});
jQuery UI: Sortable (HTML)
See also: jQuery UI: Sortable
<h1>Sorting A Table With jQuery UI</h1>
<table id="sort" class="grid" title="Kurt Vonnegut novels">
<thead>
<tr><th class="index">No.</th><th>Year</th><th>Title</th><th>Grade</th></tr>
</thead>
<tbody>
<tr><td class="index">1</td><td>1969</td><td>Slaughterhouse-Five</td><td>A+</td></tr>
<tr><td class="index">2</td><td>1952</td><td>Player Piano</td><td>B</td></tr>
<tr><td class="index">3</td><td>1963</td><td>Cat's Cradle</td><td>A+</td></tr>
<tr><td class="index">4</td><td>1973</td><td>Breakfast of Champions</td><td>C</td></tr>
<tr><td class="index">5</td><td>1965</td><td>God Bless You, Mr. Rosewater</td><td>A</td></tr>
</tbody>
</table>
jQuery UI: Sortable
From: http://jsfiddle.net/pmw57/tzYbU/205/
HTML that goes with it: jQuery UI: Sortable (HTML)
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index) {
$(this).width($originals.eq(index).width())
});
return $helper;
},
updateIndex = function(e, ui) {
$('td.index', ui.item.parent()).each(function (i) {
$(this).html(i + 1);
});
};
$("#sort tbody").sortable({
helper: fixHelperModified,
stop: updateIndex
}).disableSelection();
The above is now outdated. Move along.
Copyright
// anywhere
printf( 'Copyright © 2010-%s COMPANY_NAME. All Rights Reserved', date( 'Y' ) );
// in WP/CP with translation/textdomain
printf( __( 'Copyright © 2010-%s COMPANY_NAME. All Rights Reserved', 'textdomain' ), date( 'Y' ) );
// can also make future year(s) conditional
$year = date('Y');
$copyright_years = $year == '2014' ? $year : '2014-'.$year;
Compass.app: @include transition
.scss {
@include transition(all .2s linear);
}
// becomes
.css {
-webkit-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-o-transition: all 0.2s linear;
transition: all 0.2s linear;
}
No longer using Compass. Going with Prepros (on Mac) with Autoprefixer enabled (so no @include needed).