• Resolved ejm

    (@llizard)


    I relatively recently switched to a child theme of twentytwelve I’m having fits trying to include a file on my side bar. The file shows variable entries that are based on what the date is. I used to use the following coding on the sidebar:

    <?php include($_SERVER['DOCUMENT_ROOT'] . "/textfiles/special.php"); ?>

    But, of course, php coding cannot be used in widgets.

    I’ve tried to search for the answer but do not know what search words to enter in order to find it….

    I’m guessing that I could achieve this by putting code into my theme’s functions file. But I’m stymied (a little knowledge of coding truly is a dangerous thing) about what exactly to put to include the file to appear just after the calendar on the sidebar.

    Could someone please point me in the direction of a page that outlines in dummy-fashion how to achieve this?

    Thank you.

    -E Morris, etherwork [dot] net [slash] blog << URL purposely left unlinked in an attempt to keep spammers at bay

    edit: in the meantime, I’ve hardcoded the entry that I would like to appear for the month of December.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Since you’re already using a child theme, you could put that code directly in sidebar.php above the call to dynamic_sidebar() and use CSS to style the result so that it looks like a widget.

    Another option would be to use this PHP code that will allow the built-in text widget to execute PHP:

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

    One last option would be to use a special text widget that can execute PHP code.

    Thread Starter ejm

    (@llizard)

    Thank you for your reply, stephencottontail.

    I am trying to avoid adding too many extra files to my child theme so have not added the sidebar.php at all.

    My grasp of PHP (in terms of security) is on the shaky side so I’m a little nervous to add the built-in text widget that allows PHP coding.

    I had no idea that there was such an option as a special text widget. Armed with new search terms, I’ll do a little more investigation.

    edit a few moments later:

    Yes, as I thought, enabling PHP coding in widgets (via the child functions file) might not be the best way for me to go:

    Use PHP in widgets very judiciously as this is a potential security vulnerability you open by enabling code execution from a widget, versus a file on the server. -https://www.carriedils.com/extend-wordpress-widgets-without-plugin/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to insert code using widgets OR child function file’ is closed to new replies.