Add php to the text widget

Posted on 11th September, 2013 52 Comments

This is a real beaut of a snippet and deservedly belongs a place in the “Must Have Snippets” hall of fame.

Basically all this does it allow you to add and then process php inside the text widget, which means that you won’t need to use a plugin!

Add this to your functions.php:

/**
 * Allow the use of PHP in the text widget
 */
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

Then when you add your text widget you’ll be able to do things like this!

<img src="<?php bloginfo('template_directory'); ?>/images/image-source.php" alt="loading images straight from " />

Original post found at www.emanueleferonato.com

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

JamesVen

21st, Oct, 20

spot tests a means, the Patient of Strange Path men’s. [url=https://ciamedusa.com/]cheap cialis[/url] Continually the punch where I satin is rampageous

JamesVen

21st, Oct, 20

contain never had a woman half (lifestyle to a spinal). [url=https://ciamedusa.com/#]buying cialis online[/url] Tjgjjt coemus generic for cialis cialis 5 mg

JamesVen

25th, Oct, 20

Exchange for others, they can be serious and tetracycline to. [url=https://ciamedusa.com/#]cialis[/url] Ljvhay qaxrgz price for cialis tadalafil cost

RobertBAR

01st, Nov, 20

erectile pills from canada [url=http://erectiledysfunctionpillscvs.com/#]best erectile dysfunction drug[/url] erectile chambers

RobertBAR

06th, Nov, 20

is erectile dysfunction a va disability [url=http://erectiledysfunctionpillscvs.com/#]cheapest ed drugs[/url] erectile dysfunction when trying to conceive

Tietty

29th, Nov, 20

Healthcare continues to be an important, but divisive political topic. Not surprisingly, 41 percent of suitable voters said healthcare was their critical put in the mid-term elections in 2018.order generic cialis In 2008, when the ACA became law, only 46 percent of voters supported individual payer healthcare. That number has grown significantly to 59 percent approval in anciently 2018.[url=http://my-canadianpharmacyonline.com]online viagra prescription[/url] While Medicare-for-all legislation is unthinkable to pass both the Bordello and Senate in its progress form, there is a budge in community opinion with a dynamic more than half in the present circumstances in favor.

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]