Web Hosting
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Running PHP code from Text Widget in WordPress

Discussion in 'Misc WordPress Requests' started by Samuel Wood (Otto), Dec 6, 2017 at 9:16 AM.

  1. Samuel Wood (Otto)

    Guest

    Reply To: Running PHP code from Text Widget in WordPress, by Samuel Wood (Otto)

    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.

    Reply To: Running PHP code from Text Widget in WordPress
     
    #1

Share This Page

Web Hosting