简体   繁体   中英

wordpress custom html widget with PHP

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:

在此处输入图片说明

HOW I SEE THE PAGE:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM