Php in Text Widget
-
Hello everyone!
I would like to be able to execute a php code in my Text widgets. For example on one site I have made a function for my own little youtube video gallery and I would like to display it in the text widget like: “<php my_function_video_gallery(); ?>”.
Plus I would like to do it without a plugin (have tried https://wordpress.org/plugins/php-code-widget/ and that works). I have found several tutorials for this:
https://firstsiteguide.com/run-php-code-text-widget-wordpress/
https://wordpress.stackexchange.com/questions/215463/how-to-run-php-code-in-text-widget-with-no-plugin-in-wp-4-4
http://www.emanueleferonato.com/2011/04/11/executing-php-inside-a-wordpress-widget-without-any-plugin/
https://www.wpstuffs.com/how-to-execute-php-code-in-text-widget-without-using-plugin/
…As you can see, they are all pretty much the same and I understand what the code is trying to do. Unfortunately it does not work. The Text Widget does not execute php command…
Here are some screenshots. Here I am trying to use a custom function for displaying dummy ad, but that is not really important. Point is, I want to be able to use php in Text widget…
This is my functions.php: https://drive.google.com/file/d/15pjZqwVV6TUoBoNOe7G_aLawSkiMwgYK/view?usp=sharing
This is my widget area:
https://drive.google.com/file/d/1Wzi0owshfQycykPi1ZY0sq3TaJKNGyYK/view?usp=sharing
On the site it just displays (no execution…):
https://drive.google.com/file/d/185i3_dxDx17p3KCUJoyG5GWWzMGGOWod/view?usp=sharing
-
You can’t use PHP in a standard text widget, so you’ll need another one that can do that.
With a (very) quick search in the plugins repoistory, this one looks good:
https://wordpress.org/plugins/php-code-widget/
But there’s a lot more options:
@catacaustic Thanks for reply! But… read my submit again. Plugin you posted is mentioned there, I know it works, but I am looking for a way to do it without a plugin. I am sure I am not the only one, since those tutorials I posted are exactly trying to do that too. But for some reason they dont work and I want to figure out why…
If the plugin works, I’m not sure why you don’t want to use it, but I would guess you want to try this for yourself?
Your code is not going to help, mainly because you’ve got in in a screenshot, so we can’t copy it to try and debug it. For next time, plain text works better. 🙂
This case calls for some basic PHP debugging, and I can see where there’s some issues with the tutorial. I’d start with something like this to see what you’re working with, and how it’s being processed:
function php_execute( $html ){ // Debugging... echo "<p>Debugging 1: '".$html."'</p>"; if( strpos( $html, "<"."?php" )!==false ){ ob_start (); eval( "?".">".$html ); $html = ob_get_contents (); ob_end_clean (); } // if () // Debugging... echo "<p>Debugging 2: '".$html."'</p>"; return $html; } // php_execute () add_filter('widget_text','php_execute',100);
That will show you what is, or isn’t, happening with the code. From there you can work into it and see why it’s having an issue.
@catacaustic
Thank you for another reply! I am trying to do it without a plugin because:- I am trying to use as little plugins as possible. I just dont generally like the idea of having a lot of plugins for every single seemingly simple thing…
- The code in tutorials seems good enough for me so I am hoping to make it work.
- I am still quite new to WordPress, but I like to code and experiment.
- Therefore yes – I would love to try it by myself :).
… But back to the problem. I tried some basic php echoes even before, pretty much just like you proposed – at the start and end of the function. On the site it shows:
Debugging 1: ‘<?php show_ad(1); ?>’
Debugging 2: ‘<?php show_ad(1); ?>’
<?php show_ad(1); ?>So the function obviously receives the input html string, but fails to process it. I tried to dig deeper:
function php_execute( $html ){ // Debugging... echo "<p>Debugging 1: '".$html."'</p>"; // There seems to be a problem here... echo "<p>Debugging 2: '".strpos( $html, "<"."?php" )."'</p>"; if( strpos( $html, "<"."?php" ) !== false ) { echo "<p>Debugging 3: Condition Entered. </p>"; ob_start (); eval( "?".">".$html ); $html = ob_get_contents (); ob_end_clean (); } // if () // Debugging... echo "<p>Debugging 4: '".$html."'</p>"; return $html; } // php_execute () add_filter('widget_text','php_execute',100);
On site:
Debugging 1: ‘<?php show_ad(1); ?>’
Debugging 2: ”
Debugging 4: ‘<?php show_ad(1); ?>’
<?php show_ad(1); ?As you can see, the Debugging 3 line is missing, therefore the function does not enter the condition… Thats why I echoed the result of the strpos() before the condition in “Debuggin 2” and it returns an empty string… So it seems that function is not working as expected and is probably interpreted as false. I am not that skilled in php, but I know some programming languages tend to do this…
Any ideas? Thanks again for help.
That makes sense. 🙂
What’s happening there is that the widget is turning the ‘<?php’ into HTML-safe characters of ‘<’?php’ before it gets sent to your function. That’s pretty standard security stuff, so to be expected.
What I’d do is to convert it to “pure” text so that you can see if the ‘<?php’ is there. If it is, process it, and if not, convert it back so it is as expected. Something like this (not tested, so see how it goes):
function php_execute( $html ){ $coverted_html = html_entity_decode( $html ); // Debugging... echo "<p>Debugging 1: '".$html."'</p>"; // There seems to be a problem here... echo "<p>Debugging 2: '".strpos( $html, "<"."?php" )."'</p>"; if( strpos( $converted_html, "<"."?php" ) !== false ) { echo "<p>Debugging 3: Condition Entered. </p>"; ob_start (); eval( "?".">".$converted_html ); $html = ob_get_contents (); ob_end_clean (); } // if () else { $html = htmlentities( $coverted_html); } // else // Debugging... echo "<p>Debugging 4: '".$html."'</p>"; return $html; } // php_execute () add_filter('widget_text','php_execute',100);
- The topic ‘Php in Text Widget’ is closed to new replies.