Advent Calendar- How to disable comments for WordPress pages in any theme

Usually you don’t need comments on pages. Unfortunately, WordPress doesn’t offer a separate option to leave comments on posts on and turn them off for pages. If your theme calls comments_template(); in its page.php and you don’t want to break automatic updates, you cannot just remove the function call, because it will be overwritten with the next update.

No problem. There’s an hook for that. It’s a filter called … wait for it … comments_template. We can change the path to the comments template here – if haven’t figured that out already. Let’s build a small plugin.

So, what do we do? We hook into comments_template(); and change the path. Our new path should point to an existing file without any HTML output. In this case, we use just our plugin file, because we know it exists and we control the output.

As you may have noticed our plugin file is included two times: First as a plugin, later as a replacement for comments.php. The function_exists() check prevents any You cannot redeclare … errors.

Putting this all together …

<?php # -*- coding: utf-8 -*-
/**
Plugin Name: Disable Comments On Pages
Version:     1.0
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL
*/
// This file is not called from WordPress. We don't like that.
! defined( 'ABSPATH' ) and exit;

// If the function exists this file is called as comments template.
// We don't do anything then.
if ( ! function_exists( 't5_disable_comments_on_pages' ) ) {
	/**
	 * Replaces the path of the original comments template with this
	 * file's path on pages.
	 *
	 * @param  string $file Original comments template file path.
	 * @return string
	 */
	function t5_disable_comments_on_pages( $file ) {
		return is_page() ? __FILE__ : $file;
	}

	add_filter( 'comments_template', 't5_disable_comments_on_pages', 11 );
}

Plugin to Remove Comments Completely from WordPress

The comment feature in WordPress is an essential component of blogs - but not in all cases it's needed, especially if you use it as a traditional CMS.

Sure, you can just leave out the comment form while you creating a theme and disable the comment options in your backend. But this is not the best way for everyone. In such a case, I turn the comments off completely, making sure that the posts can not have that option and also remove the fields for the comments in the backend. That way the user gets exactly what he needs. Meanwhile, I have needed this so often that I created a small Plugin and I'm able to immediately „switch off“ the functions and comment areas. If there are major adaptations, I create them via Plugin or use my existing Plugin Adminimize.
Continue reading …

New Plugin to Style your Plugin on WordPress Admin with Default Styles!

WordPress is developing fast - this also applies to the design of the backend. So it is important not to use your own styles in the admin area and use tags and classes of WordPress. This is the best way you can simplify your work as a developer and you don't have to test the design with every update. Unfortunately, there are quite extensive opportunities in the backend to implement the requirements. Several different classes and HTML structures are used. To be able to look up something this simple, I have developed a small Plugin, which tinkers in the development environment and quickly represents the necessary elements. Here you see two screenshots with the differences between version 3.1 and 3.2 of WordPress and the current contained elements of the Plugin.
Continue reading …

Plugin To Add Custom Field To An Attachment In WordPress

5 Day
The media library is on WordPress in some areas, certainly not perfect, but it has great potential. Each attachment can contain various metadata and also WordPress stores some data directly while uploading - for example a part of the Exif data of an image.

However, it might not be enough for you and you need some additional custom data with the attachment, whether in order to control something in the backend of WordPress, or even display the data in the front end to the image. So here is a small tutorial how to add an additional field. You can use this solution directly and adding a new field with the help of a Plugin.
Continue reading …