4

I'm trying to add a php code inside a custom HTML widget but the site in showing this like plain text.

Any idea why is this happening?

PHP CODE:

<?php
$param = uniqid();
echo "<script language='javascript'>";
echo "alert('message successfully sent ' . $param)";
echo "</script>";
?>

HTML:

enter image description here

HOW I SEE THE PAGE:

enter image description here

3
  • 2
    Wordpress' Custom HTML widget doesn't support PHP. You would need to install a plugin for that functionality, ie PHP Code Widget Commented Sep 27, 2017 at 18:21
  • since it's a simple JS code, why not simply put it without PHP tag Commented Sep 27, 2017 at 18:23
  • its simple js only for the example. Commented Sep 27, 2017 at 20:57

1 Answer 1

2

Same problem happenned to me today!!, resolved it by adding the following code to the Wordpress theme function.php file.

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;
}

To enable shortcodes in the Text and Custom HTML widget just add this code to your theme functions.php :

add_filter('widget_text','do_shortcode',10);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.