The complete guide to customising the default post _type

Posted on 1st October, 2013 Leave a Comment

Sometimes rather than creating a custom post type with custom taxonomies, we can just edit the default Posts section in WordPress. With that in mind I thought that I’d create a complete “snippets” guide on how to edit the default WordPress post_type “Posts”.

 In this example I’m renaming the “Posts” to “Developments”.

Quick Menu:

Remember to share if you like this post!


Start by renaming the posts admin menu and removing the unused menu items.

Rename the Posts menu items titles, this includes the categories and tags.

Everything on this page belongs in the functions.php!.

/**
 * Rename the Posts to Developments
 *
 */
function edit_post_menu(){
	 global $menu;
	 global $submenu;

	$menu[5][0] = 'Developments'; // Change posts to developments
	$submenu['edit.php'][5][0] = 'All Developments';
	$submenu['edit.php'][10][0] = 'Add a Development';
	$submenu['edit.php'][15][0] = 'Post Codes'; // Rename categories to Post Codes
	$submenu['edit.php'][16][0] = 'Areas'; // Rename tags to Areas

	remove_menu_page('tools.php'); // Remove the unused menu items
	remove_menu_page('edit-comments.php');
	remove_submenu_page('themes.php' , 'nav-menus.php'); //Remove the unused sub-menu items
	remove_submenu_page('plugins.php' , 'plugin-editor.php');
}
add_action( 'admin_menu' , 'edit_post_menu' );

Then lets change the Post Labels

Post labels appear above the edit posts as the titles. As an example when you edit a post it will show Edit Post as the title.

/**
 * Change the post type labels
 */
function change_post_type_labels() {
  global $wp_post_types;

  // Get the post labels
  $postLabels = $wp_post_types['post']->labels;
  $postLabels-> name = 'Developments';
  $postLabels-> singular_name = 'Development';
  $postLabels-> add_new = 'Add New Development';
  $postLabels-> add_new_item = 'Add New Development';
  $postLabels-> edit_item = 'Edit Developments';
  $postLabels-> new_item = 'Developments';
  $postLabels-> view_item = 'View Developments';
  $postLabels-> search_items = 'Search Developments';
  $postLabels-> not_found = 'No Developments found';
  $postLabels-> not_found_in_trash = 'No Developments found in Trash';
}
add_action( 'init', 'change_post_type_labels' );

Amend the “Enter Title Here” text.

This shouldn’t need an explanation, but it is the first box above the text editor.

/**
 * Change the "Enter Title Here" text
 */
 function title_text_input( $title ){
     return $title = 'Enter Development Title';
}
add_filter( 'enter_title_here', 'title_text_input' );

Modify “Add Featured Image” text.

/**
 * Change the "Add Featured Image" text
 */

function development_image_text( $content ) {
    return $content = str_replace( __( 'Set featured image' ), __( 'Set Development Image' ), $content);
}
add_filter( 'admin_post_thumbnail_html', 'development_image_text' );

Rename the Category, Tag and Featured Image headers

These titles appear above the modules whilst you’re editing the post. Each one should be pretty self explanatory.

/**
 * Rename the Category, Tag and Featured Image headers in the edit post page
 */

 // Categories
add_action( 'add_meta_boxes', 'change_cat_meta_box', 0 );
function change_cat_meta_box() {
		global $wp_meta_boxes;

	unset( $wp_meta_boxes['post']['side']['core']['categorydiv'] );
	add_meta_box('categorydiv', //The Meta Parimeter - check here http://codex.wordpress.org/Function_Reference/remove_meta_box
	__('Post Code'), // New Label
	'post_categories_meta_box', // The function - check here http://phpxref.ftwr.co.uk/wordpress/nav.html?_functions/index.html
	'post',
	'side',
	'low');
}
// Tags
 add_action( 'add_meta_boxes', 'change_tag_meta_box', 0 );
function change_tag_meta_box() {
		global $wp_meta_boxes;

	unset( $wp_meta_boxes['post']['side']['core']['tagsdiv-post_tag'] );
	add_meta_box('tagsdiv-post_tag',
	__('Area'),
	'post_tags_meta_box',
	'post',
	'side',
	'low');
}
//Featured Image
add_action( 'add_meta_boxes', 'change_featured_image_meta_box', 0 );
function change_featured_image_meta_box() {
		global $wp_meta_boxes;

	unset( $wp_meta_boxes['post']['side']['core']['postimagediv'] );
	add_meta_box('postimagediv',
	__('Development Image'),
	'post_thumbnail_meta_box',
	'post',
	'side',
	'low');
}

Remove the unused metaboxes

You may not want all the extra metaboxes that appear on the posts admin pages. So let’s remove them completely.

/**
 * Remove the unused metaboxes
 */
 function remove_my_post_metaboxes() {
	remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
	remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
	remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
	remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
	remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
	remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
	remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
	remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
}
add_action('admin_menu','remove_my_post_metaboxes');

Add a custom helper content to the help tab

This is handy for giving the user a quick reference guide to adding a new post. For this example I’m going to use an ordered list to show them how to add all the relevant content needed to display the post or in this particular occasion “development”.

/**
 * Include a help menu to the posts/developments
 */
