public
Created

Custom "Download" metabox for tutorial on Jayj.dk

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
<?php
/**
* Custom "Download" custom type metabox for tutorial on Jayj.dk
*
* @link http://jayj.dk/
*/
 
/**
* 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 ( isset( $screen->post_type ) && $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' );
*/
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.