WordPress custom post type name length longer than 20 characters

WordPress is standard using a max of 20 characters for the custom post type name. You can add the code for a new custom post type in your functions.php but if the name is longer than 20 characters WordPress doesn’t accept it.

I’ve searched for it and found the solution to get a custom post type with a name longer than 20 characters:

  1. Open in /wp-includes the post.php file and search for the following line:
    if ( strlen( $post_type ) > 20 )
    return new WP_Error( ‘post_type_too_long’, __( ‘Post types cannot exceed 20 characters in length’ ) );
  2. Change this line to: (I took 50 characters in my example)
    if ( strlen( $post_type ) > 50 )
    return new WP_Error( ‘post_type_too_long’, __( ‘Post types cannot exceed 50 characters in length’ ) );
  3. Then open your database and edit the wp_posts > post_type column from 20 characters to 50 characters.

Remember that if you update WordPress you need to do these steps every time.

That’s it!

Moving a WordPress website

I have moved several WordPress websites and this is my roadmap for it.

  1. Download Files from “old” WordPress website by FTP. (https://filezilla-project.org/)
  2. Download Database .sql “old” WordPress website.
  3. Make sure you have a new empty server package.
  4. Upload FTP files “old” WordPress website to “new” WordPress website except config.php.
  5. Import Database .sql file from “old” WordPress website to database “new” WordPress website.
  6. Connect config.php file to “new” database.
  7. Run the following SQL query on the database of “new” WordPress website for changing the Site / Home URL:
    UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com') WHERE option_name = 'home' OR option_name = 'siteurl';

That’s it!

Ultimate WordPress comments guide

There are many things you can do with WordPress comments. You can use for example default themes with default settings, Use linear or nested/threaded commenting. You can use social implementation, email verification, spam protection, show your comment form in a popup or add many extra features to optimise it exactly the way you want. You also have many ways to call the functions and show your comments and comment box.

In this guide I will show you all my tips and tricks that I have found during developing my WordPress projects and make one big guide with as much as possible information about WordPress commenting.

Play with WordPress comments

In WordPress comments you can choose for 2 options.

  1. Use default theme comments with save admin control and
  2. Play with your template and functions, to make it your own!

If you chose for the second I will show you how you can do it. (if you have better options? Please comment down)

First you need to make a file named comments.php. And call it with:

<?php comments_template(); ?>

This piece of code says to WordPress: “Take comments.php and show it”.

Then you need a comment template for example:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */

// Do not delete these lines
	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Please do not load this page directly. Thanks!');

	if ( post_password_required() ) { ?>
		<p>This post is password protected. Enter the password to view comments.</p>
	<?php
		return;
	}
?>

<!-- You can start editing here. -->

<?php if ( have_comments() ) : ?>
	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

	<ol>
		<?php wp_list_comments('type=comment&callback=advanced_comment'); //this is the important part that ensures we call our custom comment layout defined above 
                ?>
	</ol>
	<div></div>
	<div>
		<div><?php previous_comments_link() ?></div>
		<div><?php next_comments_link() ?></div>
	</div>
 <?php else : // this is displayed if there are no comments so far ?>

	<?php if ( comments_open() ) : ?>
		<!-- If comments are open, but there are no comments. -->

	 <?php else : // comments are closed ?>
		<!-- If comments are closed. -->
		<p class="nocomments">Comments are closed.</p>

	<?php endif; ?>
<?php endif; ?>

<?php if ( comments_open() ) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

<div>
	<small><?php cancel_comment_reply_link(); ?></small>
</div>

<?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
<p>You must be <a href="<?php echo wp_login_url( get_permalink() ); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( is_user_logged_in() ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : //this is where we setup the comment input forums ?>

<p><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Post" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>

</form>

<?php endif; // If registration required and not logged in ?>
</div>

<?php endif; // if you delete this the sky will fall on your head ?>

In this template are sevaral options, functions and if statements to make it a save and dynamic template. You can expand or minimalise it but I gave this as an example/base template.

If you look closely you see the comments being shown by

<?php wp_list_comments('type=comment&callback=advanced_comment');

In this function you see a “callback” function named “advanced_comment”. With this function we can say in functions.php how to show the comments.

Extra: There are more options for the <?php wp_list_comments(); ?> function:

<?php $args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => ''
); ?>

So we make a function in functions.php called: “advanced_comment” and use for example a template like this:

<?php 
function advanced_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>

<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
   <div>
     <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
       <div<a href="<?php the_author_meta( 'user_url'); ?>"><?php printf(__('%s'), get_comment_author_link()) ?></a></div>
       <small><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></small>
     </div>
     <div></div>

     <?php if ($comment->comment_approved == '0') : ?>
       <em><?php _e('Your comment is awaiting moderation.') ?></em>
       <br />
     <?php endif; ?>

     <div>	
         <?php comment_text() ?>
     </div>

   <div>
      <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
   </div>
   <div></div>
<?php } ?>

In this template you see:

  • comment_class(); This helps WordPress giving classes to the comment like: comment, even (or odd), thread-even, depth-1, etc.. so you can easy style them different ways.
  • get_avatar This shows an avatar picture of the author.
  • get_comment_date Gives the date when the comment is placed.
  • edit_comment_link This shows a link where the admin user can edit the comment directly.
  • if($comment->comment_approved == ‘0’) : This checks of the comment is approved and if not it gived the waiting for moderation text.
  • comment_text This shows the content of the comment.
  • comment_reply_link Shows a link for replying.

Important WordPress comment template tags

Template Tag Description
<?php comment_ID(); ?> The ID of a comment
<?php comment_author(); ?> The author of a comment
<?php comment_author_link(); ?> The author of a comment, wrapped with a link to his website if he specified one
<?php comment_type(); ?> The type of comment; pingback, trackback or a comment
<?php comment_text(); ?> The actual comment
<?php comment_date(); ?> The date it was posted
<?php comment_time(); ?> The time it was posted

Decide whether a comment is parent or child

If you want decide whether a comment is parent or child (for example if you want to style it another way) then you can check it by the following code:

WordPress comments custom post type

If you want to use comments in custom post types you first need to add “comments” to your custom post type.

Comment e-mail notification

If you want your visitors notificated after submitting a comment  you can use the following plugins:

Comment e-mail moderation

If you want to do other thing with your comment moderation e-mails you can use the following plugins:

Other Helpful comment plugins

Simple Questions and Answers

How to turn on nested/threaded comments in WordPress admin?

To turn on nested/threaded comment in your WordPress admin you go to settings->Discussion->Other comment settings->Enable threaded (nested) comments X levels deep.

Recources