Creating a plugin that will display text on every page

I know there is going to be an easy way to do this but I just can’t seem to find the best practice for the following;

I have a plugin which when complete should generate HTML on every page, I want it to do this without the need for JavaScript and I want it to be independent of the theme when possible.

Read More

It will appear on every single page, so it can’t be a shortcode in a template file (but would a shortcode in the header.php file be appropriate?).

Ideally there would be a hook that would allow me to insert code after the <h1> tag in the header (don’t think it will be just that easy though!)

Could someone point me in the right direction or send me some pseudocode to help me on my way?

Edit

Brian asked me What exactly are you trying to insert and where on the template, here is my answer;

It will be a element styled from within the plugin with an options value generated by the plugin. It will be appearing at the top right of every page, in the header of the page.

Related posts

Leave a Reply

1 comment

  1. Use the_content or the_title/single_post_title(?) filter and simply prepend/append what you need. Also take a look at the Action/Filter API Reference.


    @Example:

    /**
     * Appends the authors initials to the content.
     * Mimics paper magazines that have a trailing short to show the end of the article.
     * Gets appended at the end inside the last paragraph.
     * @param (string) $content
     * @return (string) $content
     */
    function wpse28904_append_to_content( $content )
    {
        // Only do it for specific templates
        if ( is_page() || is_archive() )
            return $content;
    
        // Get author initials
        $author = '';
        preg_match_all( '/[A-Z]/', get_the_author(), $initials_arr );
        foreach ( $initials_arr[0] as $initials )
            $author .= $initials;
        $author_url     = get_the_author_meta('url');
        if ( $author_url )
        {
            $title  = esc_attr( sprintf(__("Visit %s’s website"), $author ) );
            $author = "<a href='{$author_url}' title='{$title}' rel='external'>{$author}</a>";
        }
    
        // Append  author initials to end of article
        $content  = preg_replace( "/<p[^>]*><\/p[^>]*>/", '', $content );
        $position = strrpos( $content, '</p>' );
        $content  = substr_replace( $content, "<sub>{$author}</sub>", $position, -1 );
    
        return $content;
    }
    add_filter( 'the_content', 'wpse28904_append_to_content' );