add_action( 'add_meta_boxes' , function () {
   $the_help_tab_content =  '<h3>' . __( 'Adding a New Develpment') . '</h3>'.
   							'<ol>' .
   							'<li>' . __( 'Enter the development name as the main title. &nbsp;&nbsp;&nbsp;<i>ex. Avant Garde</i>') . '</li>'.
   							'<li>' . __( 'Enter the developer in the text editor. &nbsp;&nbsp;&nbsp; <i>ex. Hurford Salvi Carr</i>') . '</li>'.
   							'<li>' . __( 'Select one Post Code on the right') . '</p>'.
   							'<li>' . __( 'Below that add the areas seperated by commas,  &nbsp;&nbsp;&nbsp;<i>ex. Bloomsbury, Clerkenwell, City</i>') . '</li>'.
   							'<li>' . __( 'Custom Fields: Select primary from the dropdown then add the main area for the development. &nbsp;&nbsp;&nbsp; <i>ex. Clerkenwell</i>') . '</li>'.
   							'<li>' . __( 'Upload the development image as the featured image. ') . '</li>'.
   							'<li>' . __( 'Publish the development.') . '</li>'.
							'</ol>' ;
   get_current_screen()->add_help_tab( array(
			'id'      => 'my-help-id',
			'title'   => __( 'The Help Tab' ),
			'content' => $the_help_tab_content,
		) );
}, 10 , 2 );

Disable the Visual editor

Sometimes the visual editor is overcomplicated for what we need so we might need to disable it completely.

add_filter('user_can_richedit' , create_function('' , 'return false;') , 50);

We can also add some custom CSS to the post_type

I’m using css content to replace the titles of the modules and remove the unused buttons from the plain text editor.

/**
 * Quickly change the edit text box height, remove unused buttons from the text editor and add some custom titles to the modules
 */
add_action('admin_head', 'wpt_include_custom_styles');
function wpt_include_custom_styles() {
	echo'<style type="text/css">
			/* Change the height of the text editor */
			#content{ height:100px !important; }
			/* Remove unwanted buttons */
			#qt_content_block, #qt_content_strong, #qt_content_em, #qt_content_link, #qt_content_del,
			#qt_content_ins, #qt_content_img, #qt_content_ul, #qt_content_ol, #qt_content_li,
			#qt_content_code, #qt_content_more, #qt_content_close, #qt_content_hed_tabs,
			#qt_content_tag, #qt_content_fullscreen { display: none !important; }
			/* Add our new titles */
			#ed_toolbar:before {content: "Add the Developer"; font-size: 1.4em; line-height: 2em;}
			#postcustom .hndle > span {	display: none;}
			#postcustom .hndle:before { content: "Select Primary from the dropdown and add the Primary Area. (ex.Clerkenwell)"; font-size: 1em; line-height: 1.2em;	}

		</style>';
}

Add buttons to the visual editor

We may also need to add some buttons with specific functions to the visual editor, this has been widely covered online but is essential part of editing the post_type.

We’re creating an extra dropdown list for all our new styles and removing some of the unwanted style options from the current visual editor:

/**
 * TinyMCE - Remove unwanted options and add some of our own!
 */

function make_mce_awesome( $init ) {
    $init['theme_advanced_blockformats'] = 'h2,h3,h4,p'; /* Include these block formats */
    $init['theme_advanced_disable'] = 'underline,spellchecker,wp_help'; /* Remove these from the editor */
    $init['theme_advanced_text_colors'] = '46a3d5,f9a936,b9b9b9'; /* Add site specific colours */
    $init['theme_advanced_buttons2_add'] = 'styleselect'; /* Include a styles dropdown */
    return $init;
}

add_filter('tiny_mce_before_init', 'make_mce_awesome');
/* Include a custom stylesheet for the visual editor*/
	add_editor_style('new-editor-styles.css'); 

add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );
function my_mce_before_init( $settings ) {
    $style_formats = array(
	array(/*adds a class to an anchor tag*/
    		'title' => 'Lora',
    		'selector' => 'a',
    		'attributes' => array(
					'class'	=>	'lora'
				)
    	),

    	array(/*adds class to an img*/
    		'title' => 'clearfix',
    		'selector' => 'img',
    		'attributes' => array(
					'class'	=>	'clearfix'
				)
    	),

	array(/*adds an attribute to an anchor*/
    		'title' => 'nofollow',
    		'selector' => 'a',
    		'attributes' => array(
					'rel'	=>	'nofollow'
				)
    	),

	array(/*adds a target attribute to an anchor*/
    		'title' => 'target',
    		'selector' => 'a',
    		'attributes' => array(
					'target'	=>	'new'
				)
    	),

    	array(/*add an anchor complete with link, class and attribute*/
    		'title' => 'Demo Button',
    		'inline' => 'a',
				'attributes' => array(
					'href' => 'http://demo.wptricks.co.uk/',
					'class' => 'button',
					'rel' => 'nofollow'
				)
    	),

	array(/*create a span with an attribute*/
    		'title' => '* span',
    		'inline' => 'span',
				'attributes' => array(
					'class' => 'org',
				)
    	),

        array(/*an a span with specific styles*/
        	'title' => 'Bold Lora Text',
        	'inline' => 'span',
        	'styles' => array(
        		'color' => '#666',
        		'fontWeight' => 'bold',
			'fontFamily' => 'Lora'
        	)
        )
    );

    $settings['style_formats'] = json_encode( $style_formats );
    return $settings;
}

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]