WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Aug 25 2011

How to Get Original Image from a Thumbnail Image in WordPress

Recently, in order to dummy proof a metabox that accepts a file upload via Media Library from a user accidentally selecting something other than Original Image like Small, Medium, or Large, I wrote a validation script to check and correct this.
Media Library: Insert Into Posts
This function will change a thumbnail URL (http://domain.com/wp-content/uploads/2011/08/example_2a-158x300.jpg) to the original URL (http://domain.com/wp-content/uploads/2011/08/example_2a.jpg). To use this function, simply:

[php]$url = wps_get_original_image( $url );[/php]

Here is the script that can be added to your functions.php:

[php]<!--?php function wps_get_original_image( $url ) { global $_wp_additional_image_sizes; $new_url = $url; // Get All Image Sizes $builtin_sizes = array( 'large' =--> array(
'width' => get_option('large_size_w'),
'height' => get_option('large_size_h')
),
'medium' => array(
'width' => get_option('medium_size_w'),
'height' => get_option('medium_size_h')
),
'thumbnail' => array(
'width' => get_option('thumbnail_size_w'),
'height' => get_option('thumbnail_size_h')
)
);

$image_sizes = array_merge( $builtin_sizes, $_wp_additional_image_sizes );

//$url = 'http://webendev.com/review/scheumanndental/wp-content/uploads/2011/08/example_2a-300x158.jpg';

// Parse URL
$info = pathinfo( $url );

// Check to see if it is a post-thumbnail: assuming size < 999x999 so -999x999.jpg = 12chars $pos = strrpos($info['basename'], '-', -1); $length = strlen( $info['basename'] ); if ( ( $length - $pos ) > 12 )
return $url;

// Check to see if image is indeed a thumbnail and not example-2.jpg

// Get image size extensions, e.g. -200x200
// Get image size width only extensions, e.g., -200x
// Get image size height only extensions, e.g., x200.
$image_exts = array();
$image_exts_width = array();
$image_exts_height = array();
foreach ($image_sizes as $image_size) {
$image_exts[] = '-'.$image_size['width'].'x'.$image_size['height'];
$image_exts_width[] = '-'.$image_size['width'].'x';
$image_exts_height[] = 'x'.$image_size['height'];
}

// Cycle through image size extensions, e.g. -200x200
foreach ( $image_exts as $image_ext ) {
//if found, simply replace with nothing (easy)
$new_url = str_replace( $image_ext , '' , $url );

//report changed url
if ( $new_url != $url )
break;
}

// if a new url hasn't been generated...
if ( $new_url == $url ) {
// Cycle through image width only extensions, e.g. -200x
foreach ( $image_exts_width as $image_ext ) {

// check for width, e.g., -200x
$pos1 = strrpos( $info['basename'] , $image_ext , -1 );
if ( $pos1 ) {
// strip, & assign new url
$new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos1 ) . '.' . $info['extension'];
}

if ( $new_url != $url )
break;
}

// if a new url hasn't been generated...
if ( $new_url == $url ) {
// Cycle through image height only extensions, e.g. x200.
foreach ( $image_exts_height as $image_ext ) {

// check for height, e.g., x200
$pos2 = strrpos( $info['basename'] , $image_ext , -1 );

//example_2a-263x300.jpg -> example_2a.jpg
if ( $pos2 ) {
// get position of -, strip, & assign new url
$pos3 = strrpos( $info['basename'] , '-' , -1 );
$new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos3 ) . '.' . $info['extension'];
}

if ( $new_url != $url )
break;
}
}
}

if ( $new_url != $url )
return $new_url;
else
return $url;
}
[/php]

Written by Travis Smith · Categorized: Tutorials, WordPress

Aug 24 2011

Infographic about WP_Query

wp_query Functions

Written by Travis Smith · Categorized: WordPress

Aug 24 2011

The Difference between do_action, add_action and add_filter

The Foundation: do_action

