• I’m teaching myself how to write my own plugins, and I’m making this simple one that prints an url at the end of avery post. Problem is, every post gets replaced by the url, how come? What’s the correct hook to use in order for the url to appear at the end of each post instead of replacing every post? Here’s my code:

    <?php
    /*
    Plugin Name: Name
    Plugin URI: https://www.myurl.com
    Description: My plugin description
    Version: 1.0
    Author: Me
    Author URI: https://www.myurl.com
    */
    
    ?>
    
    <?php
    function my_link()
    {
    echo "<h2>Some text<h2>";
    	$my_url = '<a href="https://www.google.com/"</a>';
    	print $my_url;
    }
    
    add_action ( 'the_content', 'my_link' );
    ?>

    I’ve tried using both an action and a filter with not much success, can anyone please enlighten me? I know I’m close to getting it. Thanks in advance.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Try a filter.

    Plugin_API/Filter_Reference

    Might also look how other plugins do it:
    Plugins/Posts_Formatting

    HuitZiloP: If you use a filter for the_content, the function has a $content parameter, which is the contents of the post or page.

    Which means you can do this:

    function my_link($content) {
    $content .= "Hello!
    ";
    return $content;
    }
    
    add_filter( 'the_content', 'my_link' );

    ($string .= ‘text’ is the same as $string = $string . ‘text’; in case you had not used it before)

    Once the function is finished, you must return $content at the end.

    The basic explanation is that this allows you to modify the content and then send the modified version back to WordPress for display.

    It is best not to use echo or print, because it will generally not display where you want. It will just display on whatever part of the page was currently being generated when the plugin was executed.

    Also, If instead you wanted to modify the content, you could do a search and replace on it, and then return the new, modified, data in the same manner.

    For example, a lot of my plugins allow you to display something (such as a form, list of posts, etc..) wherever you want in the post or page. By adding the special trigger text to your post, your plugin can search for it, and if it is found, replace it with the generated data.

    Hope that helps!

    Thread Starter HuitZiloP

    (@huitzilop)

    Hi Aleister, thank you very much for your helpful answer. I managed to make it work, however, I still have trouble controlling this little buddy, because it appears at the beggining of each post and not at the end, which is where I want it to appear. It also appears at the fornt page, but I want to display it just in single posts, how can it be done? Here’s a sample of what I’ve done so far:

    function my_link($content)
    {
    echo "<h2>Some text<h2>";
    	$my_url = '<a href="https://www.google.com/"</a>';
    	print $my_url;
    
    return $content;
    }
    
    add_action ( 'the_content', 'my_link' );

    I thank you very much for your help and hope you can help me again, I’m kind of green with programming. Thanks a lot again buddy!

    EDIT: Oh and how can I add h2/general html tags without using print or echo? I seem to be still getting valid code. Thanks a lot!

    HuitZiloP: As I mentioned, if you use echo or print, you cannot really control where it goes. You have to add the text you want to display on to the $content variable before returning it. Take a look at my example again to see.

    @huitzilop

    I believe that last line:

    add_action ( 'the_content', 'my_link' );

    should be

    add_filter ( 'the_content', 'my_link' );

    Thread Starter HuitZiloP

    (@huitzilop)

    Aleister, thank you very much for your inmense help. I’ve managed to make the plugin work the way you told me, but it still displays at the home, how do I limit it to displaying just in single posts? Would you mind if I email you my code so you can take a look at it? Thanks a lot again!

    @michaelh Thanks a lot, I needed to replace the action for a filter indeed.

    HuitZiloP: Here is one way:

    function my_link($content)
    {
    if (is_single()) {
      // do whatever
    }
    return $content;
    }
    Thread Starter HuitZiloP

    (@huitzilop)

    Still no luck. Now it shows only in single posts, but got back to the beginning of each post. I’m about to give up, this can’t be so hard. Thanks a lot for the support though!

    Just remember – do not use echo or print – append the text to the $content variable.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Okay, this is really not that difficult to understand.

    A “filter” takes some input text, modifies it, and returns the modified version. It does not make any output by itself.

    It doesn’t have to modify the input text, BTW. If it returns the exact same text back, then that’s okay too.

    What you want to do is simply done by this:

    function my_filter($content) {
    if (is_single()) {
    $content .= "Stuff to add to end of content";
    }
    return $content;
    }
    add_filter('the_content','my_filter');

    That’s it. You don’t do any “echo” or “print” stuff. You’re just modifying the content and sending it back. WordPress will print it as needed.

    The above code takes the content, and if it’s a single page then it appends some text to the end of it. Then it returns that text back, whether it appended stuff to the end of it or not. Simple, see?

    Thread Starter HuitZiloP

    (@huitzilop)

    Finally, I got this little guy to run the way I wanted. I want to thank Alleister, MichaelH and Otto for being such a great help during the process. Keep up the gret work guys, thanks again!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Help writing a simple plugin’ is closed to new replies.