Is there a 'good working practise' way to modify the markup that a WordPress plugin produces without editing the plugin's core files. The problem I foresee is that when you update the plugin, the markup that you would have modified overwritten.
I know in Drupal there are te开发者_如何转开发mplate overrides, but I don't know enough about WordPress to do a similar practise.
Any help?
The plugin itself would probably have to be written to allow for it (though see below). There are a few ways to do this: You could have the plugin look for template files that it pulls from an arbitrary location (e.g. "uploads/myplugin") You could possibly store the HTML in the database as a setting. The plugin could be written with an apply_filter hook (just as WordPress itself uses hooks) to allow outside calls that change output (e.g. from a separate plugin or a theme's functions.php). I've used all of these methods.
If you're talking about altering the output of somebody else's plugin, you could possibly ask them to implement one of the above. Push comes to shove you can use JavaScript to manipulate the DOM.
The answer to your question is hooks and templates. If you're lucky, the plugin will use templates for its output and will check your theme to see if you've overridden them, or it may have some filter hooks that let you modify its output. If you're not so lucky, and you can't get the plugin authors to add some for you, you'll need to get more creative.
- hook into the WordPress queries that the plugin is making, and alter them to return different results.
- hook into the WordPress get_option() function to change the plugin's settings on the fly.
- hook into the page content, and use preg_replace() to hack the HTML.
- hook in early in the page generation, call ob_start() to buffer the page output, and hook the wp_footer to call ob_end_clean() and use preg_replace() to hack the HTML.
Just some ideas :)
Obviously, it's best to work with the plugin than against it. You should check to see whether your plugin uses templates, and if it doesn't, search for calls to apply_filter() and do_action(). But sometimes, needs must!
精彩评论