[wp-hackers] post id in custom template

ml_wordpress at copperleaf.org ml_wordpress at copperleaf.org
Sun Jun 5 12:46:05 GMT 2005


I have a plugin that modifies "the_content" with a registered content 
filter. I'd like to be able to have the part being modified be defined 
with an optional template file but need to know the post id and misc 
data in the template page. Let me try and illustrate.

mytemplatefile.php:

<div>
    the post id is <?php echo $post-ID; ?><br />
    my tag key is <?php echo $mymatches; ?><br />
</div>


in the content filter, I do something like:

preg_replace_callback('/<!--\s*mytag(.*)-->/iU', callback, $content);

function callback($mymatches)
{
    ob_start();
    include('mytemplatefile.php');
    $buf =ob_get_content();
    ob_end_clean();
    return $buf; 
}

What is the best way to pass in the post id as well as $mymatches to the 
template file? I know I can use the global $post to get the post id, but 
is that the best way? Or would I be better off defining a function in 
the mytemplatefile.php. Something like:

<?php
function mytemplatefile($postid, $mymatches){
?>
<div>
    the post id is <?php echo $postid; ?><br />
    my tag key is <?php echo $mymatches; ?><br />
</div>
<?php } ?>

TIA
Bill


More information about the wp-hackers mailing list