Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
    
What exactly are you trying to insert and where on the template? –  Brian Fegter Sep 19 '11 at 16:06
    
It will be a <div> 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. –  Toby Sep 19 '11 at 16:10
add comment

1 Answer

up vote 1 down vote accepted

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&#8217;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' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.