Skip to content

Instantly share code, notes, and snippets.

@caratage
caratage / query.php
Created November 27, 2012 04:22
check if post has image attachment
$attachments = get_children (
array (
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image'
)
);
if ( $attachments ) {
// do conditional stuff here
@caratage
caratage / functions.php
Created November 27, 2012 04:23
Detach and re-attach media files
add_filter("manage_upload_columns", 'upload_columns');
add_action("manage_media_custom_column", 'media_custom_columns', 0, 2);
function upload_columns($columns) {
unset($columns['parent']);
$columns['better_parent'] = "Parent";
return $columns;
@caratage
caratage / query.php
Created November 27, 2012 04:32
Add class to first post of loop
while ( have_posts() ) : the_post();
if( $wp_query->current_post ==0 && !is_paged() ) {
$myclass = "fist-post";
}
endwhile;
@caratage
caratage / gist:4152402
Created November 27, 2012 04:34
Get the first word of a sentence in PHP
$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test
@caratage
caratage / functions.php
Created November 28, 2012 02:39
Remove shortcodes from the_content
// remove all shortcodes
add_filter('the_content', 'strip_shortcodes');
// remove specific shortcode
remove_shortcode('gallery');
@caratage
caratage / functions.php
Created November 28, 2012 03:01
YouTube HTML5 oembed for WordPress
wp_oembed_add_provider( '#http://(www\.)?youtube.com/watch.*#i', 'http://www.youtube.com/oembed?iframe=1', true );
@caratage
caratage / meta-box.html
Created November 28, 2012 04:48
Send to editor - jQuery
<div class="upload_field"
<input type="text" name="upload_one" id="upload_one" class="upload" value="" />
<input type="button" class="upload-button" value="Upload Image" />
</div>
<div class="upload_field"
<input type="text" name="upload_two" id="upload_two" class="upload" value="" />
<input type="button" class="upload-button" value="Upload Image" />
</div>
@caratage
caratage / functions.php
Created November 28, 2012 10:26
Format your Filesize in WordPress
// size calculation
$docsize = @filesize( get_attached_file ( $attachment->ID ) );
// show the attachment,
echo $filename . ' &mdash; ' . $docsize;
// echoes something like attachment.zip — 13143
// use size_format() instead. start with size calculation
$docsize = @filesize( get_attached_file( $attachment->ID ) );
if (FALSE === $docsize)
@caratage
caratage / functions.php
Created November 28, 2012 13:00
Remove fields from Media Uploader overlay
// edit fields in media upload area
add_filter('attachment_fields_to_edit', 'remove_media_upload_fields', 10000, 2);
function remove_media_upload_fields( $form_fields, $post ) {
// remove unnecessary fields
unset( $form_fields['image-size'] );
unset( $form_fields['post_excerpt'] );
unset( $form_fields['post_content'] );
unset( $form_fields['url'] );
unset( $form_fields['image_url'] );
@caratage
caratage / functions.php
Created November 28, 2012 13:04
Removing Admin Menu items
function remove_menu_pages() {
remove_menu_page('link-manager.php');
remove_menu_page('edit-comments.php');
}
add_action( 'admin_init', 'remove_menu_pages' );
// http://www.netmagazine.com/tutorials/customise-wordpress-admin-area