1

I have a some code being dynamically added into my Footer via add_action('wp_footer' ...). Unfortunately I'm not sure how to remove it using conventional methods, I've looked at the following question and tried to use the solutions there:

remove_action or remove_filter with external classes?

But had no luck with any of the answers so now I'm looking for Alternatives. Is there a way to actually to run some kind of preg_replace on the contents of wp_footer()? Are there any other ways to remove things in wp_footer without using remove_action()?

More Information:

The plugin has an output file. On line 697 is where the action actually gets added:

add_action('wp_footer', array($this, 'add_inline_styles'));

The actual function is defined on line 1743

I wasn't sure whether to reference RevOperations:: or RevSliderFront:: - Here's the initial setup file in the root of the plugin folder.

15
  • 1
    "Are there any other ways to remove things in wp_footer without using remove_action()?" dom manipulation can work, but if the action that prints stuff on footer is added, there should be a way to remove it, and that is fair better. Show us the code that add the action and tell us where it is placed, an I bet someone can find a way to remove it...
    – gmazzap
    Sep 20, 2014 at 0:59
  • @G.M. Well the problem is that it's premium plugin code, and doing so would probably steer the question to "Off-Topic" so I wanted to make it more WP Friendly. The main problem is that when adding it to footer it references $this and I've tried at least 50 combinations of classes and functions to figure out how to remove it (referencing the linked question above) but couldn't. I'll add in some code detail on Monday though.
    – Howdy_McGee
    Sep 21, 2014 at 15:54
  • 1
    It looks like the instance isn't available to us. One (long route!) would be to redefine the rev_slider shortcode and start from there, doing some code rewrites. A possible "hacky" shortcut might be to check the data in $GLOBALS['wp_filter']['wp_footer'] and see if you can identify it there so you could remove it before the wp_footer hook is activated.
    – birgire
    Sep 22, 2014 at 17:43
  • 1
    Have a look at this answer. Using the function there, you can remove the action using add_action( 'wp_footer', function() { remove_object_filter( 'wp_footer', 'RevSliderOutput', 'add_inline_styles', 10 ); }, 0);
    – gmazzap
    Sep 22, 2014 at 21:29
  • 1
    When an hook is added with no priority WordPress uses the default one, that is 10. Source
    – gmazzap
    Sep 22, 2014 at 21:50

1 Answer 1

2

Since you ruled out remove_action there is only one way you can do it. And you've guessed it: preg_repalce, substr mixture but with a little help and PHP DOM

add_action('wp_footer', 'my_start_footer_ob', 1);
function my_start_footer_ob() {
    ob_start("my_end_footer_ob_callback");
}

add_action('wp_footer', 'my_end_footer_ob', 1000);
function my_end_footer_ob() {
    ob_end_flush();
}

function my_end_footer_ob_callback($buffer) {
    // remove what you need from he buffer

    return $buffer;
}

Within my_end_footer_ob_callback you edit the $buffer to your needs. The $buffer parameters should have all the contents of the footer after all actions and filters have been call to action. If it does not simply edit 1000 to a bigger number so that my_end_footer_ob is called last.

Now, I do not know what HTML contents that action produces but you can use pre_replace or a sequence of substrs to remove it.

If you want to use PHP DOM do it like this:

function my_end_footer_ob_callback($buffer) {
    // remove what you need from he buffer

    $doc = new DOMDocument;
    $doc->loadHTML($buffer);

    $docElem = $doc->getElementById("theID");

    if($docElem !== NULL) // if it exists
        $docElem->parentNode->removeChild($docElem);

    return $doc->getElementsByTagName('body')->firstChild->nodeValue;
}

Tell me if this works for you.

7
  • $doc->saveHTML() seems to create a new document root in my footer. <!DOCTYPE html ... <html>...<head>...etc. But I'll play around with this and see if I can get it to work with preg_replace()
    – Howdy_McGee
    Sep 19, 2014 at 16:23
  • @Howdy_McGee, ups, my bad: here you go. An edit.
    – aifrim
    Sep 19, 2014 at 16:29
  • Correct me if I'm wrong but for ob_start() to catch everything I would have to run this before any plugins or functions run. I tried to add it ont the action muplugins_loaded but still had no luck with the buffer. I ran a few simple tests which I've added in a pastebin if you would like to try them. pastebin.com/Duxjrsz5 -- Thanks for the suggestions.
    – Howdy_McGee
    Sep 19, 2014 at 19:01
  • Don't activate the functions in that hook. They have hooks of their own. I have made 1 modification to the code. I gave priority 1 to the ob_start() function. Read about the hooks I use and you will see that I ob_start() when wp_footer is initialized and ob_flush at the end of the rendering.
    – aifrim
    Sep 21, 2014 at 11:58
  • unfortunately it remove everything in wp_footer, all my footer scripts disappear when using this method but +1 for effort. even if I try to store and echo the end_flush I get nothing from it.
    – Howdy_McGee
    Sep 22, 2014 at 20:57

Your Answer

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

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