get_files( 'php', 1 ); $nusantara_templates = array(); $base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory())); foreach ((array)$templates as $template) { $template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); $nusantara_name = str_replace($base, '', $template); if (false !== strpos($nusantara_name, '/template-style')) continue; $template_data = implode('', file( $template )); $name = ''; if (preg_match( '|Template Style:(.*)$|mi', $template_data, $name)) $name = _cleanup_header_comment($name[1]); if (!empty($name)) { if(basename($template) != basename(__FILE__)) $nusantara_templates[trim($name)] = $nusantara_name; } } return $nusantara_templates; } // -- Function Name : nusantara_post_templates_dropdown // -- Params : // -- Code Reference: function nusantara_templates_dropdown() { global $post; $nusantara_templates = nusantara_templates(); foreach ($nusantara_templates as $template_name => $template_file) { if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = 'selected'; } else { $selected = ''; } $opt = ''; echo $opt; } } // -- Function Name : nusantara_post_template // -- Params : $template // -- Code Reference: function nusantara_post_template($template) { global $post; $custom_field = get_post_meta($post->ID, '_wp_post_template', true); if(!empty($custom_field) && file_exists(get_template_directory() . "/{$custom_field}")) { $template = get_template_directory() . "/{$custom_field}"; } return $template; } add_filter('single_template', 'nusantara_post_template'); // -- Function Name : nusantara_custom_box // -- Params : // -- Code Reference: function nusantara_add_custom_box() { if(nusantara_templates() ) { add_meta_box( 'nusantara_post_template', __( 'Template Style', 'nusantara' ), 'nusantara_custom_box', 'post', 'normal', 'high' ); add_meta_box( 'nusantara_post_template', __( 'Template Style', 'nusantara' ), 'nusantara_custom_box', 'portfolio', 'side', 'high' ); } } add_action('add_meta_boxes', 'nusantara_add_custom_box'); // -- Function Name : nusantara_custom_box // -- Params : // -- Code Reference: function nusantara_custom_box() { global $post; wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_nonce' ); echo ''; echo '
'; echo ''; echo '

' . __('You can select your template style', 'nusantara' ) . '

'; } // -- Function Name : nusantara_template_save // -- Params : $post_id, $post // -- Code Reference: function nusantara_template_save($post_id, $post) { global $nusantara_key; if (!isset( $_POST[ 'custom_meta_box_nonce' ] ) || !wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)) ) { return $post->ID; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if ( 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post->ID )) return $post->ID; } else { if ( !current_user_can( 'edit_post', $post->ID )) return $post->ID; } $nusantara_key['_wp_post_template'] = $_POST['_wp_post_template']; foreach ($nusantara_key as $key => $value) { if( $post->post_type == 'revision' ) return; $value = implode(',', (array)$value); if(get_post_meta($post->ID, $key, FALSE)) { update_post_meta($post->ID, $key, $value); } else { add_post_meta($post->ID, $key, $value); } if(!$value) delete_post_meta($post->ID, $key); } } add_action('edit_post','nusantara_save_postdata'); add_action('save_post', 'nusantara_template_save', 1, 2);