• Resolved teejayuu

    (@teejayuu)


    Hi I am trying to write a plugin that display information between the header and the 1st post. On my current template it would go:
    <?php get_header(); ?>
    Information to go here
    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

    I have looked through the info at https://wphooks.flatearth.org, but can’t seem to find anything that covers. I was hoping for a pre_post filter hook. The sort of thing I am hoping to write is:

    add_filter('pre_post', 'my_function');
    function my_function($value){
    echo $value;
    return $value;
    }

    Iam new to writing plugins and am using this to help me understand better how WP works.
    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Because it is a “theme” thing. Write your function in your plugin, and add a tag in a theme page where you want it to show up.

    Thread Starter teejayuu

    (@teejayuu)

    Tag? How would I write a tag in the theme page? Do you mean call the the function from the theme page, and would it just be a procedural call i.e.
    <?php get_header(); ?>
    my_function('The information to display');
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    Yes. In a more elegant way, put this in your theme:
    <?php if ((function_exists('my_function')) { my_function('The information to display') } ?>

    So, if your plugin is deactivated, no error will show up.
    Or, other developpers invite users to put a special html comment tag in the theme page, your plugin finds it, and puts its content there:
    <!-- my_function -->

    Thread Starter teejayuu

    (@teejayuu)

    Thank you PozHonks. Using the <?php if ((function_exists('my_function')) { my_function('The information to display') } ?> works fine, but using <!-- my_function --> show nothing. Any ideas?

    teejayhu, I’m not sure I’d recommend what PozHonks suggests (i.e. <!-- my_function -->), since it would require your plugin go the way of output buffering each time a page loads, parsing the entire document to locate the specific comment tag (and replace it). A lot of overhead for so little gain.

    You could try something like the following, which comes close to what you’re looking for:

    function top_of_the_posts() {
    echo '<p>Top of the posts to ya!<p>';
    }

    add_action('loop_start', 'top_of_the_posts');

    The API hook ‘loop_start’ occurs before the first post (initial the_post() call) in a posts query. Beyond that, you’d be better off providing a function/template tag for users to add to their templates.

    Thread Starter teejayuu

    (@teejayuu)

    Kafkaesqui – thanks this is just what I’m looking for and it works perfectly.

    Thanks to all who have contributed as I have learned a lot over these last few hours.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to use hooks to display info between header and posts’ is closed to new replies.