ThemeForest

wp_register_sidebar_widget

685 posts
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • Uruguay
  • Referred between 1 and 9 users
_rg_ says

Does anybody knows how this works exactly?

I´m trying to register a sidebar dinamicly on a page or post, i have the ID but i´m not able to register/insert it on a certain widget area.

What i´m trying is to put one of the inactive widgets on a dinamic area.

Thanks!

oh, by the way, this is how i get all widgets ID´s, active ones and inactive:

$sidebars_widgets = get_option('sidebars_widgets');
foreach ($sidebars_widgets as $index=>$widgets) {
    if ($index!='wp_inactive_widgets' && $index!='array_version') {
        if(!empty($widgets) ){
            foreach($widgets as $n){
                echo 'Widget in use: '.$n.'<br />';
            }
        }
    }
    if($index=='wp_inactive_widgets'){
            foreach($widgets as $n){
                echo 'Widget inactive: '.$n.'<br />';
            }
    }
}

Put that on any template and you will see what i mean.

2 years ago
2698 posts
  • Has been a member for 1-2 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Forum Superstar
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 50 and 99 users
duotive says

Hy. I am adding the the widget name and description into a table in the database and then getting the variables and registering as many widget area as i want. And than selecting from a list with metabox in post/page i am getting the sidebar needed for a post or a page. Pretty simple concept. I hope this helps. Vlad

2 years ago
685 posts
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • Uruguay
  • Referred between 1 and 9 users
_rg_ says

Ok, i already can get the widgets names/ids, also if i need descripion too, but what i can´t is register the widget into a widget area.

How is your last step? adding the widget to template?

Do you register_sidebar (new one) and there you place the widget?

I can´t figure your idea… :(

2 years ago
2698 posts
  • Has been a member for 1-2 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Forum Superstar
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 50 and 99 users
duotive says

in the sidebar.php i get the meta box for each page or post ( if is_post() or if is_page() ) and get the contet for the current sidebar registered to that particular page/post i can share the code if you would like…

2 years ago
2698 posts
  • Has been a member for 1-2 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Forum Superstar
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 50 and 99 users
duotive says
Here it goes. The isertion you can do. The first function is the one called first.

    function sidebars_require () {
        global $wpdb;        
        $portfolio_require_query = 'SELECT * FROM duotive_sidebars ORDER BY ID ASC';    
        $portfolio_require = $wpdb->get_results($portfolio_require_query);
        return $portfolio_require;
    }    

This gets all the sidebars from the db.

function custom_sidebars_initialization() { 
    $sidebars = sidebars_require();
    if ( count($sidebars) > 0 ):
        foreach ( $sidebars as $sidebar):
           register_sidebar( array(
                'name' =>  $sidebar->NAME,
                'id' => str_replace(' ','-',strtolower($sidebar->NAME)),
                'description' => $sidebar->DESCRIPTION ,
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',        
                'before_widget' => '<li class="widget-container %2$s" id="%1$s">',
                'after_widget' => '</li>',
            ) );        

        endforeach;
    endif; 
} ?&gt;
&lt;?php add_action( 'widgets_init', 'custom_sidebars_initialization' );?&gt;

This one registers all the widget areas that i defined. The metaboxes are made with the help of a tutorial at WPShout.com => http://wpshout.com.

function hybrid_post_meta_boxes() {
    $sidebars = sidebars_require();
    if ( count($sidebars) > 0 )
    {    
        $sidebar_array = array("No Sidebar");
        $i = 0;
        foreach ( $sidebars as $sidebar)
        {
            array_push($sidebar_array,$sidebar->NAME);
        }
    }
    /* Array of the meta box options. */
    $meta_boxes = array(
            'video' => array( 'name' => 'video-thumbnail', 'title' => 'Has video tumbnail?', 'type' => 'select', 'options' => array( 0 => 'No',1 => 'Yes' ) ),
            'url' => array( 'name' => 'url-to-video', 'title' => 'Please insert the url to your video.', 'type' => 'text' ),
            'sidebar' => array( 'name' => 'sidebar', 'title' => 'Custom sidebar?', 'type' => 'select', 'options' => $sidebar_array )
    );

    return apply_filters( 'hybrid_post_meta_boxes', $meta_boxes );
}

and now… in the sidebar.php file:

    <?php if ( is_single() || is_page() )
    {
        $sidebar =  get_post_meta($post?>ID, "sidebar", true);
        $sidebar = str_replace(' ','-',strtolower($sidebar));
        if ($sidebar != '' )
        {
            if ( is_active_sidebar($sidebar) )
            {
                echo '<ul>';
                    dynamic_sidebar($sidebar);
                echo '</ul>';
            }
        }
    }
    ?>

Hope this helps. :)
2 years ago
685 posts
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • Uruguay
  • Referred between 1 and 9 users
_rg_ says

waw, well i need to take a time to see that, then i´ll tell you.

anyway, while i´m wrigting i´m seeing that´with your code you active or not certain sidebars widgets areas, but what i´m trying to is to active or not certain “widgets” only.

what i said, i´m able to unregister them on any post or page with the code i posted, doing a wp_unregister_sidebar_widget. I think i need to review this from a diferent point of wiew.

In humans words, on the widget admin page, you have the inactive widgets panel where you can leave widgets and those ones will keep settings, like title or any content… well, those inactive widgets, is there no way to call them from anyplace on templates?

There must be a way to call a widget (not a dinamic sidebar area, just a single widget) directly from anywere. That´s the main idea in real.

I know the existence of the_widget(), but only works with default widgets like WP_search and son on.

thanks for your time, if i find solution i will post here and may be build a plugin. :)

2 years ago
by
by
by
by
by