Matthew Vaccaro

Coordinator of Internets
  • Index
  • Blog
  • Snippets
  • getimagesize() without PHP!

    Apparently getimagesize() is a neat function built into PHP, but it’s also slow as molasses and requires the use of an fopen property some servers have disabled. Enter cURL.

    You can use cURL and a function to return the width and height of an image VIA a URL. The code below is a cluster of something I found online, with a mix of my own magic.

     

    // Function that does some cURLing to determine the images size without using getimagesize()
    if(!function_exists( ‘curlsizer’ )) {
    function curlsizer( $rawImage ){

    $header = array(
    “Range: bytes=0-32768″
    );

    $ch = curl_init( $rawImage );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec( $ch );
    curl_close( $ch );

    }
    }

    // I work out…
    $raw = curlsizer($rawImage);
    $im = imagecreatefromstring($raw);
    $width = imagesx($im);
    $height = imagesy($im);

    echo $width.’<br/>’.$height;

    cURL Debug Code

    Using cURL is tons of fun (not), but if you have to do it, some basic debugging can be helpful to see if your MIME types, effective URL, and file size are coming in correctly. I used these lines to do view some cURL information while working with it.

    echo ‘Req URL: ‘.curl_getinfo($ch, CURLINFO_EFFECTIVE_URL).’<br/>’;
    echo ‘Time: ‘.curl_getinfo($ch, CURLINFO_TOTAL_TIME).’<br/>’;
    echo ‘Req Size: ‘.curl_getinfo($ch, CURLINFO_REQUEST_SIZE).’<br/>’;
    echo ‘C-Type: ‘.curl_getinfo($ch, CURLINFO_CONTENT_TYPE).’<br/>’;

    Useful WordPress Debug Code

    I find these snippets to be useful for finding information about your posts, custom posts, pages, etc.

    global $wp_query;
    $tag = $wp_query->get_queried_object();

    echo “My taxonomy : <br /> <pre>”;
    var_dump($tag);
    echo “</pre>”;

    echo “<br /> Taxonomy id: “.$tag->term_id;

    $terms = get_the_terms($post->ID, ‘newsletter_category’);

    print_r($terms);

    Insert RSS Feed Into WP Page

    I wanted to include an RSS feed from a wordpress website into on of its own pages for a newsletter that was being sent out. Turns out, though, you don’t need to do this and can just query the custom post type, haha.

    On a site note, yes, this is basically the same script as the demo they provide you with in the WP codex. For some reason it wasn’t working for me until I edited it into this form…

    Include this somewhere before the script at the top of the page (I put mine in the template declaration):

    <?php
    /* Template Name: Newsletter*/
    include_once(ABSPATH . WPINC . '/feed.php');
    ?>

    Here’s the script:

    <?php

    $rss = fetch_feed(‘http://www.url.com/feed/?post_type=news’);

    if (!is_wp_error( $rss ) ) :
    $maxitems = $rss->get_item_quantity(7);
    $rss_items = $rss->get_items(0, $maxitems);
    endif;

    if ($maxitems == 0 ) echo $rss->get_error_message();

    else

    foreach ( $rss_items as $item ) :

    echo ‘<h2><a href=”‘.$item->get_permalink().’”>’.$item->get_title().’</a></h2>’;
    echo ‘<p>’.$item->get_description().’<a href=”‘.$item->get_permalink().’” class=”more-link”>Read More…</a></p>’;

    endforeach; ?>

    Get WP Utmost Parent

    I needed to highlight part of a large navigation menu, even when you are inside a sibling or child page of a parent page. So, we needed to find the top level ‘grandparent’ or ‘utmost parent’ of a page. In comes this little guy:

    Add this to your functions.php file:

    // Get grandparent page closest to the top
    function get_top_ancestor($id){
    $currentPage = get_post($id);
    if(!$currentPage->post_parent){
    return $currentPage->ID;
    } else {
    return get_top_ancestor($currentPage->post_parent);
    }
    }

    Put this in your page:

    $currPageID = get_the_ID();
    $currPageActive = get_top_ancestor($currPageID);

    Simple and useful!

    jQuery Simply Slide Menu

    Using jQuery, sub-menus slide down from a horizontal navigation span. This is extremely primitive and simple, I haven’t revisited it yet but it works!

    CSS Code:

    
    
    header {
            z-index: 9999;
    }
    #locations-drop,
    #contact-drop {
    	display: none;
    	z-index: 9998;
    }

    HTML Code:

    <header>
    	<h1><a href="<?php bloginfo( 'url' ); ?>"><?php bloginfo( 'name' ); ?> - <?php bloginfo( 'description' ); ?></a></h1>
    	<ul id="nav">
    	  <li class="current"><a href="<?php bloginfo( 'url' ); ?>">Home</a></li>
    	  <li><a href="<?php bloginfo( 'url' ); ?>" id="the-locations">Locations</a></li>
    	  <li><a href="<?php bloginfo( 'url' ); ?>" id="contact-button">Contact Us</a></li>
    	</ul>
    </header>

    jQuery Code:

    $(document).ready(function(){
    	$('#the-locations').click(
    	  function () {
    	    $('#locations-drop').stop().animate({display:'block'}).slideDown(1000);
    	  },
    	  function () {
    	    $('#locations-drop').slideUp();
    	  });
    	$('#contact-button').click(
    	  function () {
    	    $('#contact-drop').stop().animate({display:'block'}).slideDown(1000);
    	  },
    	  function () {
    	    $('#contact-drop').slideUp();
    	  });
    });
    WP Check Page Children

    This snippet was used to check a WordPress page for children. If it had none, it either printed out a list of pages or the alternate content.

    <?php  
    		
    		$pageID = get_the_ID();
    	               					
    		$args = array(
    		'title_li' => __('<h1>Heading Here</h1>'),
    		'child_of' => $pageID
    		);
    
    		$children = get_pages('child_of='.$post->ID);
    		
    		if( count( $children ) != 0 ) {
    			 wp_list_pages($args); 
    		}else {
    			 echo 'Alternate Content!';
    		}
    		
    		?>
    Large First Paragraph Letter Drop

    NOTE: I know there is a much easier way to do this. I did this because I was forced to make it work in IE6 and IE 7.

    I poked around Google for a good 30 minutes before giving up, so I did it myself. I wanted to add a large first letter to a paragraph I was doing in a medieval styled theme, but with a twist. Instead of the standard ‘super’ styling of the first letter where it is higher than the rest of the text, I wanted it to be lower than the rest of the text. This may not be the easiest way of going about it, but it works as far as I can see…

    Basically it positions the first letter, which we made a pseudo-element of the ‘P’ tag, over an empty div which is floated left, causing the text to wrap around it and create our desired effect.

    CSS Markup:

    p#pseudo:before {
    	content: "P";
    	font: bold 66px Georgia;
    	color: #700f07;
    	position: absolute;
    	display: block;
    	margin-top: -10px;
    }
    p#pseudo #spacer {
    	width: 55px;
    	height: 60px;
    	display: block;
    	float: left;
    }

    HTML Markup:

    <p id=”pseudo”><span id=”spacer”></span>lease join us for these special events coming up in the near future. You may register for these events at the links below.</p>

    Switch WP Date Number to Month

    While working with custom post types and some event/date related stuff, I came upon an issue of how to easily transform the numbered output of the jQuery date picker I was using for users to choose dates inside a custom post type, into the abbreviated month names such as Jan. and Feb. (As opposed to 01 and 02).

    EDIT: This was more complex, but I neutered it down to fit this specific example.

    <?php
    
    $date = 01-08-2011;
    list($year, $month, $day) = explode("-", $date);
    
    switch ($month) {
    case "01":
    	echo "JAN";
    	break;
    case "02":
    	echo "FEB";
    	break;
    case "03":
    	echo "MAR";
    	break;
    case "04":
    	echo "APR";
    	break;
    case "05":
    	echo "MAY";
    	break;
    case "06":
    	echo "JUN";
    	break;
    case "07":
    	echo "JUL";
    	break;
    case "08":
    	echo "AUG";
    	break;
    case "09":
    	echo "SEPT";
    	break;
    case "10":
    	echo "OCT";
    	break;
    case "11":
    	echo "NOV";
    	break;
    case "12":
    	echo "DEC";
    break;
    }
    echo '<span class="eventDay">'; echo $day; echo '</span>';							
    
    ?>
    Simple jQuery Browser Sniff and Append

    I wanted to add some elements into my DOM for users of browsers which I feel are ‘lacking’. So I went ahead and wrote this small jQuery snippet which allows you to append stuff to your DOM after jQuery sniffs for the browsers version. This one will display an element on all Microsoft browsers less than version 8.0.

    <script type="text/javascript">
    	$(document).ready(function(){
    		var bv = $.browser;
    		if (bv.msie && bv.version.slice(0,3) < "8.0") {
    			$('body').append("ADD THIS TO MY SITE!");
    		}
    	});
    </script>
    Show Item by Blog/Page – WP MultiSite

    The code snippet below allows you to display a certain element or item, whatever, on a specific blog or page. This example allows you to specifcy either the page ID, the blog ID, or both using your PHP operators. You can else switch the item with the do nothing statement, or vice versa.

     

    <?php
    
    $blogID = get_current_blog_id();
    $pageID = get_the_ID();
    
    if ( 1 == $blogID && $pageID == 17 ) {
        // Do nothing.
    } else {
        echo ''.get_template_part('includes/toolBar').'';
    }
    
    ?>
    Style the Inside of Your Blog Name

    Ever wanted to style the inside of your blog name or blog title with CSS tags? Well here’s an easy way to do it.

    <?php
    
    $blogName = get_bloginfo('name');
    
    $blogNamePiece = explode(" ", $blogName);
    //This is to check that our blogName is printing out, as a test.
    //echo $blogName;
    
    echo $blogNamePiece[0].' <span>'.$blogNamePiece[1].'</span> '.$blogNamePiece[2];
    
    ?>