Viewing 5 replies - 1 through 5 (of 5 total)
  • 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:

    https://wordpress.org/plugins/search/php+widget/

    Thread Starter roppard

    (@roppard)

    @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.

    Thread Starter roppard

    (@roppard)

    @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.

    • This reply was modified 5 years, 10 months ago by roppard.
    • This reply was modified 5 years, 10 months ago by roppard.
    • This reply was modified 5 years, 10 months ago by roppard.

    That makes sense. 🙂

    What’s happening there is that the widget is turning the ‘<?php’ into HTML-safe characters of ‘&lt’?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);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Php in Text Widget’ is closed to new replies.