Created
February 17, 2018 23:15
-
-
Save tradesouthwest/e3ce106574cfd3b64119754a6beac620 to your computer and use it in GitHub Desktop.
Replace WordPress Gravatar Upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replace default Gravatar with branding logo. | |
*/ | |
function wordness_new_default_gravatar($avatar_defaults) | |
{ | |
//be safe default | |
$def = esc_url( plugin_dir_url( dirname(__FILE__) ) | |
. 'css/poplogin-default-logo.png' ); | |
$defavatar = get_option( 'wordness_options' )['wordness_upload_logo']; | |
if($defavatar == '') $defavatar = esc_url( $def ); | |
$avatar_defaults[$defavatar] = "Default Gravatar"; | |
return $avatar_defaults; | |
} | |
add_filter( 'avatar_defaults', 'wordness_new_default_gravatar' ); | |
/** | |
* add_user_meta( $user_id, $meta_key, $meta_value, $unique ) | |
* https://developer.wordpress.org/plugins/users/working-with-user-metadata/ | |
* @hook edit_user_profile | |
*/ | |
/** | |
* The field on the editing screens. | |
* | |
* @param $user WP_User user object | |
*/ | |
add_action( 'show_user_profile', 'wordness_user_profile_avatar' ); | |
add_action( 'edit_user_profile', 'wordness_user_profile_avatar' ); | |
add_action( 'personal_options_update', 'wordness_save_extra_user_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'wordness_save_extra_user_profile_fields' ); | |
function wordness_user_profile_avatar( $user ) | |
{ | |
//$user_id = get_current_user_id(); | |
?> | |
<h3><?php esc_html_e( 'Upload Personal Avatar', 'wordness' ); ?></h3> | |
<table> | |
<tr> | |
<th><label for="wordness_custom_avatar">Your Avatar: </label></th> | |
<td> | |
<input type="text" name="wordness_custom_avatar" id="wordness_custom_avatar" | |
value="<?php echo esc_attr( get_the_author_meta( | |
'wordness_custom_avatar', $user->ID ) ); ?>" class="regular-text" /> | |
<br> | |
<span><?php esc_html_e( 'Type in the URL of your avatar or Upload an image in Media page and copy url. | |
Image should be 70x70 pixels.', 'wordness' ); ?></span> | |
<?php | |
$def = esc_url( plugin_dir_url(dirname(__FILE__)) . 'css/poplogin-default-logo.png' ); | |
//be safe default | |
$wn_avatar = get_the_author_meta( 'wordness_custom_avatar', $user->ID ); | |
if($wn_avatar == '') $wn_avatar = esc_url( $def ); | |
print( | |
'<div class="image-preview-wrapper"> | |
<img id="image-preview" src="' . esc_url( $wn_avatar ) . '" height=64> | |
</div>' ); | |
?> | |
</td> | |
</tr> | |
</tbody></table> | |
<?php | |
} | |
/** | |
* Saves avatar image to user_meta | |
* $user_id, $meta_key, $meta_value, $prev_value = '' | |
* @param $_POST uploaded file | |
* @param int $user_id ID of user to assign image to | |
*/ | |
function wordness_save_extra_user_profile_fields( $user_id ) | |
{ | |
//$user_id = get_current_user_id(); | |
if ( !current_user_can( 'edit_user', $user_id ) ) return false; | |
update_user_meta( $user_id, 'wordness_custom_avatar', | |
$_POST['wordness_custom_avatar'] ); | |
} | |
/** | |
* Use Custom Avatar if Provided | |
* @author Bill Erickson | |
* @link http://www.billerickson.net/wordpress-custom-avatar/ | |
* | |
*/ | |
function wordness_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) | |
{ | |
// If provided an email and it doesn't exist as WP user | |
$email = is_object( $id_or_email ) ? $id_or_email->comment_author_email : | |
$id_or_email; | |
if( is_email( $email ) && ! email_exists( $email ) ) | |
return $avatar; | |
$custom_avatar = get_the_author_meta('wordness_custom_avatar'); | |
if ($custom_avatar) | |
$return = '<img src="'.$custom_avatar.'" width="'.$size.'" | |
height="'.$size.'" alt="'.$alt.'" />'; | |
elseif ($avatar) | |
$return = $avatar; | |
else | |
$return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" | |
alt="'.$alt.'" />'; | |
return $return; | |
} | |
add_filter('get_avatar', 'wordness_gravatar_filter', 10, 5); | |
/** | |
* Count the views when someone refreshes or views page. | |
* | |
* @uses do_shortcode( '[wordness_postcount]' ). | |
* + get_post_type, is_single | |
*/ | |
function wordness_display_post_count_views( $content ) { | |
//grab option | |
$group = get_option('wordness_fields'); | |
$postcount_check = $group['wordness_postcount_check']; | |
$postcounter = (empty( $postcount_check ) ) ? 0 : $postcount_check; | |
//validate + check | |
if ( is_single() && 'post' == get_post_type() && $postcounter != 0 ) | |
{ | |
$postcount = do_shortcode( '[wordness_postcount]' ); | |
ob_start(); | |
echo $content . '<div class="wnclearfix"></div>' . $postcount; | |
$output = ob_get_clean(); | |
return $output; | |
} else { | |
return $content; | |
} | |
} | |
add_filter( 'the_content', 'wordness_display_post_count_views' ); | |
//ToDo: serialize postmeta??? | |
//http://wpsnipp.com/index.php/cache/track-post-views-without-a-plugin-using-post-meta/ | |
function wordness_getPostViews($postID) | |
{ | |
$count_key = 'post_views_count'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count=='') | |
{ | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '0'); | |
return "0 View"; | |
} | |
return $count.' Views'; | |
} | |
function wordness_setPostViews($postID) | |
{ | |
$count_key = 'post_views_count'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count=='') | |
{ | |
$count = 0; | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '0'); | |
}else{ | |
$count++; | |
update_post_meta($postID, $count_key, $count); | |
} | |
} | |
// Remove issues with prefetching adding extra views | |
//remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment