Club 15CC

WordPress ~ 15CC Code

Task Code
WordPress Get Related Posts by Tag/Taxonomy Term in WordPress »
$taxonomy = 'post_tags';  // or whatever you want

$query = NULL;
$terms = wp_get_post_terms($post->ID, $taxonomy);
if ($terms) {  
    // Get the term ids into an array for the query args
    $term_ids = array();  
    foreach($terms as $individual_term) $term_ids[] = $individual_term->term_id;  

    $args=array(  
        'post_type' => get_post_type(),
        'tax_query' => array(
            array(
                'taxonomy'  => $taxonomy,
                'field'     => 'id',
                'terms'     => $term_ids,
                'operator'  => 'IN'
            ) 
        ),
        'post__not_in' => array($post->ID),  
        'showposts' => $count, // Number of related posts that will be shown.  
        'caller_get_posts' => 1  
    );  

    $query = WP_Query($args)
}
WordPress Get the URL for an Attachment File in WordPress »
$link = wp_get_attachment_url($attach_id);
WordPress Determine Whether 404 Was *Actually* a Private Page View in WordPress » WordPress WP_Query’s get_posts() method takes a...
// Place in 404.php and load subtemplate accordingly
global $wp_query, $wpdb;
if ( count($wpdb->get_results($wp_query->request)) > 0 )
    echo 'private post';
WordPress Give “Subscribers” Access to Private Posts & Pages » Anywhere should do for this. I’ve got mine in a...
$sub_role = get_role( 'subscriber' ); 
$sub_role->add_cap( 'read_private_posts' ); 
$sub_role->add_cap( 'read_private_pages' ); 
WordPress Add a Form Parameter to Media Uploads in WordPress »
////
// Say you want to get a form parameter to pass to the media uploader so you can test for it in the $_POST on your wp_handle_upload filter…
////

/**
 * Add checkbox to set autocreate on upload
 */
function fifteenccgal_autocreate_post_on_image_upload_form_tweaks()
{
    ?>
    <label style="display: inline-block; padding: 5px 8px; border: 3px solid lightgrey; border-radius: 1em">
        <input  type="checkbox" 
                id="fifcc_autocreate" 
                name="fifcc_autocreate" 
                value="1" 
                <?php echo ((FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE == 1) ? 'checked="checked"' : '')?>" />
         Auto-create a Photo Post for each uploaded image?
    </label><br/><br/>
    <script type="text/javascript">
        jQuery(function ($) {
            $('#fifcc_autocreate').change(function (e) {
                uploader.settings.multipart_params.fifcc_autocreate = ($('#fifcc_autocreate:checked').val() ? 1 : 0);
            })
        });
    </script>
    <?php
}
add_action('post-upload-ui', 'fifteenccgal_autocreate_post_on_image_upload_form_tweaks');

////
// You might also wish to set a default value on load. WordPress provides a suitable hook…
////

/**
 * Initialise the value to reflect the checkbox default
 */
function fifteenccgal_autocreate_post_on_image_upload_post_params_init($params)
{
    $params['fifcc_autocreate'] = FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE;
    return $params;
}
add_filter('upload_post_params', 'fifteenccgal_autocreate_post_on_image_upload_post_params_init');



WordPress Add IPTC Keywords and Category Metadata to Uploaded Images in WordPress »
function fifteenccgal_add_additional_image_meta( $meta, $file, $sourceImageType ) 
{
    $image_file_types = apply_filters( 
        'wp_read_image_metadata_types', 
        array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM )
    );

    // Check that it's an image type
    if (  in_array( $sourceImageType, $image_file_types ) 
          && function_exists('iptcparse')  ) {

        getimagesize($file, $info);
        $iptc = iptcparse($info['APP13']);
        $meta['category'] = $iptc['2#015'];
        $meta['keywords'] = $iptc['2#025'];
    }

    return $meta;
}
add_filter('wp_read_image_metadata', 'fifteenccgal_add_additional_image_meta', 10, 3);
WordPress Check User Capabilities on a WordPress Post »
$ptype = get_post_type_object($post_type);   // eg. 'post', 'page' 

if ( !current_user_can( $ptype->cap->edit_posts ) ) { 
    wp_die( __( 'You are not allowed to create posts or drafts on this site.' ) ); 
}
WordPress Retrieve a Post From It’s Title » Not entirely obvious from what’s our...
$sql = sprintf( "SELECT * from {$wpdb->posts} WHERE post_title='%s' AND ( post_status <> 'trash' AND post_status <> 'inherit' AND post_status <> 'auto-draft' )", addslashes( $title ) ); 

$matching_post = get_object_vars( $wpdb->get_row($sql) );
WordPress Display Custom Posts on the Tag Page »
// Say you want to grab the posts for a custom post type for the current tag whose page is being viewed…

$page_tag_name = single_tag_title( '', false ); 
$tag = array_pop(get_tags( array('name__like' => $page_tag_name) )); 
query_posts(array( 'post_type' => array($my_custom_post_type), 'tag' => $tag->slug ));

// ...Then continue with The Loop as normal
WordPress Convert Post Date Format to More Human Friendly Version in WordPress »
// Converts MySQL Datetime formats…
// (source: /wp-includes/functions.php)

mysql2date( get_option('date_format'), $your_post->post_date );
WordPress Set Post Parent for Media Set via a Custom Fields » See this post for more details......
/** 
 * Set post parent on podcast custom field attachments 
 */ 
function nf_set_post_parent_for_podcast_custom_fields($post_id, $post) 
{ 
    // Safety check for plugin api function 
    if (!function_exists('slt_cf_field_value')) return ''; 

    // The loop was for when I had multiple fields to attach. It's unnecessary as it stands now... 
    foreach ( array( 'podcast' ) as $key ) { 
        // Get the attachment id stored in the field. 
        if ($attach_id = slt_cf_field_value( $key, 'post', $post_id )) { 
            $attachment = get_post( $attach_id ); 
            $attachment->post_parent = $post_id; 
            wp_update_post( $attachment ); 
        } 
    } 
} 
add_action('save_post', 'nf_set_post_parent_for_podcast_custom_fields', 10, 2);
WordPress Get All Attachments to a Post »
$attachments = get_children('post_type=attachment&post_mime_type=image&post_parent='. $post_id);
WordPress Enqueuing Javascript File Dependencies Correctly »
// Placeholder plugin dependency if requried
$depends = array('jquery', 'jquery-form');
if (get_option('mc_use_placeholders') == 'on' || true){
    $depends[] = ('jquery-placeholder');
    wp_register_script('jquery-placeholder', MCSF_URL.'js/jquery.placeholder.min.js', array('jquery'));
}
WordPress Add custom image thumbnail sizes to the media library’s insert size choices »
/**
 * Add intermediate image sizes to media gallery modal dialog
 */
function nf_add_image_sizes_to_editor( $form_fields, $post ) {
  if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
    return $form_fields;

    if ( is_array($imagedata['sizes']) ) {
        foreach ( $imagedata['sizes'] as $size => $val ) {
            if ( $size == 'teaser-image' ) {// put your thumbnail image name(s) here
                $css_id = "image-size-{$size}-{$post->ID}";
                $pretty_size = ucwords(str_replace('-', ' ' , $size));
                $html .= '';
                $html .= ''.$pretty_size.'';
                $html .= ' '.sprintf( __("(%d × %d)"), $val['width'], $val['height'] ). '';
                $html .= '';
            }
        }
    }

$form_fields['image-size']['html'] .= $html;
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'nf_add_image_sizes_to_editor', 100, 2 );