WordPress Changing Pluggable Functions Without Child Theme

A new concept (?) – to change a pluggable function of a WordPress theme without creating a child theme.

Example – to remove the  post tags output from the posts of Twenty Twelve.

Often, these things can be done by editing the styles of the theme, however in this case, CSS would not work as the tags don’t have their own CSS class.

Creating a child theme is usually recommended as the easiest option; but this might be over the top if the only changed paremeters are from some pluggable functions.

The alternative way would be to create a plugin to alter (in this example) the post-meta-data output, which is done through the function ‘twentytwelve_entry_meta()’.

How to:
use FTP to create a file, for example with the file name twentytwelve-entry-meta-modified.php, in the /wp-content/plugins/ folder; and add this example code (http://pastebin.com/7dMVvC9E)

<?php
/*
Plugin Name: Entry Meta Twenty Twelve Modified
Plugin URI: n/a
Description: personalization of the entry meta of Twenty Twelve without child theme; in this particular example the removal of the tags.
Version: 1.0
Author: alchymyth
Author URI: http://transformationpowertools.com
License: GPL2
*/

add_action( 'setup_theme', 'modify_pluggable_theme_functions' );

function modify_pluggable_theme_functions() {

if( wp_get_theme() == 'Twenty Twelve' && get_stylesheet_directory() == get_template_directory() ) :
//check for the right theme and that no child theme is used
function twentytwelve_entry_meta() {
// Translators: used between list items, there is a space after the comma.
$categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time datetime="%3$s">%4$s</time></a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);

$author = sprintf( '<span><a href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
get_the_author()
);

if ( $categories_list ) {
$utility_text = 'This entry was posted in %1$s on %2$s<span> by %3$s</span>.';
} else {
$utility_text = 'This entry was posted on %2$s<span> by %3$s</span>.';
}

printf(
$utility_text,
$categories_list,
$date,
$author
);
}
endif;
}
?>

The code checks that Twenty Twelve is the active theme, and that no child theme is activated.

Activate the plugin ‘Entry Meta Twenty Twelve Modified’ as usual from within dashboard – plugins.

http://codex.wordpress.org/Pluggable_Functions
http://codex.wordpress.org/FTP_Clients
http://codex.wordpress.org/Plugins
http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Plugin_API/Action_Reference

Mark Comments by Current User

To highlight comments of the currenly logged-in user, add this plugin:

<?php
/*
Plugin Name: Comment Class for Logged-In User
Plugin URI: n/a
Description: add the comment_class .comment-author-logged-in for the comments of logged-in users.
Version: 1.0
Author: alchymyth
Author URI: http://transformationpowertools.com
License: GPL2
*/

add_filter( 'comment_class', 'comment_class_logged_in_user' );

function comment_class_logged_in_user( $classes ) {
	global $comment;
	if ( $comment->user_id > 0 && is_user_logged_in() ) {  
		global $current_user; get_currentuserinfo(); 
		$logged_in_user = $current_user->ID;  
		if( $comment->user_id == $logged_in_user ) $classes[] = 'comment-author-logged-in';
	}
return $classes;
}

//  requires formatting of the css class: 
//  .comment-author-logged-in { }
?>

Add a file with the above code into the /wp-content/plugins folder; activate the plugin under ‘appearance – plugins – installed plugins’; and add a corresponding style to the stylesheet of the currently used theme.

The code compares the comment author’s ID with the ID of the logged-in user, and adds the class .comment-author-logged-in to the ‘comment_class’ output.

Title Attribute for Post Thumbnail

two men shake hands - bronze statues in Calagary/BCThe ‘featured image’ in WordPress automatically outputs the image alt attribute, but no title attribute.

Those two attributes are used for different reasons:
– the alt attribute is used to output alternative information if the image is inaccessible, for instance if the user has opted not to show images in the site, or for screen readers for accessibility reasons;
– the title attribute would present additional information about the image, and is shown by some browsers when you hover over the image.

For a WordPress ‘featured image’, the code below is taking the image title as you see it in the media library,  to be used as the title attribute.

The influence on search engine optimisation is not clear – it seems that the alt attribute is given more importance than the title attribute.

Back to the coding – To add the image title of the featured image as the title attribute to the img tag, a little filter function is needed, added  to the  ‘post_thumbnail_html’ filter.

Instead of just adding the code to functions.php of your theme, where it would get lost when you switch themes, it is more universal to create a basic plugin:

<?php
/*
Plugin Name: Featured Image Title Attribute Output
Plugin URI: n/a
Description: adds the image title as the title attribute to the 'featured image' output.
Version: 1.0
Author: alchymyth
Author URI: http://www.transformationpowertools.com/wordpress
License: GPL2
*/

add_filter('post_thumbnail_html','add_post_thumbnail_title_attribute',10,3);

function add_post_thumbnail_title_attribute($html, $post_id, $post_thumbnail_id) {

if( $html == '' ) {
return $html;
} else {
$out = $html;
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));

if ($thumbnail_image && isset($thumbnail_image[0])) {

$title = $thumbnail_image[0]->post_title;
if($title) {
$output = ' title="' . esc_attr($title) . '" ';
$out = str_replace( 'class=', $output . 'class=', $html );
}
}
}
return $out;
}
?>

Upload this as a appropriately named file into the plugins folder of your WordPress installation, and activate it like any other plugin from dashboard – plugins.

Defining the writing area of the dashboard editor

the first of my upcoming ‘tips and tricks’ – a handy function to style the editor area of your self-hosted wordpress blog or cms:
if you are working on a modern widescreen monitor, but want to restrict the wordpress editor area (your writing paper, so to speak) to the width as it will later show in your blog, here is a tip what to add to functions.php of your theme:

function custom_max_width_editor() {
echo '<style type="text/css"> textarea#content, #content_ifr {
max-width:580px;
min-height:400px;
border-right: 1px solid #ddd; }</style>';
}
add_action('admin_head', 'custom_max_width_editor');

the ‘min-height’ is optional, and will give you a larger writing area to start with.
and the border shows clearly how wide the defined writing area is.
adjust the number values, and enjoy the next level of control over your wordpress.

you might even want to add the predominant background-color of your blog’s content area …

ps:
a big thank you to @Purren from the wordpress support forum, who submitted a correction to make it also work for the visual editor.

pps:
wp3 has brought the ‘add_editor_style()‘ function which makes the above tip redundant.

ppps:
for theme independant, more permament changes, the principle can also be turned into a basic plugin; in this example to change the font-size in the html editor:

<?php
/*
Plugin Name: Html Editor Font Size
Description: set the font size for the html editor.
License: GPL2
*/
function custom_font_size_html_editor() {
echo '<style type="text/css"> #editorcontainer textarea#content {
font-size: 20px; }</style>';
}
add_action('admin_head', 'custom_font_size_html_editor');
?>