Mixing PHP and HTML in variable

I’m trying to create a WordPress plugin that adds a button below all posts. The button is a “Like” button that pass some parameters through the URL to a new site.

The parameters are WordPress permalink, title, and blog name.

Read More

Can’t make it work.

function add_bloglovin($content) {

   $blog_title = get_bloginfo('name');
   $link = the_permalink();
   $title = the_title();

   $bloglovin ="<br><a href="http://www.bloglovin.com/like/?b=$blog_title&p=$link&t=$title" onclick="window.open(this.href, 'bloglovin_like', 'width=480,height=320, toolbar=0, location=0, menubar=0, scrollbars=0, status=0'); return false;"><img src="http://www.bloglovin.com/widget/bilder/like.gif"></a>";
   return $content .$bloglovin;
}
add_filter('the_content', add_bloglovin);

Related posts

Leave a Reply

3 comments

  1. the_permalink() is a display function. Use get_permalink() to return a string that you can use. To make the_title return just the title with no wrapped HTML you need to use the_title(”,”,false);

    function add_bloglovin($content) {
        $blog_title = get_bloginfo('name');
        $link = get_permalink();
        $title = the_title('','',false);
        $bloglovin ="<br><a href="http://www.bloglovin.com/like/?b=$blog_title&p=$link&t=$title" onclick="window.open(this.href, 'bloglovin_like', 'width=480,height=320, toolbar=0, location=0, menubar=0, scrollbars=0, status=0'); return false;"><img src="http://www.bloglovin.com/widget/bilder/like.gif"></a>";
        return $content .$bloglovin;
    }
    
  2. From the WordPress Codex:
    http://codex.wordpress.org/Function_Reference/the_permalink

    Function Reference/the permalink

    Displays the URL for the permalink to
    the post currently being processed in
    The Loop. This tag must be within The
    Loop, and is generally used to display
    the permalink for each post, when the
    posts are being displayed. Since this
    template tag is limited to displaying
    the permalink for the post that is
    being processed, you cannot use it to
    display the permalink to an arbitrary
    post on your weblog.

    You can’t use $link = the_permalink(); in isolation unless it’s in the Loop.