• Здравствуйте!
    Стоит задача добавить два поля в Галерею Медиа загрузчика!

    /**
     * Add Client and Agency fields to media uploader
     *
     * @param $form_fields array, fields to include in attachment form
     * @param $post object, attachment record in database
     * @return $form_fields, modified form fields
     */
    
    function be_attachment_field_credit( $form_fields, $post ) {
    	$form_fields['be-client'] = array(
    		'label' => 'Client',
    		'input' => 'text',
    		'value' => get_post_meta( $post->ID, 'be-client', true ),
    		'helps' => 'Add Client',
    	);
    
    	$form_fields['be-agency'] = array(
    		'label' => 'Agency',
    		'input' => 'text',
    		'value' => get_post_meta( $post->ID, 'be-agency', true ),
    		'helps' => 'Add Agency',
    	);
    
    	return $form_fields;
    }
    
    add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );
    
    /**
     * Save values of Client and Agency in media uploader
     *
     * @param $post array, the post data for database
     * @param $attachment array, attachment fields from $_POST form
     * @return $post array, modified post data
     */
    
    function be_attachment_field_credit_save( $post, $attachment ) {
    	if( isset( $attachment['be-client'] ) )
    		update_post_meta( $post['ID'], 'be-client', $attachment['be-client'] );
    
    	if( isset( $attachment['be-agency'] ) )
    update_post_meta( $post['ID'], 'be-agency', esc_url( $attachment['be-agency'] ) );
    
    	return $post;
    }
    
    add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
    
    ?>

    Код вставлен в functions.php
    Все отлично в админке появились эти поля редактируются сохраняются все отлично!

    Только вопрос как мне передать эти переменные в свой шаблон

    вот код моей галлереи

    function my_own_gallery_shortcode($attr) {
    	$post = get_post();
    
    	static $instance = 0;
    	$instance++;
    
    	if ( ! empty( $attr['ids'] ) ) {
    		// 'ids' is explicitly ordered, unless you specify otherwise.
    		if ( empty( $attr['orderby'] ) )
    			$attr['orderby'] = 'post__in';
    		$attr['include'] = $attr['ids'];
    	}
    
    	// Allow plugins/themes to override the default gallery template.
    	$output = apply_filters('post_gallery', '', $attr);
    	if ( $output != '' )
    		return $output;
    
    	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
    	if ( isset( $attr['orderby'] ) ) {
    		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
    		if ( !$attr['orderby'] )
    			unset( $attr['orderby'] );
    	}
    
    	extract(shortcode_atts(array(
    		'order'      => 'ASC',
    		'orderby'    => 'menu_order ID',
    		'id'         => $post ? $post->ID : 0,
    		'itemtag'    => 'li',
    		'icontag'    => 'figure',
    		'captiontag' => 'figcaption',
    		'columns'    => 0,
    		'size'       => 'full',
    		'include'    => '',
    		'exclude'    => '',
    		'itemClient' => 'Client',
    		'itemAgency' => 'Agency'
    	), $attr, 'gallery'));
    
    	$id = intval($id);
    	if ( 'RAND' == $order )
    		$orderby = 'none';
    
    	if ( !empty($include) ) {
    		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    
    		$attachments = array();
    		foreach ( $_attachments as $key => $val ) {
    			$attachments[$val->ID] = $_attachments[$key];
    		}
    	} elseif ( !empty($exclude) ) {
    		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    	} else {
    		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    	}
    
    	if ( empty($attachments) )
    		return '';
    
    	if ( is_feed() ) {
    		$output = "\n";
    		foreach ( $attachments as $att_id => $attachment )
    			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
    		return $output;
    	}
    
    	$itemtag = tag_escape($itemtag);
    	$captiontag = tag_escape($captiontag);
    	$icontag = tag_escape($icontag);
    
    	$columns = intval($columns);
    	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    	$float = is_rtl() ? 'right' : 'left';
    
    	$selector = "gallery-{$instance}";
    
    	$size_class = sanitize_html_class( $size );
    	$gallery_div = "<section class='projects'><ul>";
    	$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
    
    	$i = 0;
    	foreach ( $attachments as $id => $attachment ) {
    		if ( ! empty( $attr['link'] ) && 'file' === $attr['link'] )
    			$image_output = wp_get_attachment_link( $id, $size, false, false );
    		elseif ( ! empty( $attr['link'] ) && 'none' === $attr['link'] )
    			$image_output = wp_get_attachment_image( $id, $size, false );
    		else
    			$image_output = wp_get_attachment_link( $id, $size, true, false );
    
    		$image_meta  = wp_get_attachment_metadata( $id );
    
    		$orientation = '';
    		if ( isset( $image_meta['height'], $image_meta['width'] ) )
    			$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
    
    		$output .= "<{$itemtag}>";
    		$output .= "
    			<{$icontag}>
    				$image_output
    				<{$captiontag}></{$captiontag}>
    			</{$icontag}>";
    		if ( $captiontag && trim($attachment->post_excerpt) ) {
    			$output .= "
    				<{$captiontag} class='wp-caption-text gallery-caption'>
    				<span>$itemClient</span>
    				" . wptexturize($attachment->post_excerpt) . "
    				<span>$itemAgency</span>
    				" . wptexturize($attachment->post_excerpt) . "
    				</{$captiontag}>";
    		}
    		$output .= "</{$itemtag}>";
    		if ( $columns > 0 && ++$i % $columns == 0 )
    			$output .= '<br style="clear: both" />';
    	}
    	$output .= "
    			<br style='clear: both;' />
    		</ul></section>\n";
    
    	return $output;
    }

    html структура

    <figure>
    	<a class="fancybox" data-fancybox-group="campaigns" href="images/img-campaigns-1.jpg">
    		<img src="images/img-campaigns-1.jpg" alt="" width="670" height="520">
    	</a>
    	<figcaption><span>Client:</span> Defence Force <span>Agency:</span> GPY&R Melbourne</figcaption>
    </figure>

Просмотр 2 ответов — с 1 по 2 (всего 2)
Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Тема «2 Custom fields in Media Uploader» закрыта для новых ответов.