Archived topic
Hook an element (author dropdown) into a footer widget
4 replies · Started by Ezra on April 29, 2021
I'm trying to create an author dropdown menu to reside in a footer widget so that when I select an author, it will open that author's archive page. (Just like the "Category" dropdown in widget 4). I found the code to do just that here! And it works great!
<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Author' ) ); ?></option>
<?php
// loop through the users
$users = get_users('role=author');
foreach ($users as $user)
{
if(count_user_posts( $user->id ) >0)
{
// We need to add our url to the authors page
echo '<option value="'.get_author_posts_url( $user->id ).'">';
// Display name of the auther you could use another like nice_name
echo $user->display_name;
echo '</option>';
}
}
?>
</select>
The only problem is, I don't have a way to "hook" it into a particular footer widget. I already have 4 widgets, and I want to add it to the 3rd one. (Right now it's hooked straight into the footer, just for testing.)
I saw this very similar post here, (https://generatepress.com/forums/topic/can-an-element-be-hooked-into-a-specific-footer-widget/) and it got me started in the right direction, but I'm not sure how to actually create a shortcode that I can use in a widget like that. Any help would be appreciated! :)
Hi Ezra,
Seems it's already in the 3rd widget of footer, have you figured it out already?
https://www.screencast.com/t/R3inviVA
Let me know :)
Never mind, I hadn't read the instructions well enough. I was able to get my custom shortcode working, (following these instructions) and it runs beautifully!
For the record, here's what I did:
Register the shortcode.
add_shortcode('portable_hook', function($atts){
ob_start();
$atts = shortcode_atts( array(
'hook_name' => 'author_dropdown'
), $atts, 'portable_hook' );
do_action($atts['hook_name']);
return ob_get_clean();
});
Then I added my author dropdown PHP code to an Element -> hook, hooked to the custom author_dropdown hook.
<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Author' ) ); ?></option>
<?php
// loop through the users
$users = get_users('role=author');
foreach ($users as $user)
{
if(count_user_posts( $user->id ) >0)
{
// We need to add our url to the authors page
echo '<option value="'.get_author_posts_url( $user->id ).'">';
// Display name of the auther you could use another like nice_name
echo $user->display_name;
echo '</option>';
}
}
?>
</select>
Finally, I put the shortcode [portable_hook hook_name="author_dropdown"] in my widget, and Tada, it worked!
Hi Ezra,
Seems it’s already in the 3rd widget of footer, have you figured it out already?
Let me know
Yes, I did! We must have both posted around the same time. :) Thanks!
Very good job!
And thanks for sharing your experience with all of us!