Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds a custom WDCC Post Type and it's respective taxonomies and the wdcc-setup and wdcc-entries for Meta Boxes.
<?php
// =============================================================================
// FUNCTIONS/GLOBAL/ADMIN/META/ENTRIES.PHP
// -----------------------------------------------------------------------------
// Registers the meta boxes for pages, posts, and portfolio items.
// =============================================================================
// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
// 01. WDCC Items
// =============================================================================
// WDCC Items
// =============================================================================
function x_add_wdcc_item_meta_boxes() {
$meta_box = array(
'id' => 'wdcc-meta-box-winkel-prijs',
'title' => __('Te Koop op', '__wdcc__'),
'description' => __('Voeg een winkel en prijs toe', '__wdcc__'),
'page' => 'wdcc',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __('Winkel', '__wdcc__'),
'desc' => __('Kies een winkel', '__wdcc__'),
'id' => '_wdcc_winkel_keuze',
'type' => 'wdcc-winkels',
'std' => '',
),
),
);
wdcc_add_meta_box($meta_box);
}
add_action('add_meta_boxes', 'x_add_wdcc_item_meta_boxes');
<?php
// =============================================================================
// INC/POST-TYPES/WDCC-POST-TYPE.PHP
// -----------------------------------------------------------------------------
// Sets up custom post types for WDCC Figures.
// =============================================================================
// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
// 01. WDCC Custom Post Type
// 02. Add Thumbnails to the Admin Screen
// =============================================================================
// WDCC Custom Post Type
// =============================================================================
function wdcc_type_init() {
$slug = 'wdcc';
$menu_icon = ( floatval( get_bloginfo( 'version' ) ) >= '3.8' ) ? get_stylesheet_directory_uri() . '/assets/img/admin/wdcc-icon.png' : NULL;
//
// Enable the custom post type.
//
$labels = array(
'name' => __( 'WDCC', '__wdcc__' ),
'singular_name' => __( 'WDCC Item', '__wdcc__' ),
'add_new' => __( 'Add New Item', '__wdcc__' ),
'add_new_item' => __( 'Add New WDCC Item', '__wdcc__' ),
'edit_item' => __( 'Edit WDCC Item', '__wdcc__' ),
'new_item' => __( 'Add New WDCC Item', '__wdcc__' ),
'view_item' => __( 'View Item', '__wdcc__' ),
'search_items' => __( 'Search WDCC', '__wdcc__' ),
'not_found' => __( 'No wdcc items found', '__wdcc__' ),
'not_found_in_trash' => __( 'No wdcc items found in trash', '__wdcc__' )
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'author', 'custom-fields', 'revisions' ),
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array( 'slug' => $slug, 'with_front' => false ),
'menu_position' => 5,
'menu_icon' => $menu_icon,
'has_archive' => true
);
$args = apply_filters( 'wdcc_args', $args );
register_post_type( 'wdcc', $args );
}
add_action( 'init', 'wdcc_type_init' );
function wdcc_taxonomies_init() {
$taxonomies = array(
array(
'slug' => 'wdcc-series',
'single_name' => 'Serie',
'plural_name' => 'Series',
'post_type' => 'wdcc',
'has_ui' => true,
'hierarchical' => true,
),
array(
'slug' => 'wdcc-editions',
'single_name' => 'Edition',
'plural_name' => 'Editions',
'post_type' => 'wdcc',
'has_ui' => true,
'hierarchical' => true,
),
array(
'slug' => 'wdcc-winkels',
'single_name' => 'Winkel',
'plural_name' => 'Winkels',
'post_type' => 'wdcc',
'has_ui' => true,
'hierarchical' => false,
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['single_name'],
'search_items' => 'Search ' . $taxonomy['plural_name'],
'all_items' => 'All ' . $taxonomy['plural_name'],
'parent_item' => 'Parent ' . $taxonomy['single_name'],
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
'edit_item' => 'Edit ' . $taxonomy['single_name'],
'update_item' => 'Update ' . $taxonomy['single_name'],
'add_new_item' => 'Add New ' . $taxonomy['single_name'],
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
'menu_name' => $taxonomy['plural_name'],
);
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : false;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => $taxonomy['has_ui'],
'query_var' => true,
'rewrite' => $rewrite,
));
}
}
add_action('init', 'wdcc_taxonomies_init');
// Add Thumbnails to the Admin Screen
// =============================================================================
function wdcc_add_thumbnail_column( $columns ) {
$thumb = array( 'thumb' => __( 'Thumbnail', '__wdcc__' ) );
$columns = array_slice( $columns, 0, 2 ) + $thumb + array_slice( $columns, 1 );
return $columns;
}
function wdcc_add_thumbnail_column_content( $column ) {
if ( $column == 'thumb' ) {
echo '<a href="' . get_edit_post_link() . '">' . get_the_post_thumbnail( get_the_ID(), array( 200, 200 ) ) . '</a>';
}
}
add_filter( 'manage_wdcc_posts_columns', 'wdcc_add_thumbnail_column', 10, 1 );
add_action( 'manage_wdcc_posts_custom_column', 'wdcc_add_thumbnail_column_content', 10, 1 );
<?php
// =============================================================================
// FUNCTIONS/GLOBAL/ADMIN/META/WDCC-SETUP.PHP
// -----------------------------------------------------------------------------
// Sets up custom meta boxes utilized throughout the theme in various areas.
// =============================================================================
// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
// 01. Set Paths
// 02. Add WDCC Meta
// 03. Create Meta Entries
// 04. Save Meta Entries
// 05. Include Entry and Taxonomy Meta Box Setup
// =============================================================================
// Set Paths
// =============================================================================
$child_meta_path = get_stylesheet_directory() . '/framework/functions/global/admin/meta';
// Add WDCC Meta
// =============================================================================
function wdcc_add_meta_box($meta_box) {
if (!is_array($meta_box)) {
return false;
}
$callback = create_function('$post,$meta_box', 'wdcc_create_meta_box($post, $meta_box["args"]);');
add_meta_box($meta_box['id'], $meta_box['title'], $callback, $meta_box['page'], $meta_box['context'], $meta_box['priority'], $meta_box );
}
// Create Meta Entries
// =============================================================================
function wdcc_create_meta_box($post, $meta_box) {
if (!is_array($meta_box)) {
return false;
}
if (isset($meta_box['description']) && $meta_box['description'] != '') {
echo '<p>' . $meta_box['description'] . '</p>';
}
wp_nonce_field(basename(__FILE__), 'x_meta_box_nonce');
echo '<table class="form-table x-form-table">';
foreach ($meta_box['fields'] as $field) {
$meta = ($post->post_status == 'auto-draft') ? $field['id'] : get_post_meta($post->ID, $field['id'], true);
echo '<tr><th><label for="' . $field['id'] . '"><strong>' . $field['name'] .'</strong>
<span>' . $field['desc'] . '</span></label></th>';
switch ($field['type']) {
case 'wdcc-winkels':
$winkels = get_terms('wdcc-winkels');
$meta = ($meta == '') ? array(0 => 'Alle Winkels') : $meta;
echo '<table id="winkelsTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Store</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Store</th>
<th>Price</th>
</tr>
</tfoot>
<tbody></tbody>
</table>';
echo '<td><select name="x_meta[' . $field['id'] . '][]" id="' . $field['id'] . '">';
echo '<option value="Alle Winkels" ' . selected($meta[0], 'Alle Winkels', true) . '>Alle Winkels</option>';
foreach ($winkels as $winkel) {
echo '<option value="' . $winkel->name . '"';
if (in_array($winkel->term_id, $meta)) echo ' selected="selected"';
echo '>' . $winkel->name . '</option>';
}
echo '</select></td>';
echo '<input id="prijsText" type="text" value="" placeholder="price" />';
echo '<a href="#" id="addWinkel">+ Add Store Price</a>';
?>
<script>
jQuery(document).ready(function($) {
var t = jQuery('#winkelsTable').DataTable({
"searching": false,
"paging": false,
});
jQuery('#addWinkel').on('click', function(e) {
e.preventDefault();
var store = $('#<?php echo $field["id"] ?>').val();
var price = $('#prijsText').val();
t.row.add([
store,
price,
]).draw(false);
});
});
</script>
<?php
break;
default :
do_action( 'x_add_meta_box_field_type', $field['type'] );
break;
}
echo '</tr>';
}
echo '</table>';
}
// Save Meta Entries
// =============================================================================
function wdcc_save_meta_box($post_id) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if (!isset($_POST['x_meta']) || !isset($_POST['x_meta_box_nonce']) || !wp_verify_nonce($_POST['x_meta_box_nonce'], basename(__FILE__))) {
return;
}
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) return;
} else {
if (!current_user_can('edit_post', $post_id)) return;
}
foreach( $_POST['x_meta'] as $key => $val ) {
if ( is_array( $val ) ) {
update_post_meta( $post_id, $key, $val );
} else {
update_post_meta( $post_id, $key, stripslashes( htmlspecialchars( $val ) ) );
}
}
}
add_action('save_post', 'wdcc_save_meta_box');
// Include Entry and Taxonomy Meta Box Setup
// =============================================================================
require_once( $child_meta_path . '/wdcc-entries.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.