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 am trying to write a plug-in to display the RunKeeper Healthy button on single posts, but my output has an extra space and double-quote when I look at the page source. What I'm trying to do with the button is open a new window via inline JavaScript.

I've tried switching between single and double quotes and have also ensured that I was escaping nested quotes properly. Trying to append the get_permalink() breaks the generated link for the RunKeeper sharing URL. The image tag for the link itself is fine, but I see an incomplete link when I hover over it.

The link output is:

<a href="javascript:void(window.open('http://runkeeper.com/share?healthyUrl=http://my_website/my_post_permalink/ " , '' , 'width=630,height=350');">

But what I would expect is:

<a href="javascript:void(window.open('http://runkeeper.com/share?healthyUrl=http://my_website/my_post_permalink/' , '' , 'width=630,height=350');">

This is what my plug-in code is so far:

add_filter('the_content', 'add_runkeeper_btn' );

function add_runkeeper_btn($content) {
    $output = "";

    if (is_single()) {
        $output .= "<a href=\"javascript:void(window.open('http://runkeeper.com/share?healthyUrl=";
        $output .= get_permalink();
        $output .= "', '' , 'width=630,height=350');\">";
        $output .= "<img src=\"http://runkeeper.com/static/kronos/images/HealthyButton.png\" class=\"healthyImage\" alt=\"RunKeeper Healthy Button\" />";
        $output .= "</a>";
    }

    return $output . $content;
}

For reference this is what is in the page source of the RunKeeper blog post. I am trying to modify it so that the healthyUrl query parameter references the permalink for the post.

<a href="javascript:var%20d=document,l=d.location;void(window.open('http://runkeeper.com/share?healthyUrl='%20+%20l.href,'','width=630,height=350'));">
    <img src="http://runkeeper.com/static/kronos/images/HealthyButton.png" class="healthyImage" alt="RunKeeper Healthy Button">
</a>
share|improve this question
add comment

2 Answers

You fixed it by changing the double quote to a single quote. :)

<a href="javascript:void(window.open('http://runkeeper.com/share?healthyUrl=http://my_website/my_post_permalink/ " , '' , 'width=630,height=350');">
                                                                                                               ^^^^^

You happened to fix it by refactoring with the "echo" statements. :D

share|improve this answer
add comment
up vote 0 down vote accepted

I fixed this by switching the string concatenation to use PHP's echo instead of doing string concatenation. I also noticed I had a missing parenthesis in my original code (which didn't affect the result of the output where the single quote was turned into a double-quote).

I still have no idea why I was getting a single quote replaced by a double quote even if I had split up the reference code from RunKeeper's blog post.

Here is my final function:

function add_runkeeper_btn($content) {
    if (is_single()) {
        echo '<p><a href="javascript:void(window.open(\'http://runkeeper.com/share?healthyUrl=';
        echo trim(get_permalink());
        echo '\', \'\', \'width=630,height=350\'));">';
        echo '<img src="http://runkeeper.com/static/kronos/images/HealthyButton.png" class="healthyImage" alt="RunKeeper Healthy Button" />';
        echo '</a></p>';
    }

    return $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.