Skip to content

Instantly share code, notes, and snippets.

@ataylorme
Created November 17, 2012 22:37
Show Gist options
  • Save ataylorme/4100868 to your computer and use it in GitHub Desktop.
Save ataylorme/4100868 to your computer and use it in GitHub Desktop.
WordPress coupon pop-up shortcode
add_image_size( 'coupon-thumb', 250, 115, true );
function coupons_shortcode() {//start coupon shortcode function shortcode
global $COUPONS_name, $COUPONS_prefix, $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type' => 'image'
);
$attachments = get_posts( $args );
if ( $attachments ) {
//start drawing the coupons html
$output = '<!-- coupons -->';
$output .= '<div id="coupons">';
foreach ( $attachments as $attachment ) {
$filename = basename ( get_attached_file( $attachment->ID ) );
$html = strtolower(str_replace(array('.jpg','.jpeg','.gif','.png'),'.html',$filename));
$path = ABSPATH . '/coupons/' . $html;
if( !file_exists($path) ):
$fcontent = '<html><head><meta name="robots" content="noindex, nofollow" /></head><body><center>'.wp_get_attachment_image( $attachment->ID, 'full' ).'<br /><br /><input type="button" value="Click to Print" onclick="window.print();" id="btnPrint"></body></html>';
$f = fopen($path, "w");
fwrite($f, $fcontent);
fclose($f);
endif;
$url = get_bloginfo('url') . '/coupons/' . $html;
$output.='<!-- start coupon --><div class="coupon">';
$output.='<a name="'.apply_filters( 'the_title', $attachment->post_title ).'" href="'.$url.'" title="'.apply_filters( 'the_title', $attachment->post_title ).'" onclick="window.open(this.href, '', 'resizable=no,status=no,location=no,toolbar=no,menubar=no,fullscreen=no,scrollbars=no,dependent=no,width=620,height=600'); return false;" rel="nofollow">';
$output.=wp_get_attachment_image( $attachment->ID, 'coupon-thumb' );
$output.='</a>';
$output.='</div><!-- end coupon -->';
}
$output .= '</div>';
$output .= '<!-- end coupons -->';
}
//end drawing the coupons html
//return output
return $output;
}//end coupon shortcode shortcode function
add_shortcode( 'coupons' , 'coupons_shortcode');//register coupon shortcode function with shortcode [coupons]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment