0

I have a setting that basically needs the whole page to refresh. However, I still want to have a shortcut icon to its controller. If I do that, though, I need to implement a render_callback that reproduces the targeted element by the selector. I don't want that. I want a full page refresh.

Here's what I currently have (an example with blogname for simplicity):

$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
$wp_customize->selective_refresh->add_partial( 'blogname', array(
    'selector' => '.logo a',
    'render_callback' => function () {
        $wp_customize->requestFullRefresh();
    }
) );

This works, but not perfectly. When a change to the blogname setting occurs, first the <a> element flashes (tries to be rendered) and then the whole page refreshes.

Questions

  1. How do I skip directly to the full page refresh and not wait for the partial refresh?
  2. Is there a better way to do this? Requesting a refresh in the render callback seems a bit odd.
3

If you always want a page refresh, then all you have to do is simply remove this line:

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

Or explicitly set it to refresh:

$wp_customize->get_setting( 'blogname' )->transport = 'refresh';

By doing this, selective refresh will never get invoked to refresh the partial, and it will skip straight to the full refresh.

Note it is preferable to return false in the render_callback to indicate you want to trigger a refresh. In your example, it is actually causing a fatal error because there is no such method. The same fallback behavior to do a full refresh is still happening when returning false or causing a fatal error, but in the latter case your error log is getting filled up.

In short, your example can be modified to:

$wp_customize->selective_refresh->add_partial( 'blogname', array(
    'selector' => '.logo a',
    'render_callback' => '__return_false',
) );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.