do_action creates the hook for things to hang. This is at the core of WordPress and even frameworks like Genesis and many other themes and plugins. do_action is the first domino in the chain of events with hooks. However, alone, it means nothing and does nothing. Simply, it tells WordPress to search to see if any functions are attached to it to fire. So do_actions will look something like this:
[php]<?php do_action( 'my-home' ); [/php]

It can also take and pass variables:
[php]<?php do_action( 'my-home' , $var1 , $var2 ); [/php]

The Difference between add_action and add_filter

The difference is primarily and technically semantic. Technically speaking, you can use them interchangeably, but it wouldn't follow code common sense or code "mentality" as one writer said. Filters should filter information, thus receiving information/data, applying the filter and returning information/data, and then used. However, filters are still action hooks.

WordPress defines add_filter as "Hooks a function to a specific filter action," and add_action as "Hooks a function on to a specific action."

add_action

Now, add_actions are a bit different. They hang items on the do_action hook and $priority determines the order.

Say, for example, you have this in your functions.php file:
[php]<?php
add_action( 'hook' , 'bob' );
add_action( 'hook' , 'andy' );
[/php]
The general order is "chronological" meaning if action 'bob' appears (coded/read by the server) before action 'andy', then the action hook 'bob' will fire first. However, with $priority, this order can be interrupted and changed. So...
[php]<?php
add_action( 'hook' , 'bob' , 10 );
add_action( 'hook' , 'andy' , 5 );
[/php]

With add_actions, variables and information may be passed back and forth as needed in the various functions as need. It should also be noted that not all actions are void of arguments or parameters. Some actions do have parameters, so:
[php]<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?>[/php]

So, it would look something like this:
[php]<?php add_action( 'hook_name' , 'my_function_name' , 10 , 2 ); ?>[/php]

Extended Version Example
[php]<?php
function echo_comment_id( $comment_ID )
{
echo "I just received $comment_ID";
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );[/php]

add_filter

Now WordPress (and my theme) passes much of this page through various filters that check, validate, correct, and even modify various parts. Once it passes through the filter, the information then is applied to an action.

So here is the add_filter function:
[php]<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>[/php]

This generally looks something like this:
[php]<?php
add_filter( 'filter_name' , 'my_filter_function_name' , 10 , 3 );
function filter_name( $val, $attr, $content = null ) {
//do something
}
?>[/php]

Just like add_actions go with do_actions, the same is true for filters logically speaking. With add_filter you must have apply_filters. Without the filter being called or applied then the filter means nothing, logically.

So, logicaly, in our example with filter 'filter_name', this would modify content/information that is coded like this:
[php]<?php
// Allow plugins/themes to override the default caption template.
$output = apply_filters( 'filter_name', $output , $val, $attr , $content );
if ( $output != '' )
return $output;
[/php]

So, the code is basically saying, “Take the value of the `$output` variable, apply any filters attached to the ‘filter_name’ hook passing the variables $val, $attr, $content to the filter function (whatever that may be and if it accepts them), and assign the filtered value back to the `$output` variable”.

However, technically speaking, PHP is rather forgiving and instead of filtering anything, it can function like add_action, adding the filter to a do_action, not filtering anything, which adds to the confusion. So if you have the following, it will work (though not good form):
[php]</php
do_action( 'my_action' );
add_filter( 'my_action' , 'my_function');
function my_function() {
//do something
}[/php]

Since the add_filter did not have any filters being applied, it worked as an add_action.

Written by Travis Smith · Categorized: Tutorials, WordPress

Aug 23 2011

How to Get WordPress's Custom Menu Description

So recently, someone wanted to use menu descriptions as a tagline of sorts for a variety of pages, categories, archives, etc. instead of using basic metaboxes on pages (since this would also apply to category pages, etc.). So I wrote up a simple function that I believe may be helpful for others. So here is the code. To use, simply copy the function to your functions.php file and then use the function wherever you'd like.

