3

I've been working with the Theme Customiser API lately and found that pseudo elements are not live updating using the previewer. In my project I have an image containing a transparent overlay using pseudo elements.

   .hero-section:before {
      position: absolute;
      content: '';
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: #000000;
      opacity: 0.5;
    }

While this doesn't live update:

    // Update targeting a pseudo element.
    wp.customize( 'image_color', function( value ) {
        value.bind( function( newval ) {
            $('.hero-section:before').css('background', newval );
        } );
    } );

This does live update:

   // Update targeting a class.
   wp.customize( 'image_color', function( value ) {
       value.bind( function( newval ) {
           $('.hero-section').css('background', newval );
       } );
   } );

All settings are set using postMessage.

   $wp_customize->add_setting('image_color', array(
      'default'       => '#000000',
      'transport'     => 'postMessage',
   ) );

   $wp_customize->get_setting( 'image_color' )->transport = 'postMessage';

If I save changes (refresh) everything is working fine, the color is updated. How can it be that pseudo elements don't work when live updating?

3
  • 1
    AFAIK you can't target pseudo elements with jQuery - $( ".classame:before" ) won't work. Commented Feb 20, 2015 at 14:46
  • @TheDeadMedic That's probably it, will have to use extra markup then. Thanks for your answer!
    – Tim
    Commented Feb 20, 2015 at 14:58
  • As @TheDeadMedic indicates you can't access pseudo elements with jQuery, so it is a jQuery issue. You could make use of a plugin like JSS to write a stylesheet into the page which includes :before or :after elements.
    – Alvaro
    Commented Jul 18, 2017 at 21:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.