Jump to content

Hyperlink in PHP (harder than it sounds)


frankdm
Go to solution Solved by benanamen,

Recommended Posts

I am trying to, in php, add a hyperlink containing a wordpress variable. This is what I got so far:

 

case 'excerpt':
if ($atts['excerpt'] == 'true') {
$content .= '<td>' <a href="http://www.google.com/search?q=" . get_the_title() . ></a> '</td>';
}
break;
 
This is giving me "error on line" errors...
 
If I just use: 
$content .= '<td>'  . get_the_title() .  '</td>';
 
Then I get the post title so that part works... but i need to do a google search with a post title, so i need to add a hyperlink around that:
 
And this needs to work in the case above. I cannot figure it out :(
Link to comment
Share on other sites

Your quotes are all over the place. If you cannot fix them yourself, install a proper IDE (integrated development environment) to assist you with the syntax.

 

You also need to URL-encode and HTML-escape the title before you can insert it into the anchor. It's most definitely not URL-compatible by itself and can also lead to cross-site scripting attacks.

Link to comment
Share on other sites

Depends on how get_the_title() is structured. Doe it end with return or echo?

 

With echo it would be

$content .= "<td><a href='http://www.google.com/search?q={get_the_title()}></a></td>";    
 

else

$content .= "<td><a href='http://www.google.com/search?q=". get_the_title().'></a></td>";  
 

Plus what @Jaques1 said.

 

Hey

I tried this but it gives me a line error again:

 

 

case 'excerpt':
if ($atts['excerpt'] == 'true') {
$content .= "<td><a href='http://www.google.com/search?q={get_the_title()}> target="_blank"><img border="0" alt="imagelink" src="wp-content/custom/imageicon.png"></a></td>";
}
break;
 
EDIT: I also tried your method 2, also a line error. As it the hyperlink needed to be in the form of a image I also included those parts.
Edited by frankdm
Link to comment
Share on other sites

I had edited the post when I spotted the error. Go back to the previous post. 

 

I tried your edited post like this:

 

case 'excerpt':
if ($atts['excerpt'] == 'true') {
$content .= "<td><a href='http://www.google.com/search?q=". get_the_title()."'> target="_blank"><img border="0" alt="imagelink" src="wp-content/custom/imageicon.png"></a></td>";
}
break;
 
Still a line error :(
Link to comment
Share on other sites

  • Solution

Are you defining $content before here? Reason is you are doing dot equals. If it is not defined change .= to just =

 

The image doesn't help. Where is $content first defined? Meaning where is content= without the period?

Edited by benanamen
Link to comment
Share on other sites

Are you defining $content before here? Reason is you are doing dot equals. If it is not defined change .= to just =

 

The image doesn't help. Where is $content first defined? Meaning where is content= without the period?

 

Here is the entire php file

www.mediansoft.net/php.zip

 

The error says line 485 is wrong (if it helps you locate the code we are talking about in the php file)

 

Thank you again for the help! I have very, VERY limited php knowledge so while i can copy paste edit things and generally understand what the code is doing, i cannot write it myself :(

Edited by frankdm
Link to comment
Share on other sites

This is getting boring.

 

Again: If you cannot get the syntax right, use an IDE. This far more efficient than going back and forth in a forum.

 

Screenshot:

attachicon.gifScreenshot_2016-07-23_21-58-05.png

 

Thank you for trying to help but I do not know what a IDE is (except a sort of hard drive connection) and my php knowledge is not existant... I need to alter this wordpress plugin (the whole plugin is this one file) and I already contacted the author but he does not reply. It is a old plugin but there is nothing like it so I have to do it myself, but i can't, so i ask for help here.

Link to comment
Share on other sites

Are you defining $content before here? Reason is you are doing dot equals. If it is not defined change .= to just =

 

The image doesn't help. Where is $content first defined? Meaning where is content= without the period?

 

The author of the plugin tried to help me but either gave up or has connection issues on Skype.. but the last thing he told me to try was this:

 

case 'excerpt':
if ($atts['excerpt'] == 'true') {
$content .= "<td><a href=\"http://www.google.com/search?q=' . get_the_title() . ' \" target=\"_blank\"><img border=\"0\" alt=\"imagelink\" src=\"wp-content/custom/imageicon.png\"></a></td>";
}
break;
 
But this makes the image search for:
' . get_the_title() . '
 
But it is close i think :(
Link to comment
Share on other sites

 

The author of the plugin tried to help me but either gave up or has connection issues on Skype.. but the last thing he told me to try was this:

 

case 'excerpt':
if ($atts['excerpt'] == 'true') {
$content .= "<td><a href=\"http://www.google.com/search?q=' . get_the_title() . ' \" target=\"_blank\"><img border=\"0\" alt=\"imagelink\" src=\"wp-content/custom/imageicon.png\"></a></td>";
}
break;
 
But this makes the image search for:
' . get_the_title() . '
 
But it is close i think :(

 

 

Your issues are due to your use of quotation marks, you were indeed correct in your assumption that the last person who helped you over skype had been close to getting it right, although he used single quotation marks rather than double which is your issue.

$content .= "<td><a href=\"http://www.google.com/search?q=" . get_the_title() . " \" target=\"_blank\"><img border=\"0\" alt=\"imagelink\" src=\"wp-content/custom/imageicon.png\"></a></td>";

This code should work fine.

Edited by Jordan97
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.