Skip to content

Instantly share code, notes, and snippets.

@meloniq
Created April 20, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meloniq/eff6167f2ea01e3804244a10141342cc to your computer and use it in GitHub Desktop.
Save meloniq/eff6167f2ea01e3804244a10141342cc to your computer and use it in GitHub Desktop.
[ClassiPress theme + Anonymous plugin] Change the anonymous author name to the one from custom field
<?php
/**
* Change the anonymous author name.
*
* @param string $author_meta The value of the metadata.
* @param int $user_id The user ID.
*
* @return string
*/
function childtheme_modify_anonymous_author_name( $author_meta, $user_id ) {
global $authordata, $post;
if ( ! is_singular( APP_POST_TYPE ) ) {
return $author_meta;
}
if ( ! $user_id ) {
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
}
$user = get_userdata( $user_id );
if ( 'anonymous' != $user->user_login ) {
return $author_meta;
}
// get user name from custom field (REPLACE THE FIELD NAME!!)
$custom_user_name_field = get_post_meta( $post->ID, 'cp_anonymous_custom_name', true );
if ( ! empty( $custom_user_name_field ) ) {
$author_meta = $custom_user_name_field;
}
return $author_meta;
}
add_filter( 'the_author_display_name', 'childtheme_modify_anonymous_author_name', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment