0

Let's say there's a template file called sample-template.php with contents below

sample-template.php

<?php
/**
 * Template Name: Sample template
 **/
get_header(); ?>
<?php the_title(); ?>
<myplugin type="video" id="1"></myplugin>
<?php get_footer(); ?>

and is expected to be rendered by wordpress like so

<!DOCTYPE html>
<html>
    <head><title>The Title</title></head>
    <body>
        <h1>The Title</h1>
        <myplugin type="video" id="1"></myplugin>
    </body>
</html>

but before it gets rendered by wordpress, I want to replace <myplugin type="video" id="1"></myplugin> with a different string

<!DOCTYPE html>
<html>
    <head><title>The Title</title></head>
    <body>
        <h1>The Title</h1>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/hxnb2V0muGw" frameborder="0" allowfullscreen></iframe>
    </body>
</html>

such that <myplugin type="video" id="1"></myplugin> wil be replaced by <iframe width="560" height="315" src="https://www.youtube.com/embed/hxnb2V0muGw" frameborder="0" allowfullscreen></iframe>

What action or filter can be used for modifying the page to be rendered?

  • You can't modify HTML with PHP unless it is output by PHP. You could use JavaScript to edit the element once it is loaded though. Is a JavaScript solution possible for you to use? Why aren't you able to just change the template directly? – Richard Webster Feb 26 '17 at 12:59
  • @RichardWebster The intent here is to be able to change the string right from the server-side. Thus, JavaScript on this case is not a viable option. The reason why just-changing-the-template-directly is not an option is that I wanted also to implement <myplugin type="breadcrumb" /> which will generate the page link heirarchy – Abel Melquiades Callejo Feb 26 '17 at 13:06
  • 1
    So if you removed that HTML entirely and had your own action you could hook into with an add_action() call, would that be useful to you? You could then output as many elements as you'd like. Or failing adding your own, you could hook into the the_title() action call and output stuff after the title. I can give you some code if you answer those. Otherwise you can't change hardcoded HTML directly with PHP. – Richard Webster Feb 26 '17 at 13:13
  • agree with @RichardWebster, using <?php do_action('my-plugin'); ?> in the template is the most flexible solution here, then you can add actions to that hook that can do any infinite number of things. – majick Feb 26 '17 at 15:09

Your Answer

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

Browse other questions tagged or ask your own question.