Skip to content

Instantly share code, notes, and snippets.

@scofennell
scofennell / is_pano.php
Last active August 29, 2015 14:00
WordPress function to detect if is a photo is panoramic.
<?php
/**
* Given an attachment_id and a definition of panoramia, detect if a photo is panoramic
*
* @param int $attachment_id the attachment_id for the image
* @param mixed $min_ratio the aspect ratio that defines panoramia
* @return boolean returns true if image is panoramic, otherwise returns false
*/
function sjf_deh_is_pano( $attachment_id, $min_ratio='2' ){
@scofennell
scofennell / sjf_deh_alter_main_query.php
Last active August 29, 2015 14:01
WordPress function to alter the main query to grab posts of the attachment type
<?php
/**
* Alter the main query to grab posts of the attachment type
*
* @param object $query The WP_Query for the current page view
*/
function sjf_deh_alter_main_query( $query ) {
// we don't want to alter the query in wp-admin
if ( is_admin() ) { return; }
@scofennell
scofennell / tags_for_attachments.php
Last active August 29, 2015 14:01
Register WordPress tags for posts of the attachment post type, register other custom taxonomies as well
<?php
/**
* Register WordPress tags for posts of the attachment post type, register other custom taxonomies as well
*/
function sjf_deh_taxonomies() {
// allow tags to apply to posts of the attachment post_type
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
// Add new taxonomy, make it hierarchical (like categories)
@scofennell
scofennell / sjf_deh_the_bg_image_styles.php
Last active August 29, 2015 14:01
WordPress function to return a block of CSS for displaying and positioning a full-page background image.
<?php
/**
* Returns a block of CSS for displaying and positioning a full-page background image
*
* @return string|bool A block of CSS for displaying and positioning a full-page background image or false on failure
*/
function sjf_deh_the_bg_image_styles(){
// this function won't work if $post is not set (which would be the case on a 404 page, for example)
$post = get_post();
alert('hello world');
@scofennell
scofennell / sjf_deh_get_image_src.php
Last active August 29, 2015 14:01
WordPress function to
<?php
/**
* Get the current post featured image, with a call to wp_is_mobile to grab a smaller size for mobile users.
*
* This function uses wp_is_mobile(), which is not intended for public use on the front end -- buyer beware.
* @return string|bool Returns the src for the featured image, or false on failure
*/
function sjf_deh_get_image_src(){
// $post must work in order to grab the featured image
@scofennell
scofennell / sjf_def_og_image.php
Created May 21, 2014 18:36
Function to add featured image to open graph tags
function sjf_def_og_image(){
echo'
<meta property="og:image" content="'.sjf_deh_get_image_src().'" />
';
}
add_action('wp_head', 'sjf_def_og_image');
@scofennell
scofennell / sjf_deh_add_custom_meta_box.php
Last active August 29, 2015 14:01
WordPress Custom Meta Boxes for Attachments
<?php
/**
* Add the Meta Box to attachments
*/
function sjf_deh_add_custom_meta_box() {
add_meta_box(
'custom_meta_box', // id
'Art Direction', // title
'sjf_deh_show_custom_meta_box', // callback
@scofennell
scofennell / sjf_deh_meta_fields.php
Last active August 29, 2015 14:01
Return a multi-dimensional array for managing WordPress custom post meta
<?php
/**
* Return a multi-dimensional array for managing custom post meta
*
* @return array An multi-dimensional associative array for drawing & saving post meta fields.
*/
function sjf_deh_meta_fields() {
$sjf_deh_meta_fields = array(
@scofennell
scofennell / sjf_deh_show_custom_meta_box.php
Last active August 29, 2015 14:01
Show custom meta boxes for attachments
<?php
/**
* Draw a custom meta box for attachments
*/
function sjf_deh_show_custom_meta_box() {
// affect the current post
global $post;