0

I'm using 'transport' => 'postMessage' to update the text within the footer area during theme customisation live preview.

My problem is that on my website, the corresponding theme setting (footer_text) has the_content filters applied to it. Is there any way to apply that filter via JS, or I am stuck with 'transport' => 'refresh' for situations like this? Thanks.

(function($){

    /** Update the footer text */
    wp.customize('footer_text', function(value){
        value.bind(function(newval){
            $('#footer-left').html(newval);
        });
    });

})(jQuery);
3
  • The point of postMessage is that it's sending data directly, in the browser. It's not going through the server, so no, you cannot apply PHP filters to it unless it's using refresh.
    – Otto
    Commented Jan 31, 2014 at 14:00
  • I've never looked in to this before today, I wasn't sure if an AJAX call was being made anywhere as part of the preview. Sad times :( I suppose I could make my own AJAX call, although that seems like overkill for this particular scenario! Thanks.
    – David Gard
    Commented Jan 31, 2014 at 14:29
  • 1
    @Otto as an answer please :)
    – Rarst
    Commented Jan 31, 2014 at 16:38

1 Answer 1

0

I've again been looking in to this so I've decided to post an answer because I actually have done this using an AJAX call.

As pointed out in the comments to my question by @Otto -

The point of postMessage is that it's sending data directly, in the browser. It's not going through the server, so no, you cannot apply PHP filters to it unless it's using refresh."

So using the original example from above for context, you could apply filters to text updated via the postMessage method using an AJAX call as follows -

JS in your theme-customiser.js file -

(function($){

    /** Update the footer text */
    wp.customize('footer_text', function(value){
        value.bind(function(newval){

            var data = {
                action: 'filter_using_the_content',
                text:   newval
            }

            $.post(MyAjax.ajaxurl, data, function(response){
                $('#footer-left').html(response);
            });

        });
    });

})(jQuery);

PHP in your functions.php file -

add_action('wp_ajax_filter_using_the_content', 'my_filter_using_the_content');
function my_filter_using_the_content(){

    echo apply_filters('the_content', $_POST['text']);
    die();  // Required for a proper result

}

Your Answer

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

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