This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

conditional slider selection

  • Creator
    Topic
  • #7178
    jrevillini
    Participant

    on the home page, i’d like to swap the default slider for an “inclement weather” slider whenever the “inclement weather” ninja announcement is active. i’ve got the logic and hook to test if the ninja announcement is active. i just need to figure out how to make the different slider show if my condition evals to true.

    i can think of several ways to accomplish this, but wanted your input. here are a few ways i came up with:
    1. brute SQL. if (true) UPDATE something SET {slider element within Home Layout} = 'incliement-weather-slider' WHERE {whatever} = 'home-slider'
    2. content hook if (true) preg_replace('{some fancy expression to grab the slider HTML}', do_action('[slider id="inclement-weather-slider"]', $content))
    3. SUPER COMPLICATED modification to the element/layout system wherein there is “element logic” kind of like the widget logic plugin
    4. some hybrid of the above. e.g. have BOTH sliders in the home page layout but have some hook that cancels one out based on some condition. actually, that sounds the most straightforward.

    what would you suggest?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #7184
    Jason Bobich
    Keymaster

    Have you checkout the Builder API?

    http://dev.themeblvd.com/tutorial_category/chapter-06/

    http://dev.themeblvd.com/tutorial/add-remove-builder-elements/

    If you’re wanting to use the layout builder system for the page, that’d be the way to go, I think. It’s fairly easy to add an element to the builder. This will essentially give you a chance to code your own PHP function that will display for a given element you’d add from the Builder interface. Also, the framework has a function themeblvd_slider you can use to display sliders.

    So maybe something like this:

    // Setup options and add to builder
    $options = array(); // Could add options if you wanted (see tutorial)
    themeblvd_add_builder_element( 'my_new_element', 'My New Element', 'none', $options, 'display_my_new_element' );
    
    // Display "My New Element"
    function display_my_new_element( $id, $options, $location ) {
    	if( $foo )
    		themeblvd_slider('foo-slider');
    	else
    		themeblvd_slider('bar-slider');
    }

    You may run into some formatting issues with the slider, so you might be able to get the surrounding markup and styles of the theme to show up better by using the shortcode.

    function display_my_new_element( $id, $options, $location ) {
    	if( $foo )
    		do_shortcode('[slider id="foo-slider"]');
    	else
    		do_shortcode('[slider id="bar-slider"]');
    }

    ========

    If you wanted to go with the approach of swapping custom layouts all together for the homepage, that’s one thing you could always do, too, fairly easily.

    Every time a page loads there is an ID for the builder that gets assigned if necessary. You can filter this global config array though if you want with “themeblvd_frontend_config” —

    my_frontend_config( $config ) {
    	if( is_home() ){
    		if( $foo )
    			$config['builder'] = 'foo-layout';
    		else
    			$config['builder'] = 'bar-layout';
    	}
    	return $config;
    }
    add_filter('themeblvd_frontend_config', 'my_frontend_config');

    See the themeblvd_frontend_init function in /framework/frontend/functions/general.php

    #7188
    jrevillini
    Participant

    This is so freaking weird. OK. So I decided to add a new field to all elements called element visibility logic. Top of functions.php:

    function tunxiscc_element_mods ( $elements ) {
        $elements['slider']['options'][] = array(
    		    	'id' 		=> 'visibility_logic',
    				'name'		=> __( 'Element Visibility Logic ', TB_GETTEXT_DOMAIN ),
    				'desc'		=> __( 'Enter any PHP expression used to calculate visibility of this element within the layout. If the expression evaluates to false, the element will not be shown.<br><br><em>Example: is_page(1234)</em>', TB_GETTEXT_DOMAIN ),
    				'type'		=> 'text',
    			);
        return $elements;
    }
    add_filter( 'themeblvd_core_elements', 'tunxiscc_element_mods' );

    That works fine. Looking at the generic

    function themeblvd_elements( $layout, $location )

    function, it looks to me like the only way to conditionally show the element is to send everything to the output buffer starting at themeblvd_element_{type}_before and then conditionally output it at themeblvd_element_{type}_after. The problem is that I’m not getting the parameters you pass in your hooks. You have

    do_action( 'themeblvd_element_'.$element['type'].'_before', $id, $element['options'], $location ); // Before element: themeblvd_element_{type}_before

    So I should be getting 3 arguments, yet this does not output anything useful about the element I’m dealing with and in face gives me errors for arguments 2 and 3:

    function tunxiscc_element_slider_before ( $id, $options, $location ) {
    	echo '<!-- SLIDER ARGS ';
    	print_r (func_get_args());
    	echo '-->';
    }
    add_action('themeblvd_element_slider_before', 'tunxiscc_element_slider_before');

    And in fact, if I hack your builder.php file and output $element right before those do_action hooks, I DO SEE useful information.

    Here is the output from my hook:

    <!-- SLIDER ARGS Array
    (
        [0] => element_1
    )

    Here is the output of $element just before running those element hooks:

    Array
    (
        [type] => slider
        [query_type] => secondary
        [options] => Array
            (
                [slider_id] => home-slider
                [visibility] => Array
                    (
                        [hide_on_standard] => 0
                        [hide_on_tablet] => 0
                        [hide_on_mobile] => 0
                    )
    
                [visibility_logic] => !tunxiscc_weather_announcement_active()
            )
    
    )

    Totally baffled. Sorry to hit you with this crazy shit.

    #7190
    jrevillini
    Participant

    nevermind. I needed to list the # of params expected when I ran add_action. new code:

    add_action('themeblvd_element_slider_before', 'tunxiscc_element_slider_before', 1, 3);
    add_action('themeblvd_element_slider_after', 'tunxiscc_element_slider_after', 1, 3);

    that always gets me. i would think NOT putting anything would, by default, get everything passed, but no.

    #7324
    Jason Bobich
    Keymaster

    Hey James,

    I just wanted to let you know I saw your message. Feel free to sell your own plugins of adding functionality to Jump Start. Sounds great.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The forum ‘Alyeska Responsive WordPress Theme’ is closed to new topics and replies.