How do I make a PHP function throw out html code?

unrealalex

New member
Feb 7, 2010
242
11
0
www.motivational5.com
In wordpress, lets say I want to have a php function that when called throws out html code. How would I accomplish this?

Lets say my code is the adsense javascript.

and if i want to make a php function get_adsense() that puts in that code in the wordpress loop, how do I define it?

function get_adsense() {
code that will return adsense script
}


Thanks. I'm sorry if I sound full retard, I have zero PHP experience and I'm currently learning through trial and error.
:bowdown:
 


I'm not entirely sure what you mean by "Wordpress loop"- likely 'cause I haven't done a ton of WP programming. I've pretty well only done theming and minor functional changes.

That said, you can do the following:

Exit the PHP tags so you don't have to encode your HTML. You can do this within a function, conditional statement, or otherwise, and the PHP will continue its execution.
PHP:
<?
#other funcs here

function get_adsense() {
 ?>
<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>
<script type="text/javascript"><!--
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();

pageTracker._trackPageview();
//-->
</script>
<?
} //end get_adsense func
?>
Or you can encapsulate your HTML src with PHP. Just don't forget to escape your special chars (like your quotes - you just need to put a \ before the char). Alternative you could just do ... echo 'source code here', but then escape any double quote the same way.
PHP:
<?
#other funcs here

function get_adsense() {
  echo "<script type=\"text/javascript\">"; //etc

 }
?>
 
  • Like
Reactions: unrealalex