How to get Wordpress Widget(sidebar) without printing it? - php

I am using Wordpress and PHP. I declared a custom sidebar using this code :
function customtheme_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'My custom sidebar', 'customtheme' ),
'id' => 'my-custom-sidebar',
'description' => esc_html__( '.', 'customtheme' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'customtheme_widgets_init' );
Ok so In my code I want to get my sidebar and store it in a PHP variable $the_widget. Using this code :
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
$the_widget = dynamic_sidebar('my-custom-sidebar');
endif;
when i do that it automatically echoes the widget(sidebar) when i call dynamic_sidebar() can i do that without the echo?? is there another function in the wordpress codex that can do the same?

No unfortunately there isn't a WordPress function such as get_dynamic_sidebar() however a common method for getting the sidebar into a variable as a string is this:
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
ob_start();
dynamic_sidebar('my-custom-sidebar');
$the_widget = ob_get_contents(); //or ob_get_clean();
ob_end_clean();
endif;

Related

How to display random custom html widget within a widget area?

I've registered a new widget area in functions.php using
'name' => __( 'Grand angle sous les articles', 'twentyten' ),
'id' => 'grand-angle',
'description' => __( 'Grand angle en bas des articles', 'twentyten' ),
'before_widget' => '<div id="grand-angle">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
) );
I'm calling this new widget area in a php template like this :
<?php if ( is_active_sidebar( 'grand-angle' ) ) : dynamic_sidebar( 'grand-angle' ); endif; ?>
Inside this new widget area I'm now adding several custom html widgets and I would like to display them randomly. These custom html widgets contain ads (mostly images) and I need to set up a rotation. Any idea how to do that? Thank you very much for your help.
The solution I've found :
add_filter( 'sidebars_widgets', 'custom_randomize_widget_order' );
function custom_randomize_widget_order( $sidebars_widgets ) {
$sidebars = array( 'grand-angle' ); // set the ids of your sidebars whose widgets you want to shuffle
foreach ( $sidebars as $sidebar ) {
if ( isset( $sidebars_widgets[ $sidebar ] ) && ! is_admin() ) {
shuffle( $sidebars_widgets[ $sidebar ] );
// Slice the array:
$sidebars_widgets[ $sidebar ] = array_slice( $sidebars_widgets[ $sidebar ], 0, 1 );
}
}
return $sidebars_widgets;
}

Wordpress custom sidebar on single page

I'm trying to display a custom sidebar in a page. The code works with custom post types "news" posts but wont work with the page "na-midia". In the page the defaul sidebar is shown.
// Custom Sidebar
function prefix_custom_sidebar() {
register_sidebar( array(
'name' => __( 'Custom Sidebar Mídia', 'page-builder-framework' ),
'id' => 'custom-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="wpbf-widgettitle">',
'after_title' => '</h4>'
) );
}
add_action( 'widgets_init', 'prefix_custom_sidebar' );
// Replace the default sidebar with our new custom sidebar on all docs posts
function prefix_do_custom_sidebar( $sidebar ) {
// this statement works for custom post types
if( is_singular( 'news' ) ) {
$sidebar ='custom-sidebar';
}
// this statement NOT works for the page which is displaying the posts, display the default sidebar instead
elseif ( is_singular( 'na-midia' ) ) {
$sidebar ='custom-sidebar';
}
return $sidebar;
}
add_filter( 'wpbf_do_sidebar', 'prefix_do_custom_sidebar' );
Solved!
elseif ( is_page ( 'na-midia' ) ) {
$sidebar ='custom-sidebar';
}

Custom built review plugin - pagination not working

I'm currently create a plugin that takes all comments from all products and places them under the review tab for all comments.
Inside Wordpress I have the following setting checked:
Settings > Discussion > Break comments into pages with...
This enables pagination on the default comment section. I've added this code with my code but it doesn't enable pagination, even when I remove the If statement.
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
) ) );
echo '</nav>';
endif; ?>
I'm currently using this function to call all reviews into one:
function products_all_reviews(){
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments );
}
What needs to be done to call/enable pagination properly? This is my first time creating plugin so the help is much appreciated

Include custom sidebar and remove main sidebar in DWQA

I want to remove main sidebar from dwqa-question page and want to add custom sidebar in that page.
I wrote a code for it in functions.php :
function dwqa_theme_register_sidebar() {
register_sidebar( array(
'name' => __( 'Single Question', 'multinews' ),
'id' => 'dqwa',
'class' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'dwqa_theme_register_sidebar' );
function remove_main_sidebar_dwqa_question(){
if ( is_singular('dwqa-question') ){
unregister_sidebar( 'Main Sidebar' );
}
}
add_action( 'widget_init', 'remove_main_sidebar_dwqa_question' );
And this code in page.php :
<?php if ( is_singular('dwqa-question') ): ?>
<?php dynamic_sidebar('dqwa') ?>
<?php endif; ?>
Below is the output screenshot :
single question page
I think you dont need to de-register the main sidebar every time, it will take time to render the output. Just write the code in siderbar.php. Here I am mixing some of your code with.
<?php
if ( is_singular('dwqa-question') )
{
dynamic_sidebar('dqwa');
}
else
{
/** FOR OTHER THAN DWQA ***/
dynamic_sidebar('main-sidebar'); //I might be wrong on ID
}//end if
?>
Hope it will work for you

do_shortcode is not working on my index.php

I register a shortcode on my functions.php
function offer_intro_shortcode ($atts, $content=null) {
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts );
extract ($offer);
return '<div class="center gap">
<h3>'.$title.'</h3>
<p class="lead">'.$text.'</p>
</div>';
}
add_shortcode ('offer', 'offer_intro_shortcode');
Then i write on wordpress post:
[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']
Then i query for this shortcode on my index.php like do_shortcode('[offer]')
But it is not working
try this.
echo do_shortcode("[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']");
check this is working or not.
After $atts add another parameter i.e shortcode so it would become.
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts,'offer' );

Categories