Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter machalek

    (@machalek)

    Ooops I forgot to put that if statement also in to the other action so here is final working code

    function test_dynamic_sidebar_before() {
       global $wp_customize;
    
       if ( isset( $wp_customize->selective_refresh) && $wp_customize->selective_refresh->is_render_partials_request() )
           return;
    
       $random_color = sprintf( "#%06x", rand( 0, 16777215 ) );
       echo "<div style='background: $random_color;'>";
    }
    add_action( 'dynamic_sidebar_before', 'test_dynamic_sidebar_before', 9 );
    
    function test_dynamic_sidebar_after() {
       global $wp_customize;
    
       if ( isset( $wp_customize->selective_refresh) && $wp_customize->selective_refresh->is_render_partials_request() )
           return;   
       
       echo "</div>";
    }
    add_action( 'dynamic_sidebar_after', 'test_dynamic_sidebar_after', 11 );
    Thread Starter machalek

    (@machalek)

    Hello again,

    I don’t know if we understand each other correctly but changing priorities of these actions doesn’t had any influence on this issue.

    But I looked at these two actions in WP_Customize_Widgets you are referring to and found that they have THAT if statement I needed (if I’m reading it correctly):
    “add this comment only if we are rendering whole sidebar not only widget partial”

    public function start_dynamic_sidebar( $index ) {
       array_unshift( $this->current_dynamic_sidebar_id_stack, $index );
       if ( ! isset( $this->sidebar_instance_count[ $index ] ) ) {
          $this->sidebar_instance_count[ $index ] = 0;
       }
       $this->sidebar_instance_count[ $index ] += 1;
       if ( ! $this->manager->selective_refresh->is_render_partials_request() ) {
          printf( "\n<!--dynamic_sidebar_before:%s:%d-->\n", esc_html( $index ), intval( $this->sidebar_instance_count[ $index ] ) );
       }
    }
    
    public function end_dynamic_sidebar( $index ) {
       array_shift( $this->current_dynamic_sidebar_id_stack );
       if ( ! $this->manager->selective_refresh->is_render_partials_request() ) {
          printf( "\n<!--dynamic_sidebar_after:%s:%d-->\n", esc_html( $index ), intval( $this->sidebar_instance_count[ $index ] ) );
       }
    }

    So I put same condition in my actions and it works.

    function test_dynamic_sidebar_before() {
       global $wp_customize;
    
       if ( isset( $wp_customize->selective_refresh) && $wp_customize->selective_refresh->is_render_partials_request() )
           return;
    
       $random_color = sprintf( "#%06x", rand( 0, 16777215 ) );
       echo "<div style='background: $random_color;'>";
    }
    add_action( 'dynamic_sidebar_before', 'test_dynamic_sidebar_before', 9 );
    
    function test_dynamic_sidebar_after() {
       echo "</div>";
    }
    add_action( 'dynamic_sidebar_after', 'test_dynamic_sidebar_after', 11 );

    Question is if this is a valid approach?

    ————————————————————————————-
    Another thing I want to mention is that something very strange was happening when I was debugging it. It’s probably due to my lack of understanding customizer core especially javascript side.

    I just added two echo lines to my dynamic_sidebar_before action from the first post.
    One before that wrapper div and one after.

    function test_dynamic_sidebar_before() {
       $random_color = sprintf( "#%06x", rand( 0, 16777215 ) );
    
       // This line gets echoed only once on page load
       echo "echo before wrapper div"; 
       // These two lines gets echoed every time ???
       echo "<div class='test' style='background: $random_color;'>"; 
       echo "echo after wrapper div";
    
    }
    add_action( 'dynamic_sidebar_before', 'test_dynamic_sidebar_before', 9 );
    
    function test_dynamic_sidebar_after() {
       echo "</div>";
    }
    add_action( 'dynamic_sidebar_after', 'test_dynamic_sidebar_after', 11 );

    But to my suprise the first echo line was echoed only first time on page load and not every time widget partial was re-rendered like other two lines. See image. I was stuck on this for an hour and gave up 😀

    customizer

Viewing 2 replies - 1 through 2 (of 2 total)