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

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;
}

Related

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';
}

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

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;

How to add a widget before default feed on the home page

In my Genesis Magazine pro theme I'm trying to add a widget area between the header and the content. What I'm trying to do is perfectly described here: https://wpsites.net/web-design/add-widget-to-magazine-pro-front-page/ but it doesn't seem to work. Nothing shows up when I add a new widget into the new before-home-top widget.
I've tried replacing my front-page.php with
<?php
/**
* Magazine Pro.
*
* This file adds the front page to the Magazine Pro Theme.
*
* #package Magazine
* #author StudioPress
* #license GPL-2.0+
* #link http://my.studiopress.com/themes/magazine/
*/
add_action( 'genesis_meta', 'magazine_home_genesis_meta' );
/**
* Add widget support for homepage. If no widgets active, display the default loop.
*
* #since 3.0.0
*/
function magazine_home_genesis_meta() {
if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
// Force content-sidebar layout setting.
add_filter( 'genesis_site_layout', '__genesis_return_content_sidebar' );
// Add magazine-home body class.
add_filter( 'body_class', 'magazine_body_class' );
// Add homepage widgets.
add_action( 'genesis_before_content_sidebar_wrap', 'before_home_top' );
// Remove the default Genesis loop.
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add homepage widgets.
add_action( 'genesis_loop', 'magazine_homepage_widgets' );
}
}
// Add body class to front page.
function magazine_body_class( $classes ) {
$classes[] = 'magazine-home';
return $classes;
}
function before_home_top() {
genesis_widget_area( 'before-home-top', array(
'before' => '<div class="before-home-top widget-area">',
'after' => '</div>',
) );
}
// Output the widget areas for the front page.
function magazine_homepage_widgets() {
echo '<h2 class="screen-reader-text">' . __( 'Main Content', 'magazine-pro' ) . '</h2>';
genesis_widget_area( 'home-top', array(
'before' => '<div class="home-top widget-area">',
'after' => '</div>',
) );
genesis_widget_area( 'home-middle', array(
'before' => '<div class="home-middle widget-area">',
'after' => '</div>',
) );
genesis_widget_area( 'home-bottom', array(
'before' => '<div class="home-bottom widget-area">',
'after' => '</div>',
) );
}
genesis();
and I added the following to my functions.php
genesis_register_sidebar( array(
'id' => 'before-home-top',
'name' => __( 'Before Home Top', 'magazine-pro' ),
) );
I'm looking to add a custom HTML widget in there and nothing is showing up. Any help would be much appreciated!
The name of the sidebar you registered in functions.php is before-home-top.
There's a conditional in your front page code that is checking to see if any of three sidebars are active (contain widgets). The reason you're not seeing anything is because none of those checks are for before-home-top.
Your problem is this line:
if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
Add your sidebar to that list of checks:
if ( is_active_sidebar( 'before-home-top' ) || is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
You may also wish to remove any sidebars that you don't intend to register.

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

Eventbrite API not returning Events

So a site I am working on for a client, http://atasteofartistry.com, needs to have EventBrite integration. It is a wordpress install running a Genesis theme.
Right now, in functions.php, I have this:
//* EventBrite
require 'Eventbrite.php';
$eb_client = new Eventbrite( array('app_key'=>'(REDACTED)',
'user_key'=>'(REDACTED)'));
$arr = array('user'=>'atasteofartistry#gmail.com',
'event_statuses'=>'live,started'
);
e$events = $eb_client->user_list_events($arr);
and I have the call in a widget listed like this:
<h1>My Event List:</h1>
<?= Eventbrite::eventList( $events, 'eventListRow'); ?>
</div>
On the site, I am not getting the events. The Eventbrite.php API client is initializing, because I get the "No Events" message, but I just cannot pass anything through to it.
I've been staring at it for 8 hours now and need help. So HELP!
I'm using the Eventbrite PHP library from Github.
https://github.com/ryanjarvinen/eventbrite.php
How to fix it would be great, but I can't even figure out how to troubleshoot it.
Many thanks,
Joe
FUNCTIONS.PHP
<?php
//* Start the engine
include_once( get_template_directory() . '/lib/init.php' );
//* Setup Theme
include_once( get_stylesheet_directory() . '/lib/theme-defaults.php' );
//* EventBrite
require 'Eventbrite.php';
add_action( 'genesis_before', 'eventbrite_event_list');
function eventbrite_event_list() {
$eb_client = new Eventbrite( array('app_key'=>'ECOC6TZS3JJVXLJ2MX',
'user_key'=>'1409014384112928178221'));
$arr = array('user'=>'atasteofartistry#gmail.com',
'event_statuses'=>'live,started'
);
try {
$events = $eb_client->user_list_events();
} catch ( Exception $e ) {
// Be sure to plan for potential error cases
// so that your application can respond appropriately
//var_dump($e);
$events = array();
}
}
//* Set Localization (do not remove)
load_child_theme_textdomain( 'daily-dish', apply_filters( 'child_theme_textdomain', get_stylesheet_directory() . '/languages', 'daily-dish' ) );
//* Child theme (do not remove)
define( 'CHILD_THEME_NAME', __( 'Daily Dish Pro Theme', 'daily-dish' ) );
define( 'CHILD_THEME_URL', 'http://my.studiopress.com/themes/daily-dish/' );
define( 'CHILD_THEME_VERSION', '1.0' );
//* Enqueue scripts and styles
add_action( 'wp_enqueue_scripts', 'daily_dish_enqueue_scripts_styles' );
function daily_dish_enqueue_scripts_styles() {
wp_enqueue_script( 'daily-dish-responsive-menu', get_bloginfo( 'stylesheet_directory' ) . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0' );
wp_enqueue_style( 'dashicons' );
wp_enqueue_style( 'daily-dish-google-fonts', '//fonts.googleapis.com/css?family=Lato:400,700,900|Playfair+Display:400|Tangerine:400,700', array(), CHILD_THEME_VERSION );
}
add_filter( 'genesis_seo_title', 'custom_genesis_seo_title', 10, 1 );
/**
* Replace Site Title text entered in Settings > Reading with custom HTML.
* #author Sridhar Katakam
* #link http://sridharkatakam.com/replace-site-title-text-custom-html-genesis/
*
* #param string original title text
* #return string modified title HTML
*/
function custom_genesis_seo_title( $title ) {
$title = '<h1 itemprop="headline" class="site-title"><a title="Homepage" href="' . get_bloginfo('url') . '">a taste of <span class="artistry">ARTISTRY</span></a></h1>';
return $title;
}
//* Add HTML5 markup structure
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', ) );
//* Add viewport meta tag for mobile browsers
add_theme_support( 'genesis-responsive-viewport' );
//* Add support for custom background
add_theme_support( 'custom-background', array(
'default-attachment' => 'fixed',
'default-color' => 'ffffff',
'default-image' => get_stylesheet_directory_uri() . '/images/bg.png',
'default-repeat' => 'repeat',
'default-position-x' => 'left',
) );
//* Add new image sizes
add_image_size( 'daily-dish-featured', 720, 470, TRUE );
add_image_size( 'daily-dish-archive', 340, 200, TRUE );
add_image_size( 'daily-dish-sidebar', 100, 100, TRUE );
//* Add support for custom header
add_theme_support( 'custom-header', array(
'header-selector' => '.site-title a',
'header-text' => false,
'height' => 80,
'width' => 400,
) );
/** Enable shortcodes to be used in widgets */
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
//* Unregister the header right widget area
unregister_sidebar( 'header-right' );
//* Remove navigation meta box
add_action( 'genesis_theme_settings_metaboxes', 'daily_dish_remove_genesis_metaboxes' );
function daily_dish_remove_genesis_metaboxes( $_genesis_theme_settings_pagehook ) {
remove_meta_box( 'genesis-theme-settings-nav', $_genesis_theme_settings_pagehook, 'main' );
}
//* Remove output of primary navigation right extras
remove_filter( 'genesis_nav_items', 'genesis_nav_right', 10, 2 );
remove_filter( 'wp_nav_menu_items', 'genesis_nav_right', 10, 2 );
//* Reposition the secondary navigation menu
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_before_header', 'genesis_do_subnav' );
//* Hook before header widget area above header
add_action( 'genesis_before', 'daily_dish_before_header' );
function daily_dish_before_header() {
genesis_widget_area( 'before-header', array(
'before' => '<div class="before-header" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
add_theme_support( 'genesis-structural-wraps', array(
'header',
'nav',
'subnav',
'site-inner',
'footer-widgets',
'footer'
) );
//* Customize the entry meta in the entry header
add_filter( 'genesis_post_info', 'daily_dish_single_post_info_filter' );
function daily_dish_single_post_info_filter( $post_info ) {
$post_info = '[post_date] [post_author_posts_link] [post_comments] [post_edit]';
return $post_info;
}
//* Customize the entry meta in the entry footer
add_filter( 'genesis_post_meta', 'daily_dish_post_meta_filter' );
function daily_dish_post_meta_filter($post_meta) {
$post_meta = '[post_categories before=""] [post_tags before=""]';
return $post_meta;
}
//* Remove comment form allowed tags
add_filter( 'comment_form_defaults', 'daily_dish_remove_comment_form_allowed_tags' );
function daily_dish_remove_comment_form_allowed_tags( $defaults ) {
$defaults['comment_notes_after'] = '';
return $defaults;
}
//* Walled Garden
// function walled_garden()
// {
// if( ! is_user_logged_in() )
// wp_redirect( '/wp-login.php' );
// }
// add_action( 'get_header', 'walled_garden' );
//* Add images to Site Header
add_action( 'genesis_site_title','add_header_images',8);
function add_header_images() {
?>
<span><img src="../wp-content/themes/daily-dish-pro/images/wine-glass.png" class="header-glass" /></span>
<span><img src="../wp-content/themes/daily-dish-pro/images/splatter.png" class="splatter" /></span>
<?php
}
//* Modify the size of the Gravatar in the author box
add_filter( 'genesis_author_box_gravatar_size', 'daily_dish_author_box_gravatar' );
function daily_dish_author_box_gravatar( $size ) {
return 180;
}
//* Modify the size of the Gravatar in the entry comments
add_filter( 'genesis_comment_list_args', 'daily_dish_comments_gravatar' );
function daily_dish_comments_gravatar( $args ) {
$args['avatar_size'] = 96;
return $args;
}
//* Hook before footer widget area below footer widgets
add_action( 'genesis_before_footer', 'daily_dish_before_footer_widgets', 5 );
function daily_dish_before_footer_widgets() {
genesis_widget_area( 'before-footer-widgets', array(
'before' => '<div class="before-footer-widgets" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
//* Hook after footer widget area below footer
add_action( 'genesis_after', 'daily_dish_after_footer' );
function daily_dish_after_footer() {
genesis_widget_area( 'after-footer', array(
'before' => '<div class="after-footer" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
//* Add support for 3-column footer widgets
add_theme_support( 'genesis-footer-widgets', 3 );
//* Add support for after entry widget
add_theme_support( 'genesis-after-entry-widget-area' );
//* Relocate after entry widget
remove_action( 'genesis_after_entry', 'genesis_after_entry_widget_area' );
add_action( 'genesis_after_entry', 'genesis_after_entry_widget_area', 5 );
//* Register widget areas
genesis_register_sidebar( array(
'id' => 'before-header',
'name' => __( 'Before Header', 'daily-dish' ),
'description' => __( 'This is the before header widget area.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-top',
'name' => __( 'Home - Top', 'daily-dish' ),
'description' => __( 'This is the top section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-middle',
'name' => __( 'Home - Middle', 'daily-dish' ),
'description' => __( 'This is the middle section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-bottom',
'name' => __( 'Home - Bottom', 'daily-dish' ),
'description' => __( 'This is the bottom section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'before-footer-widgets',
'name' => __( 'Before Footer Widgets', 'daily-dish' ),
'description' => __( 'This is the before footer widgets section.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'after-footer',
'name' => __( 'After Footer', 'daily-dish' ),
'description' => __( 'This is the after footer section.', 'daily-dish' ),
) );

Categories