Skip to content

Instantly share code, notes, and snippets.

@jayj
Created September 2, 2011 22:30
Show Gist options
  • Save jayj/1190106 to your computer and use it in GitHub Desktop.
Save jayj/1190106 to your computer and use it in GitHub Desktop.
Full code for tutorial on Jayj.dk
<?php
/**
* Custom "Download" custom type for tutorial on Jayj.dk
*
* @link http://jayj.dk/2011/download-cpt-wordpress
*/
add_action( 'init', 'jayj_create_download_post_type' );
function jayj_create_download_post_type() {
register_post_type( 'jayj_download', array(
'labels' => array(
'name' => 'Downloads',
'add_new' => 'Add New',
'add_new_item' => 'Add New Download',
'edit' => 'Edit',
'edit_item' => 'Edit Download',
'new_item' => 'New Download',
'view' => 'View Download',
'view_item' => 'View Download',
'search_items' => 'Search Downloads',
'not_found' => 'No downloads found',
'not_found_in_trash' => 'No downloads found in Trash',
),
'description' => 'The latest downloads',
'public' => true,
'show_ui' => true,
'exclude_from_search' => true,
'supports' => array( 'title', 'excerpt', 'thumbnail', 'custom-fields' ),
'has_archive' => 'downloads', // The archive slug
'rewrite' => array( 'slug' => 'download' ),
));
/**
* Custom "Download Categories" taxonomy
*/
register_taxonomy( 'download-categories', array( 'jayj_download' ),
array(
'public' => true,
'labels' => array( 'name' => 'Download Categories', 'singular_name' => 'Download Category' ),
'hierarchical' => true,
'rewrite' => array( 'slug' => 'download-types' )
)
);
}
?>
<?php
/**
* Custom "Download" post type metabox
*/
/**
* Adds the download meta box for the download post type
*/
function jayj_meta_box_download() {
add_meta_box( 'jayj-meta-box-download', 'Download Settings', 'jayj_meta_box_download_display', 'jayj_download', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'jayj_meta_box_download' );
/**
* Displays the download meta box
*/
function jayj_meta_box_download_display( $object, $box ) {
// Setup some default values
$defaults = array(
'file' => '',
'version' => '1.0',
'postid' => '',
// Add more!
);
// Get the post meta
$download = get_post_meta( $object->ID, 'download', true );
// Merge them
$download = wp_parse_args( $download, $defaults );
?>
<input type="hidden" name="jayj-meta-box-download" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
<br />
<table class="form-table">
<thead><tr><th><span class="description">None of the fields are required</span></th></tr></thead>
<tr>
<th><label for="download-hide">Hide download</label></th>
<td>
<?php $hide = get_post_meta( $object->ID, '_hide_download', true ); ?>
<label><input type="checkbox" name="download-hide" id="download-hide" value="1" <?php checked( '1', $hide ); ?> />
<span class="description">Check this if you don't want to show the download at the archive page</span></label>
</td>
</tr>
<tr>
<th><label for="download-file">File</label></th>
<td>
<input type="text" id="download-file" size="60" name="download-file" value="<?php echo esc_url( $download['file'] ); ?>" />
<input type="button" id="upload-file-button" class="button" value="Upload file" />
<label for="download-file"><span class="description">Upload or link to download file</span></label>
</td>
</tr>
<tr>
<th><label for="download-version">Version</label></th>
<td>
<input type="text" id="download-version" name="download-version" value="<?php echo esc_attr( $download['version'] ); ?>" size="3" />
</td>
</tr>
<tr>
<th><label for="download-count">Download count</label></th>
<td>
<?php $count = isset( $object->ID ) ? get_post_meta( $object->ID, 'download_count', true ) : 0; ?>
<input type="number" id="download-count" name="download-count" value="<?php echo absint( $count ); ?>" size="7" min="0" />
<?php printf( '<p>This file has been downloaded <b>%d</b> times</p>', absint( $count ) ); ?>
</td>
</tr>
<tr>
<th><label for="download-postid">Post ID</label></th>
<td>
<label><input type="text" id="download-postid" name="download-postid" value="<?php echo absint( $download['postid'] ); ?>" size="3" />
<span class="description">ID of the post which the download is associated with</span></label>
<?php if ( !empty( $download['postid'] ) ) { ?>
<p>This download is associated with <a href="<?php echo get_permalink( $download['postid'] ); ?>"><?php echo get_the_title( $download['postid'] ); ?></a></p>
<?php } ?>
</td>
</tr>
</table>
<?php
}
/**
* Save the download information
*/
function jayj_meta_box_download_save( $post_id, $post ) {
/* Verify that the post type supports the meta box and the nonce before preceding. */
if ( ! isset( $_POST['jayj-meta-box-download'] ) || ! wp_verify_nonce( $_POST['jayj-meta-box-download'], basename( __FILE__ ) ) )
return $post_id;
/* Don't save them if... */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
if ( defined( 'DOING_CRON' ) && DOING_CRON ) return;
if ( $post->post_type == 'revision' ) return;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/**
* Add the download meta
*/
update_post_meta( $post_id, 'download', array(
'file' => esc_url_raw( $_POST['download-file'] ),
'version' => strip_tags( $_POST['download-version'] ),
'postid' => absint( $_POST['download-postid'] ),
// Add your own if you've added fields
) );
// Seperate the download count and "hide" from the rest
update_post_meta( $post_id, 'download_count', absint( $_POST['download-count'] ) );
update_post_meta( $post_id, '_hide_download', ( $_POST['download-hide'] == 1 ? 1 : 0 ) );
}
add_action( 'save_post', 'jayj_meta_box_download_save', 10, 2 );
/**
* Adds the script needed for the file upload
*/
function jayj_download_metabox_script() {
$screen = get_current_screen();
// Make sure we are on the Download screen
if ( isset( $screen->post_type ) && $screen->post_type == 'jayj_download' ) : ?>
<script type="text/javascript">
jQuery(document).ready(function($){
var formfield;
// Open upload window
$('#upload-file-button').click(function() {
formfield = $('#download-file').attr('name');
tb_show( '','media-upload.php?type=image&amp;TB_iframe=true' );
return false;
});
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html) {
if (formfield) {
// Get the src value from the image
fileurl = $('img', html).attr('src');
// The upload is not an image! Get the href instead
if( fileurl === undefined )
fileurl = $(html).attr('href');
// Insert it into the text box and close
$('#download-file').val(fileurl);
tb_remove();
} else {
window.original_send_to_editor(html);
}
};
});
</script>
<?php
endif;
}
add_action( 'admin_footer', 'jayj_download_metabox_script' );
/**
* Alternative to the inline script
* Change path so it fits
*/
/*
jayj_download_metabox_enqueue_script() {
$screen = get_current_screen();
if ( $screen->post_type == 'jayj_download' )
wp_enqueue_script( 'jayj-download-upload', get_stylesheet_directory_uri() . '/admin/upload.js', array( 'jquery' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'jayj_download_metabox_enqueue_script' );
*/
/**
* Download counter function
*/
function jayj_count_and_redirect() {
// Return if not download
if ( ! is_singular( 'jayj_download' ) )
return;
$download_id = get_queried_object_id();
$postmeta = get_post_meta( $download_id, 'download', true );
$count_meta = get_post_meta( $download_id, 'download_count', true );
// Get the count
$count = isset( $download_id ) ? $count_meta : 0;
// Handle the redirect
$redirect = isset( $download_id ) ? $postmeta['file'] : '';
if ( ! empty( $redirect ) ) :
wp_redirect( esc_url_raw( $redirect ), 301 );
update_post_meta( $download_id, 'download_count', $count + 1 ); // Update the count
exit;
else :
wp_redirect( home_url(), 302 );
exit;
endif;
}
add_action( 'template_redirect', 'jayj_count_and_redirect' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment