How to switch the (single/double) quotes for a Tweet me button

203 views Asked by At

I spent about 2 hours trying to make a blending of 2 codes work, and feeling the weakness of my code skills, I am reaching out here for some help.

I think the problem lies in the single/double quotes. I tried many, many times to reformat the quotations (and both ways ie reformatting both scripts to accommodate each other).

The image (which should be linked) is added to the page in the functions.php -- the image is path/image.png in this example:

! is_admin() && add_filter( 'the_content', function( $content )
{
    if( in_the_loop() )   // <-- Target the main loop
    {
        $prepend = "<div style='color:#808080  ; border:1px solid #909090 ; border-radius:5px; float:left; padding-top:1px;'>&nbsp;<**img src='/path/image.png' alt='Tweet this' style='margin-bottom: -4px; '**>Tweet this &#8202;</div>&nbsp;";
        $content = $prepend . $content;
    } 
    return $content;
}, 9 ); 

This is the link -- When the user clicks the image, I want them to access this link:

<a href='https://twitter.com/share?url=<?php echo $post_url ?>&text=<?php echo $post_title ?>' onclick='window.open(this.href,"popupwindow", "width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable"); return false;' Target='_blank' >twitter</a>

What I want to end up with is: Click the image on the page, (it opens the Twitter link,) user gets to Tweet (share) the post they are on on Twitter.

So user clicks image (path/image.png) and opens link (twitter.com...etc).

1

There are 1 answers

3
cFreed On BEST ANSWER

Still not sure of anything: you should realize that your question lacks clarity.
Notably because you don't say where $post_url and $post_title comes from, nor explain what are $prepend and $content, and don't explain at all your function's use and context.

But I'll talk about what I believe to have understood.

Your goal seems to result in this kind of HTML part:

<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
    <a href="https://twitter.com/share?url=the-post_url&text=the-post_title"
    onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;"
    target="_blank">
        <img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;">
        Tweet this &#8202;
    </a>
</div>

(you'll notice that I've returned back to arbitrary give precedence to double-quotes, which is the most standard choice)

Say we call $div, $a, and $img the source main components, to output the desired block we simply have to write something like:
echo $div . $a . $img . 'Tweet this &#8202;</a></div';

I suppose the issue you're reporting comes from you have to define those components as strings, while they already include simple and double quotes.

So the answer is using the PHP heredoc syntax:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

And the components definition should look like:

$div = <<<EOT
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
EOT;
$a = <<<EOT
<a href="https://twitter.com/share?url=$post_url&text=$post_title"  onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank">
EOT;
$img = <<<EOT
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;">
EOT;

In fact many other variations may be considered, notably directly using a unique variable for the whole part...


EDIT:

Here is a working whole snippet, after integrating a value for $post_urland $post_title and corrected a typo in echo...:

$post_url="the-post-url";
$post_title="the-post-title";
$div = <<<EOT
<div style="color:#808080; border:1px solid #909090; border-radius:5px; float:left; padding-top:1px;">
EOT;
$a = <<<EOT
<a href="https://twitter.com/share?url=$post_url&text=$post_title"  onclick="window.open(this.href,'popupwindow','width=800,height=500,left=200,top=5,scrollbars,toolbar=0,resizable'); return false;" target="_blank">
EOT;
$img = <<<EOT
<img src="/path/image.png" alt="Tweet this" style="margin-bottom:-4px;" />
EOT;
echo $div . $a . $img . 'Tweet this &#8202;</a></div>';