frankdm Posted July 23, 2016 Share Posted July 23, 2016 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: <a href="http://www.google.com/search?q=</a> And this needs to work in the case above. I cannot figure it out Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/ Share on other sites More sharing options...
Jacques1 Posted July 23, 2016 Share Posted July 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534911 Share on other sites More sharing options...
benanamen Posted July 23, 2016 Share Posted July 23, 2016 (edited) Depends on how get_the_title() is structured. Does it end with return or echo? If it is return then $content .= "<td><a href='http://www.google.com/search?q=". get_the_title()."'></a></td>"; Edited July 23, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534912 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 (edited) 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 July 23, 2016 by frankdm Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534913 Share on other sites More sharing options...
benanamen Posted July 23, 2016 Share Posted July 23, 2016 (edited) I had edited the post when I spotted the error. Go back to the previous post. Edited July 23, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534914 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534915 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 (edited) Here is a picture showing the code, maybe it helps: http://www.mediansoft.net/helpphp.jpg As you can see, i can already tell by the colors my editor is using something is wrong Edited July 23, 2016 by frankdm Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534916 Share on other sites More sharing options...
Solution benanamen Posted July 23, 2016 Solution Share Posted July 23, 2016 (edited) 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 July 23, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534917 Share on other sites More sharing options...
Jacques1 Posted July 23, 2016 Share Posted July 23, 2016 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: Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534919 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 (edited) 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 July 23, 2016 by frankdm Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534920 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 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: Screenshot_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. Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534921 Share on other sites More sharing options...
frankdm Posted July 23, 2016 Author Share Posted July 23, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1534922 Share on other sites More sharing options...
Jordan97 Posted July 25, 2016 Share Posted July 25, 2016 (edited) 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 July 25, 2016 by Jordan97 Quote Link to comment https://forums.phpfreaks.com/topic/301567-hyperlink-in-php-harder-than-it-sounds/#findComment-1535007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.