Last active
November 20, 2017 17:22
-
-
Save robincornett/58d5ed139dfa4676f2051d1127a381ea to your computer and use it in GitHub Desktop.
Functions to help Bylines (https://bylines.io/) and the Genesis Framework play nicely together
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 | |
add_action( 'init', 'leaven_load_bylines' ); | |
/** | |
* Maybe load Bylines files. | |
*/ | |
function leaven_load_bylines() { | |
if ( ! function_exists( 'the_bylines' ) ) { | |
return; | |
} | |
$files = array( 'filters', 'sitemap' ); | |
foreach ( $files as $file ) { | |
require get_stylesheet_directory() . "/dependencies/bylines/bylines-{$file}.php"; | |
} | |
} | |
add_filter( 'genesis_post_info', 'leaven_post_info_filter' ); | |
/** | |
* Replace the Genesis post info. | |
* @param $post_info | |
* | |
* @return string | |
*/ | |
function leaven_post_info_filter( $post_info ) { | |
if ( ! is_singular( 'post' ) ) { | |
return ''; | |
} | |
return '<span class="date">[post_date]</span> ' . leaven_replace_authors_genesis_shortcode() . ' [post_comments]'; | |
} | |
/** | |
* Replace the Genesis authors post link shortcode. | |
* Allows authors to not be linked at all (due to multiple authors in Bylines). | |
* | |
* @param string $output | |
* @param array $atts | |
* | |
* @return string | |
*/ | |
function leaven_replace_authors_genesis_shortcode( $output = '', $atts = array() ) { | |
$author = get_the_author(); | |
if ( ! $author ) { | |
return $output; | |
} | |
$defaults = array( | |
'after' => '', | |
'before' => '', | |
); | |
$atts = shortcode_atts( $defaults, $atts, 'post_author_posts_link' ); | |
$output = sprintf( '<span %s>', genesis_attr( 'entry-author' ) ); | |
$output .= $atts['before']; | |
$output .= leaven_get_the_bylines_posts_links(); | |
$output .= $atts['after'] . '</span>'; | |
return $output; | |
} | |
/** | |
* Replacement getter for the_bylines_posts_links. | |
* Returns instead of echoes; replicates Genesis markup. | |
* | |
* @return string | |
*/ | |
function leaven_get_the_bylines_posts_links() { | |
if ( ! function_exists( 'the_bylines' ) ) { | |
return leaven_author_links_fallback(); | |
} | |
return bylines_render( get_bylines(), function( $byline ) { | |
$link = is_a( $byline, 'WP_User' ) ? get_author_posts_url( $byline->ID ) : $byline->link; | |
$args = array( | |
'before_html' => '', | |
'href' => $link, | |
'rel' => 'author', | |
// translators: Posts by a given author. | |
'title' => sprintf( __( 'Posts by %1$s', 'bylines' ), $byline->display_name ), | |
'class' => 'entry-author-link', | |
'text' => $byline->display_name, | |
'after_html' => '', | |
); | |
/** | |
* Arguments for determining the display of bylines with posts links | |
* | |
* @param array $args Arguments determining the rendering of the byline. | |
* @param Byline $byline The byline to be rendered. | |
*/ | |
$args = apply_filters( 'bylines_posts_links', $args, $byline ); | |
$output = $args['before_html']; | |
$output .= sprintf( '<a href="%s" %s>', $args['href'], genesis_attr( 'entry-author-link' ) ); | |
$output .= sprintf( '<span %s>%s</span>', genesis_attr( 'entry-author-name' ), esc_html( $args['text'] ) ); | |
$output .= '</a>' . $args['after_html']; | |
return $output; | |
} ); | |
} | |
/** | |
* If Bylines isn't active, we need to still return author name and link for the post info. | |
* | |
* @return string | |
*/ | |
function leaven_author_links_fallback() { | |
$url = get_author_posts_url( get_the_author_meta( 'ID' ) ); | |
$output = ''; | |
if ( $url ) { | |
$output .= sprintf( '<a href="%s" %s>', $url, genesis_attr( 'entry-author-link' ) ); | |
} | |
$output .= sprintf( '<span %s>%s</span>', genesis_attr( 'entry-author-name' ), esc_html( get_the_author() ) ); | |
if ( $url ) { | |
$output .= '</a>'; | |
} | |
return $output; | |
} |
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 | |
add_filter( 'option_wpseo_titles', 'leaven_fix_wpseo_description', 100, 2 ); | |
/** | |
* Hide crazy description box on bylines edit page. | |
* @param $value | |
* @param $option | |
* | |
* @return mixed | |
*/ | |
function leaven_fix_wpseo_description( $value, $option ) { | |
if ( 'wpseo_titles' !== $option ) { | |
return $value; | |
} | |
$value['hideeditbox-tax-byline'] = true; | |
return $value; | |
} | |
add_filter( 'get_the_author_display_name', 'leaven_use_bylines_author_name' ); | |
add_filter( 'the_author', 'leaven_use_bylines_author_name' ); | |
/** | |
* Use Bylines for author names. | |
* | |
* @param $name | |
* | |
* @return string | |
*/ | |
function leaven_use_bylines_author_name( $name ) { | |
if ( is_author() ) { | |
return get_queried_object()->display_name; | |
} else { | |
return bylines_render( get_bylines(), function ( $byline ) { | |
return $byline->display_name; | |
} ); | |
} | |
} | |
add_filter( 'get_the_author_intro_text', 'leaven_use_bylines_intro_text' ); | |
/** | |
* Replace the Genesis intro text with Bylines' description. | |
* @param $intro_text | |
* | |
* @return string | |
*/ | |
function leaven_use_bylines_intro_text( $intro_text ) { | |
$object = get_queried_object(); | |
$byline_text = $object->description; | |
return $byline_text ? $byline_text : ''; | |
} | |
add_filter( 'author_link', 'leaven_use_bylines_author_link' ); | |
/** | |
* Use the Bylines author link instead of author links. | |
* | |
* @param $link | |
* | |
* @return string | |
*/ | |
function leaven_use_bylines_author_link( $link ) { | |
$byline_url = false; | |
$bylines = get_the_terms( get_the_ID(), 'byline' ); | |
if ( ! is_wp_error( $bylines ) && count( $bylines ) > 1 ) { | |
return $byline_url; | |
} | |
$byline_url = bylines_render( get_bylines(), function ( $byline ) { | |
return $byline->link; | |
} ); | |
return $byline_url ? $byline_url : $link; | |
} | |
add_filter( 'get_avatar_url', 'leaven_use_bylines_author_gravatar' ); | |
/** | |
* Replace the gravatar URL with the Bylines custom image. | |
* | |
* @param $url | |
* | |
* @return false|string | |
*/ | |
function leaven_use_bylines_author_gravatar( $url ) { | |
if ( is_admin() ) { | |
return $url; | |
} | |
$byline_url = bylines_render( get_bylines(), function ( $byline ) { | |
$meta = get_term_meta( $byline->term_id, 'leaven', true ); | |
return isset( $meta['gravatar'] ) && $meta['gravatar'] ? wp_get_attachment_image_url( $meta['gravatar'] ) : false; | |
} ); | |
return $byline_url ? $byline_url : $url; | |
} |
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 | |
/** | |
* Copyright (c) 2017 Robin Cornett | |
*/ | |
use Bylines\Objects\Byline; | |
add_filter( 'genesis_sitemap_output', 'leaven_modify_sitemap' ); | |
/** | |
* Change the Genesis sitemap to show Bylines authors, not WordPress authors. | |
* @param $output | |
* | |
* @return string | |
*/ | |
function leaven_modify_sitemap( $output ) { | |
if ( ! function_exists( 'bylines_render' ) ) { | |
return $output; | |
} | |
$heading = 'h3'; | |
$output = sprintf( '<%2$s>%1$s</%2$s>', __( 'Pages:', 'leaven' ), $heading ); | |
$output .= sprintf( '<ul>%s</ul>', wp_list_pages( 'title_li=&echo=0' ) ); | |
$output .= leaven_get_custom_post_types_for_sitemap(); | |
$post_counts = wp_count_posts(); | |
if ( $post_counts->publish > 0 ) { | |
$output .= sprintf( '<%2$s>%1$s</%2$s>', __( 'Categories:', 'leaven' ), $heading ); | |
$output .= sprintf( '<ul>%s</ul>', wp_list_categories( 'sort_column=name&title_li=&echo=0' ) ); | |
$list = leaven_get_all_bylines(); | |
if ( $list ) { | |
$output .= sprintf( '<%2$s>%1$s</%2$s>', __( 'Authors:', 'leaven' ), $heading ); | |
$output .= sprintf( '<ul>%s</ul>', $list ); | |
} | |
$output .= sprintf( '<%2$s>%1$s</%2$s>', __( 'Monthly:', 'leaven' ), $heading ); | |
$output .= sprintf( '<ul>%s</ul>', wp_get_archives( 'type=monthly&echo=0' ) ); | |
$output .= sprintf( '<%2$s>%1$s</%2$s>', __( 'Recent Posts:', 'leaven' ), $heading ); | |
$output .= sprintf( '<ul>%s</ul>', wp_get_archives( 'type=postbypost&limit=100&echo=0' ) ); | |
} | |
return $output; | |
} | |
/** | |
* Get all author/bylines with as list items. | |
* @return string | |
*/ | |
function leaven_get_all_bylines() { | |
$bylines = get_terms( array( | |
'taxonomy' => 'byline', | |
'fields' => 'ids', | |
) ); | |
if ( ! $bylines ) { | |
return ''; | |
} | |
$output = ''; | |
foreach ( $bylines as $term ) { | |
$object = array( Byline::get_by_term_id( $term ) ); | |
$output .= bylines_render( | |
$object, function ( $byline ) { | |
$link = is_a( $byline, 'WP_User' ) ? get_author_posts_url( $byline->ID ) : $byline->link; | |
return sprintf( '<li><a href="%s">%s</a></li>', | |
esc_url( $link ), | |
esc_html( $byline->display_name ) | |
); | |
} | |
); | |
} | |
return $output; | |
} | |
/** | |
* Get custom post types as lists for the sitemap. | |
* | |
* @return string | |
*/ | |
function leaven_get_custom_post_types_for_sitemap() { | |
// Get public custom post types | |
$post_types = get_post_types( array( | |
'public' => true, | |
), 'objects' ); | |
// Remove pages and posts as they are added already in genesis_sitemap_output | |
unset( $post_types['page'] ); | |
unset( $post_types['post'] ); | |
// Bail if none | |
if ( ! $post_types ) { | |
return ''; | |
} | |
$sitemap = ''; | |
// Loop through the posts | |
foreach ( $post_types as $post_type ) { | |
$list = wp_get_archives( 'post_type=' . $post_type->name . '&type=postbypost&limit=100&echo=0' ); | |
// Skip if no posts | |
if ( ! $list ) { | |
continue; | |
} | |
// Add the posts to the sitemap variable | |
$heading = 'h3'; | |
$sitemap .= sprintf( '<%2$s>%1$s</%2$s>', $post_type->label, $heading ); | |
$sitemap .= sprintf( '<ul>%s</ul>', $list ); | |
} | |
return $sitemap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment