<?php
/*
Plugin Name: Pdf
Plugin URI: http://www.seriadesign.com
Description: Plugin servant a mettre des pdf
Version: 0.01
Author: Christophe Rudyj
Author URI: http://www.seriadesign.com
License: Seria-CSN
*/
/**
* Custom "Pdf" custom type for tutorial on Jayj.dk
*
* @link http://jayj.dk/2011/Pdf-cpt-wordpress
*/
add_action( 'init', 'csn_pdf_pt' );
function csn_pdf_pt() {
register_post_type( 'pdf', array(
'labels' => array(
'name' => 'Pdf',
'add_new' => 'Ajouter',
'add_new_item' => 'Ajouter',
'edit' => 'Editer',
'edit_item' => 'Editer un Pdf',
'new_item' => 'Nouveau Pdf',
'view' => 'Voir le Pdf',
'view_item' => 'Voir le Pdf',
'search_items' => 'Rechercher Pdf',
'not_found' => 'Aucun PDF trouvé',
'not_found_in_trash' => 'Aucun PDF trouvé dans la Corbeille',
),
'description' => 'Pdf récent',
'public' => true,
'show_ui' => true,
'exclude_from_search' => true,
'supports' => array( 'title', 'excerpt', 'thumbnail', ),
'has_archive' => 'Pdf', // The archive slug
'rewrite' => array( 'slug' => 'Pdf' ),
));
}
/**
* Custom "Pdf" post type metabox
*/
/**
* Adds the Pdf meta box for the Pdf post type
*/
function csn_pdf_meta() {
add_meta_box( 'csn_pdf_meta_box', 'Pdf Settings', 'csn_pdf_meta_display', 'pdf', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'csn_pdf_meta' );
/**
* Displays the Pdf meta box
*/
function csn_pdf_meta_display( $object, $box ) {
// Setup some default values
$defaults = array(
'file' => '',
'code' => ''
// Add more!
);
// Get the post meta
$Pdf = get_post_meta( $object->ID, 'Pdf', true );
// Merge them
$Pdf = wp_parse_args( $Pdf, $defaults );
?>
<input type="hidden" name="csn_pdf_meta_box" 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="Pdf-file">File</label></th>
<td>
<input type="text" id="Pdf-file" size="60" name="Pdf-file" value="<?php echo esc_url( $Pdf['file'] ); ?>" />
<input type="button" id="upload-file-button" class="button" value="Upload file" />
<label for="Pdf-file"><span class="description">Upload or link to Pdf file</span></label>
</td>
</tr>
</table>
<?php
}
/**
* Save the Pdf information
*/
function csn_pdf_meta_save( $post_id, $post ) {
/* Verify that the post type supports the meta box and the nonce before preceding. */
if ( ! isset( $_POST['csn_pdf_meta_box'] ) || ! wp_verify_nonce( $_POST['csn_pdf_meta_box'], 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 Pdf meta
*/
$codeu = esc_url_raw( $_POST['Pdf-file'] );
$codef = '[pdfic]'.$codeu.'[pdfic]';
update_post_meta( $post_id, 'Pdf', array(
'file' => esc_url_raw( $_POST['Pdf-file'] ),
'postid' => absint( $_POST['Pdf-postid'] ),
'code' => $codef,
// Add your own if you've added fields
) );
// Seperate the Pdf count and "hide" from the rest
update_post_meta( $post_id, 'Pdf_count', absint( $_POST['Pdf-count'] ) );
update_post_meta( $post_id, '_hide_Pdf', ( $_POST['Pdf-hide'] == 1 ? 1 : 0 ) );
}
add_action( 'save_post', 'csn_pdf_meta_save', 10, 2 );
/**
* Adds the script needed for the file upload
*/
function pdf_metabox_script() {
$screen = get_current_screen();
// Make sure we are on the Pdf screen
if ( isset( $screen->post_type ) && $screen->post_type == 'pdf' ) : ?>
<script type="text/javascript">
jQuery(document).ready(function($){
var formfield;
// Open upload window
$('#upload-file-button').click(function() {
formfield = $('#Pdf-file').attr('name');
tb_show( '','media-upload.php?type=image&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
$('#Pdf-file').val(fileurl);
tb_remove();
} else {
window.original_send_to_editor(html);
}
};
});
</script>
<?php
endif;
}
add_action( 'admin_footer', 'pdf_metabox_script' );
/**
* Alternative to the inline script
* Change path so it fits
*/
/*
pdf_metabox_enqueue_script() {
$screen = get_current_screen();
if ( $screen->post_type == 'pdf' )
wp_enqueue_script( 'jayj-Pdf-upload', get_stylesheet_directory_uri() . '/admin/upload.js', array( 'jquery' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'pdf_metabox_enqueue_script' );
*/
add_filter('manage_edit-pdf_columns', 'pdf_cpt_columns');
add_action('manage_pdf_posts_custom_column', 'pdf_cpt_custom_column', 10, 2);
function pdf_cpt_columns($defaults) {
$defaults['file'] = 'Code pour un article';
return $defaults;
}
function pdf_cpt_custom_column($column_name, $post_id) {
if ( 'code' === $column_name ) {
if ( $Pdf = get_post_meta( $post_id, 'Pdf', true ) ) {
printf( '[pdfic href="%s"]', $Pdf );
} else {
echo ' - ';
}
}
?>