[php]<?php
function wps_get_menu_description( $args = array() ) {
global $post;

// Default
$defaults = array(
'echo' => false,
'format' => '',
'description' => '',
'location' => 'primary',
'classes' => 'post-description'
);

$args = wp_parse_args( $args, $defaults );
extract( $args , EXTR_SKIP );

// Get menu
$menu_locations = get_nav_menu_locations();
$nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] );

// Cycle through nav items
foreach ( $nav_items as $nav_item ) {

if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) {
$description = $nav_item->description;
}
elseif ( ( is_category() ) && ( $nav_item->object == 'category' ) ) {
$cat = get_query_var( 'cat' );
if ( $nav_item->object_id == $cat )
$description = $nav_item->description;
}
}

// Get output formatting
if ( $format == 'html' )
$output = apply_filters( 'wps_get_menu_description_output' , '<div class="'. $classes .'">' . $description . '</div>' , $args );
else
$output = $description;

// Echo description
if ($echo)
echo $output;

// Return description
return $output;
}
[/php]

To call the function, you just set the args in an array like this:
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );

With most frameworks, like Genesis/Thesis, you may need to call it like this (example reflects Genesis):
[php]<?php
// Add and Customize a Tagline under the Page Title
add_action('genesis_after_post_title', 'my_tagline');
function my_tagline() {
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );
}[/php]

Word of Warning: Please check your descriptions, and do not assume that there is nothing there if you didn't put it there. Your theme or plugins may have inserted content there as I've seen the description contain the_content automagically.

Do you see anything that I missed? Is there something else that you wish was added?

Written by Travis Smith · Categorized: Tutorials, WordPress

Aug 16 2011

How to Make a Genesis Grid Archive Template for Tags

To display posts from a specific tag or tags, you need to add a 'tag' argument to the $grid_args array.

In the StudioPress tutorial about categories, you are given this example for your home.php.
[php highlight="17"]<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function child_grid_loop_helper() {
if ( function_exists( 'genesis_grid_loop' ) ) {
genesis_grid_loop( array(
'features' => 2,
'feature_image_size' => 0,
'feature_image_class' => 'alignleft post-image',
'feature_content_limit' => 0,
'grid_image_size' => 'grid-thumbnail',
'grid_image_class' => 'alignleft post-image',
'grid_content_limit' => 0,
'more' => __( '[Continue reading...]', 'genesis' ),
'posts_per_page' => 6,
'cat' => '6,7' //enter your category IDs here separated by commas in ' '
) );
} else {
genesis_standard_loop();
}
}

/** Remove the post meta function for front page only **/
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );

genesis();[/php]

Simply change the 'cat' argument to 'tag'. And you can have the customization you want. OR, you can grab it dynamically by making the following changes:
[php highlight="6,18"]<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function child_grid_loop_helper() {
$term = get_query_var( 'term' );
if ( function_exists( 'genesis_grid_loop' ) ) {
genesis_grid_loop( array(
'features' => 2,
'feature_image_size' => 0,
'feature_image_class' => 'alignleft post-image',
'feature_content_limit' => 0,
'grid_image_size' => 'grid-thumbnail',
'grid_image_class' => 'alignleft post-image',
'grid_content_limit' => 0,
'more' => __( '[Continue reading...]', 'genesis' ),
'posts_per_page' => 6,
'tag' => $term
) );
} else {
genesis_standard_loop();
}
}

/** Remove the post meta function for front page only **/
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );

genesis();[/php]

You can save this as tag.php to make all of your tag archives as a grid. Or, you can simply apply this to a single tag by naming it tag-{SLUG}.php or tag-{ID}.php (see WordPress Codex for Tag Templates for more information).

Written by Travis Smith · Categorized: Genesis, Genesis Grid Loop, Tutorials

  • « Previous Page
  • 1
  • …
  • 34
  • 35
  • 36
  • 37
  • 38
  • …
  • 61
  • Next Page »
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in