WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Writing a Plugin: How Do I Add HTML After The Content? (7 posts)

  1. Joe Hall
    Member
    Posted 6 years ago #

    Hello All! I am currently attempting to write my first plugin! However, I am having trouble. The plugin seems to be pretty basic. I am trying to insert some html below the content of each post. I have been successful inserting plain text below the content. However, HTML just isn't working. I am following this tutorial at devlounge. My code is below minus the header info. What am I doing wrong?

    <?php
    
    if (!class_exists("FIRSTPlugin")) {
           class FIRSTPlugin {
    
    function addContent($content = '') {
    	$content .= "<a href='http://www.yahoo.com'>yahoo</a>";
    return $content;
    }   
    
              function FIRSTPlugin() {
              }
          }
    
       }
    
    if (class_exists("FIRSTPlugin")) {
    $dl_pluginSeries = new FIRSTPlugin();
    }
    
    if (isset($dl_pluginSeries)) {
    
    add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
    
    }
    
    ?>

    PS: sorry for the messy structure above!

  2. addmarx
    Member
    Posted 6 years ago #

    Hi there,

    I am a relative newbie myself (just created my first plugin and am waiting to upload it).

    In response to your question, I think you are actually making it more complicated than needed.

    Here is what my plugin essentially does:

    <?php
    
    function addContent($content) {
    	$content .= '<a href="http://www.yahoo.com">yahoo</a>';
    return $content;
    }   
    
     add_action('the_content', 'addContent');
    
    }
    
    ?>

    Hope this helps!

    -Jeff

  3. addmarx
    Member
    Posted 6 years ago #

    PS--If you are solely looking to update the content at the bottom of posts, note that you will need to put some conditional code in there as well. For my plugin, the code to determine whether or not $content was in reference to a posting was accomplished via if (!is_page() && is_single()). In other words, if the content is not a "page" (About this Blog is example of a "page" rather than a post) and if the content is single (not the list of postings found on the front page), then execute the change to the $content variable.

    -Jeff

  4. addmarx
    Member
    Posted 6 years ago #

    One final note about the first code snippet. Take out the } right after add_action('the_content', 'addContent'); and you should be all set.

    -Jeff

  5. Joe Hall
    Member
    Posted 6 years ago #

    addmarx,

    you are correct! Often times I do make things overly complicated. Although, the tutorial i read said that you should set up a class system to verify that there aren't any duplicate naming issues with other plugins. But anyways, after taking your advice I have fixed the code....well, kinda. The problem I am facing now is that I am having a hard time processing the "bloginfo('url')" function in my content filter. Is this not possible? The reason that I need to get the blog URL is because I need to create global URLs that point back to the plugin directory. Have any ideas?

  6. Joe Hall
    Member
    Posted 6 years ago #

    Never mind, I found my fix to my latest problem here>> http://wordpress.org/support/topic/185618?replies=4

  7. addmarx
    Member
    Posted 6 years ago #

    On the topic you raised about verifying non-duplication (a valid point), I just had the theory that if I made an obscure enough function name, it would not be an issue! So, steer away from something as generic as "addContent" and you should be fine. For instance you could use "RedBMedia_add_Content" and I bet that would be sufficient.

Topic Closed

This topic has been closed to new replies.

About this Topic