• Resolved pandraka

    (@pandraka)


    I’m using a child theme for twenty-twelve. I had added the following code that allowed me to add php code while using the text widget

    function php_execute($html){
    if(strpos($html,”<“.”?php”)!==false){ ob_start(); eval(“?”.”>”.$html);
    $html=ob_get_contents();
    ob_end_clean();
    }
    return $html;
    }
    add_filter(‘widget_text’,’php_execute’,100);

    This worked fine until the latest release, now there is an editor telling me I can’t save the code until I use escape codes for the < and > brackets. If I do that the code doesn’t work. How do I shut down the editor so I can use the php override?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Don’t shut down the editor :p Use a plugin https://www.ads-software.com/plugins/php-code-widget/

    Thread Starter pandraka

    (@pandraka)

    Andrew, i’d pefer not use anymore plugins than necessary. Is there some work around that I could use instead of shutting down the editor?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Can I ask why you don’t want to go through a plugin?

    Thread Starter pandraka

    (@pandraka)

    Yes, I’m working with the Navy. The site is complicated and is already using 35 pluggins, all of which need to be vetted.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Hi. I’m Otto. I wrote the php code widget.

    I would highly recommend that instead of trying to run code in a text widget, that you use the php code widget instead. It’s the same as the text widget used to be, except with the addition of code that is basically similar to your own to run PHP code.

    Actually, I would recommend that you don’t use the PHP Code widget, nor this code you have to allow you to do it in a text widget. Really, you should instead put your PHP code into a custom plugin just for your site, which adds a specific custom widget just for you. This is much safer than storing executable code in the database and allowing it to be edited through the widgets interface.

    Making your own custom widget isn’t difficult. I could provide you with a quick tutorial if you like.

    Thread Starter pandraka

    (@pandraka)

    Thank you a tutorial would be very helpful.

    • This reply was modified 7 years, 2 months ago by pandraka.
    • This reply was modified 7 years, 2 months ago by pandraka.
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I’ll write one up later on my blog.

    Meanwhile, here’s quick example code to get you up and running:

    
    <?php
    /*
    Plugin Name: My Custom Widget
    */
    
    class My_Custom_Widget extends WP_Widget {
    	function __construct() {
    		$widget_ops = array('classname' => 'widget_mycustom', 'description' => 'My Custom Widget');
    		$control_ops = array('width' => 400, 'height' => 350);
    		parent::__construct('mycustom', __('My Custom Widget', 'my-custom-widget'), $widget_ops, $control_ops);
    	}
    
    	function widget( $args, $instance ) {
    		extract($args);
    		echo $before_widget;
    		?>
    		<h2 class="widget-title">My Custom Widget</h2>
    		<div>Here's my custom stuff</div>
    		<?php
    		echo $after_widget;
    	}
    
    }
    add_action('widgets_init', create_function('', 'return register_widget("My_Custom_Widget");'));
    

    That’s pretty self-explanatory. It’s a custom widget with no options or settings, that outputs just a single thing saying “Here’s my custom stuff”. Obviously, you want to rename everything to be specific to your needs, and to put in whatever code you need to run there.

    Thread Starter pandraka

    (@pandraka)

    Thank you I’ll start working on it right away.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Thread Starter pandraka

    (@pandraka)

    Thank you for all your help, my new widget is fully tested and ready for our production environment. I also read the tutorial on your blog, I found it very helpful. Thanks again.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Running PHP code from Text Widget in WordPress’ is closed to new replies.