Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display a widget anywhere: Widgets are used inside registered widget areas. Let's display them in other areas. https://www.damiencarbery.com/2019/07/display-a-widget-anywhere/
<?php
$args = array(
// These two are useful inside a sidebar but should be changed to
// '<div>' and '</div>' when invoking a widget outside a sidebar.
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
);
<?php
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
<?php
// Show Recent Posts widget (2 posts, with date) at the end of content.
add_filter( 'the_content', 'dcwd_append_recent_posts_widget_to_content' );
function dcwd_append_recent_posts_widget_to_content( $content ) {
$rpw = new WP_Widget_Recent_posts;
$args = array(
'before_widget' => '<div class="widget">',
'after_widget' => "</div\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
);
$instance = array(
'title' => 'Some Recent Posts',
'number' => 2,
'show_date' => true,
);
ob_start();
$rpw->widget( $args, $instance );
$output = ob_get_contents();
ob_end_clean();
return $content . $output;
}
<?php
// Call the WordPress Meta widget directly, outside of a sidebar.
add_action( 'wp_body_open', 'dcwd_meta_widget_at_top' );
function dcwd_meta_widget_at_top() {
$mw = new WP_Widget_Meta;
$args = array(
//'before_widget' => '<li id="%1$s" class="widget %2$s">',
//'after_widget' => "</li>\n",
'before_widget' => '<div class="widget">',
'after_widget' => "</div\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
);
$instance = array(
'title' => 'Our Meta Widget',
);
$mw->widget( $args, $instance );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.