WordPress: associare un file PDF ai post tramite una casella di selezione

WordPress: associare un file PDF ai post tramite una casella di selezione

Tramite i metabox di WordPress รจ possibile associare qualsiasi tipo di risorsa ad un post o ad una pagina. In questo articolo vedremo come creare una lista di file PDF da associare ad un post.

Il seguente codice, da aggiungere al file functions.php, crea una casella di selezione prendendo tutti i file PDF inseriti nella Media Library. Quindi crea uno shortcode per poter inserire il file scelto nel post:


add_action("admin_init", "pdf_init");
add_action('save_post', 'save_pdf_link');


function pdf_init(){
	add_meta_box("my-pdf", "Documento PDF", "pdf_link", "post", "normal", "low");
}

function pdf_link(){
	global $post;
	$custom  = get_post_custom($post->ID);
	$link    = $custom["link"][0];
	$count   = 0;
	echo '<div class="link_header">';
	$query_pdf_args = array(
		'post_type' => 'attachment',
		'post_mime_type' =>'application/pdf',
		'post_status' => 'inherit',
		'posts_per_page' => -1,
		);
	$query_pdf = new WP_Query( $query_pdf_args );
	$pdf = array();
	echo '<select name="link">';
	echo '<option class="pdf_select">Seleziona file PDF</option>';
	foreach ( $query_pdf->posts as $file) {
	   if($link == $pdf[]= $file->guid){
	      echo '<option value="'.$pdf[]= $file->guid.'" selected="true">'.$pdf[]= $file->guid.'</option>';
		 }else{
	      echo '<option value="'.$pdf[]= $file->guid.'">'.$pdf[]= $file->guid.'</option>';
		 }
		$count++;
	}
	echo '</select></div>';
	echo '<p>Seleziona un file PDF da associare al post.</p>';
	echo '<div class="pdf_count"><span>Numero dei file:</span> <strong>'.$count.'</strong></div>';
}

function save_pdf_link(){
	global $post;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
	update_post_meta($post->ID, "link", $_POST["link"]);
}

add_action( 'admin_head', 'pdf_css' );

function pdf_css() {
	echo '<style type="text/css">
	.pdf_select{
		font-weight:bold;
		background:#e5e5e5;
		}
	.pdf_count{
		font-size:9px;
		color:#0066ff;
		text-transform:uppercase;
		background:#f3f3f3;
		border-top:solid 1px #e5e5e5;
		padding:6px 6px 6px 12px;
		margin:0px -6px -8px -6px;
		-moz-border-radius:0px 0px 6px 6px;
		-webkit-border-radius:0px 0px 6px 6px;
		border-radius:0px 0px 6px 6px;
		}
	.pdf_count span{color:#666;}
	</style>';
}
function pdf_file_url(){
	global $wp_query;
	$custom = get_post_custom($wp_query->post->ID);
	return $custom['link'][0];
}

function add_pdf_file_to_post($attrs) {

	extract( shortcode_atts( array(
		'title' => 'File PDF'
	), $attrs ) );

	$pdf = pdf_file_url();
	
	return '<a href="' . $pdf . '">' . $title . '</a>';

}

add_shortcode('pdf-file', 'add_pdf_file_to_post');

Esempio d'uso:

[pdf-file title="Documento PDF"]
Torna su