1

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.

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);
3
  • what exactly doesn't work? what's failing? Commented Mar 30, 2011 at 10:15
  • it adds the first $blog_title into the $bloglovin variable.. but not $link and $title .. is it php or wordpress that's cousing the problem? Commented Mar 30, 2011 at 10:17
  • you probably need to urlencode() the code before you put in there. Commented Mar 30, 2011 at 10:33

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

not true though.. shanethehat code above works fine! i just had to use get_ in front of the variables i got from wordpress.. so it's inside the loop.. $content is inside of the loop, I added code behind it.
Hey! I said the Codex says can't use the_permalink() function [OR get_permalink() either in fact] UNLESS it's in a loop. Your original post does not show if it's referenced in a loop or not, so I was telling you to check that just in case. Glad you got it working.
0

Try a basic sanity check by var_dump()ing the $link and $title variables. Do they actually contain a string?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.