WordPress.org

Ready to get started?Download WordPress

Forums

calling 2.8 widgets directly from template/plugin (8 posts)

  1. naabster
    Member
    Posted 5 years ago #

    Hi,

    I'd like to call a 2.8 widget (extended WP_Widget class) directly from a template and / or plugin.
    The template i am trying to get a widget working doesn't have a sidebar, so I tried to initiate the widget class and call a specific widget like this:

    $sb = new WP_Widget();
    $sb->Widget_one();

    but it gives me some errors..
    Warning: Missing argument 2 for WP_Widget::__construct(), called in (......)
    and a Fatal error: Call to undefined method WP_widget::demowidget(....)

    The widget i am trying to call is working fine with my standard sidebar. I am sure it is not the widget itself..
    Do i need to load/include some other files? I didnt follow the widget development, so i dont really know wtf.

    Basically what i wanna do is: I want to call one (or more) specific widget(s) within a custom post/page template and not the whole sidebar which is defined on your widget admin page..

    thanks.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Widgets are not made to be called directly. They go into registered sidebars.

    If you want to use a widget directly, you'll probably have to hack it up a bit to make it capable of a direct function call.

  3. naabster
    Member
    Posted 5 years ago #

    it is actually pretty easy.
    After toying around for a bit I managed to call a widget from everywhere:

    $instance = array("title" => "My Widget", "number" => 9);
    $args = array("title" => "My Widget", "before_title" => "<h2>", "after_title" => "</h2>");
    $sb = new My_Widget_Class();
    $sb->widget($args,$instance);
  4. Max Chirkov
    Member
    Posted 5 years ago #

    @naabster - awesome snippet! I'm using it to call widgets from post/pages via shortcodes. The only problem with it is that the $instance['number'] doesn't pass the number to the widget method. But this will work:

    $instance = array("title" => "My Widget", "number" => 9);
    $args = array("title" => "My Widget", "before_title" => "<h2>", "after_title" => "</h2>");
    $sb = new My_Widget_Class();
    $sb->number = $instance['number'];
    $sb->widget($args,$instance);
  5. Max Chirkov
    Member
    Posted 5 years ago #

    When calling a widget from a post/page via shortcode, it's being output at the top of the post/page. I guess its because the widget method echoes the content instead of returning it by reference. Is there a way to get around it and output widget content in specific place?

    P.S. The workaround I came up with is to assign all the HTML within the widget method to a $variable and add a simple condition at the end to check if $instance['return'] is true - if yes - it returns the value or else prints it out. The return instance is declared as true in the shortcode function.

  6. r-a-y
    Member
    Posted 5 years ago #

    Great! Exactly what I was looking for!

  7. Zentode
    Member
    Posted 5 years ago #

    That is VERY useful! Glad I found it. Thanks.

    I have a problem that may solved by a variation of that code.

    In this section of my template:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left-side-widgets') ) : ?>

    I would like to add something like:

    <?php else : ?>
    <?php /* alternate sidebar file if no widgets detected in dynamic sidebar */ ?>

    <?php endif; ?>

    Obviously, I would like to include an alternate file if no widgets exist in that specific dynamic sidebar.

    How can I create that "switch"?

    I can use:
    file_get_contents($someurl);
    to grab the file if I must.

    Thanks for any help...

  8. Vinicius Massuchetto
    Member
    Posted 4 years ago #

    Thanks for the snippet!

    I've ran into this kind of problem:
    if (sidebar has widget) --> show sidebar
    else --> show some widgets by hand

    The results:

    Function to verify if sidebar has a widget:
    ------------

    function is_sidebar_active($sidebar_id, $force = false){
    
    		static $sidebars_widgets;
    
    		if($force || !isset($sidebars_widgets)){
    
    			$sidebars_widgets = get_option('sidebars_widgets', array());
    
    		}
    
    		if((int)$sidebar_id) $sidebar_id = sprintf('sidebar-%d', $sidebar_id);
    
    		return is_array($sidebars_widgets[$sidebar_id]) && count($sidebars_widgets[$sidebar_id]);
    
    	}

    Code in sidebar.php:
    ------------

    if (is_sidebar_active(1))
    
    				dynamic_sidebar('Blogs: Lateral Interna');
    
    			else {
    
    				widget_perfil();
    
    				$args = array (
    
    					'title' => '',
    
    					'before_widget' => '<li class="widget">',
    
    					'after_widget' => '</li>',
    
    					'before_title' => '<h2>',
    
    					'after_title' => '</h2>'
    
    				);
    
    				$instance = array ( 'title' => 'Categorias', 'number' => 1 );
    
    				$widget = new WP_Widget_Categories();
    
    				$widget->widget($args,$instance);
    
    				$args = array ( 'title' => '' );
    
    				$instance = array ( 'title' => 'Tweets @revistatitude', 'number' => 1 );
    
    				$widget = new WP_Widget_Tweets();
    
    				$widget->widget($args,$instance);
    
    			}

    Hope it helps someone.

Topic Closed

This topic has been closed to new replies.

About this Topic