Skip to content

Instantly share code, notes, and snippets.

@KZeni
Created March 29, 2022 06:11
Show Gist options
  • Save KZeni/ae2076d8d59bcb01d23a1ad3c4eed3f8 to your computer and use it in GitHub Desktop.
Save KZeni/ae2076d8d59bcb01d23a1ad3c4eed3f8 to your computer and use it in GitHub Desktop.
Add Post Author to Recent Post Widgets Extended (RPWE) WordPress plugin output (uses Simple HTML DOM PHP [1.9.1 here] to aid in the HTML traversing & insertion])
<?php
// Add Simple HTML DOM for PHP-based HTML/DOM Traversal
if(!function_exists('str_get_html')){
include 'include/simple_html_dom.php';
}
// Add Author info to Recent Post Widgets Extended output (add before date)
function add_author_to_rpwe_markup($html,$args){
$html = str_get_html($html);
foreach($html->find('.rpwe-title a') as $title_link){
$permalink = $title_link->href;
$post_id = url_to_postid($permalink);
$author_id = get_post_field( 'post_author', $post_id );
$author_display_name = get_the_author_meta( 'display_name', $author_id );
$title_tag = $title_link->parent();
$post_element = $title_tag->parent();
foreach($post_element->find('.rpwe-time') as $time_element){
$time_element_html = $time_element->outertext;
$time_element->outertext = '<span class="rpwe-author">'.$author_display_name.'</span> '.$time_element_html;
}
}
$html_output = $html; // Convert back to string
return $html_output;
}
add_filter('rpwe_markup','add_author_to_rpwe_markup',10,2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment