Advanced Customization

This section shows you how to modify the Post Avatar plugin on your public site display and in the WordPress admin.

For Front End Display
For Administration Screens

For Front End Display

By default, the plugin hooks into the following filters: the_content() and the_excerpt().

OVERRIDE HTML DISPLAY USING FILTER HOOK: gklpa_the_postavatar

gklpa_the_postavatar:
       Parameters:
         $post_avatar_text – Original HTML to display
         $post_avatar – Post Avatar data in array format. The keys are:
             avatar_url – The URL to the image
             show_image_dim – 1 indicates to show image dimensions, 0 to hide them
             image_height – integer value of image height or null if image dimensions is turned off
             image_width – integer value of image width or null if image dimensions is turned off
             post_id – ID of current post
             post_title – Post title for the image attribute
             image_name – Image file name

       Returned Text: New HTML display

Example: Display a default image if no avatar is selected
This example makes use of the HTML/CSS settings defined by the site admin.

<?php 
	add_filter( 'gklpa_the_postavatar', 'prefix_show_default_image', 10, 2 );
	function prefix_show_default_image( $post_avatar_html, $post_avatar_array ){
		global $post, $gklpa_plugin_settings;
		
		// Display default image;
		if( is_null( $post_avatar_array ) ){
			if( !empty( $gklpa_plugin_settings['css_class'] ) {
				$css = 'class="' . $gkl_plugin_settings['css_class']. '"';
			}
			$post_avatar_html = $gklpa_plugin_settings['html_before' ] . '<img '. $css . ' src="http://wplatest.dev/images/default-image.jpg" alt="' . esc_attr(strip_tags($post->post_title) ) . '" />'. $gklpa_plugin_settings['html_after'];
		}
		return $post_avatar_html;
	}
?>

OVERRIDE HTML DISPLAY WITH CUSTOM CONTENT HOOK

If you want to change the HTML completely or override the option to display avatars automatically, use the remove_filter() like so:

<?php
remove_filter('the_content', 'gkl_postavatar_filter', 99 );
remove_filter('the_excerpt', 'gkl_postavatar_filter', 99 );
?>

You can then define your own the_content filter function that makes use of the gkl_postavatar() or gkl_get_postavatar() functions.

gkl_get_postavatar():
       Parameters:
         $post – Post object

       Returned Array:
         $post_avatar_array – Post Avatar data in array format. The keys are:
            avatar_url – The URL to the image
            show_image_dim – 1 indicates to show image dimensions, 0 to hide them
            image_height – integer value of image height or null if image dimensions is turned off
            image_width – integer value of image width or null if image dimensions is turned off
            post_id – ID of current post
            post_title – Post title for the image attribute
            image_name – Image file name

Example:

<?php
	add_filter( 'the_content', 'my_custom_post_avatar' );
	function my_custom_post_avatar( $content ){
		global $post;

		$current_avatar = gkl_get_postavatar( $post );
		$html_before = '<span class="alignleft">';
		$html_after = '</span>';
		// Display default image
		if( is_null( $current_avatar ) ) {
			$image_url = 'http://wplatest.dev/images/default-image.jpg';
			$alt_text = esc_attr(strip_tags($post->post_title) );
		} else {
			$image_url = $current_avatar['avatar_url'];
			$alt_text = $current_avatar['post_title'];
		}
		$post_avatar_html = $html_before . '<img src="'. $image_url . '" alt="' . $alt_text . '" />'. $html_after;
			
		return $post_avatar_html;	
	}
?>

OVERRIDE HTML DISPLAY WITH template tag gkl_postavatar

If you want the post avatar to appear outside of the content, e.g. with the entry’s meta information, make use of the gkl_postavatar() template tag.
It takes four paramters:
            class => CSS class to use in the tag.
            before => HTML to appear before the image.
            after => HTML to appear after the image.
            do_what => Use echo to display the post avatar, return to pass it to a variable. Defaults to echo.

e.g. In a template file:

<?php
	<div class="entry-meta">	
	<?php gkl_postavatar('', "<span class='alignleft'>", "<span>" );?>

	## more template tags here ##
	
	</div>
?>

Or you can make your own template tag function like in the example for “Override HTML display with custom content hook”, except you call the function directly in your template instead of hooking into the_content().

For Administration Screens

Add Post Avatar to Pages and Custom Post Types

Use the filter hook gklpa_allowed_post_types to add further post types that you want the Post Avatar selection to appear on.
It takes an array of post type slugs as a parameter.

<?php
	add_filter( 'gklpa_allowed_post_types', 'prefix_my_custom_post_types' );
	function prefix_my_custom_post_types( $current_post_types ){
		$current_post_types = array( 'post', 'page', 'review', 'event' );
		return $current_post_types;
	}
?>

Enable Image Selection for Folder Outside of WordPress Installation

By default, Post Avatar looks for your images folder in relation to your WordPress installation. If you want to move your folder elsewhere, use these pair of filter hooks:
gklpa_image_url and gklpa_image_dir. They take a single parameter: Image folder url and absolute path to the image folder, respectively.

<?php
	add_filter( 'gklpa_image_url', 'prefix_change_folder_url' );
	function prefix_change_folder_url( $current_url ){
		return esc_url( 'http://mysite.com/images/' );
	}

	add_filter( 'gklpa_image_dir', 'prefix_change_folder_dir' );
	function prefix_change_folder_dir ){
		return '/user/public_html/images/';
	}
?>

Back to Top

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>