Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Finally I finished to write my widget, and now want to call it using function inside themes php files. My widget looks like this:

    function reg_custom_widget() {
        register_widget('wp_custom_widget');
    }
    class wp_custom_widget extends WP_Widget {
        function wp_custom_widget() { /* constructor */ }
        function widget($args, $instance) { /* here is the code, that uses 'core' functions to build the result and display it */ }
        function shortcode_handler($atts) { /* the same code as in widget() with a couple of features */ }

        /* here are basic functions, strongly related. they perform main widget functionality */
        function core_func_one($input) { }
        function core_func_two($input) { }
        function core_func_three($input) { }
        function core_func_four($input) { }
        function core_func_five($input) { }

        function update($new_instance, $old_instance) { /* updating settings */ }   
        function form($instance) {  /* building widget setup-form */ }
    }

I already know about following methods, that call widget, and can be placed in themes files:
1. calling via shortcode with do_shortcode() wp function;
2. calling widget with the_widget() wp function;
Both this methods work great, but my designer rejects them.
He want to hard code calling of widget in this way: if(function_exists('custom_widget')) { custom_widget(); }

How can I realize this option? Maybe is reasonable to make another one function in the widget class, make it global or something, it will run the widget from inside of it? Sounds like delirium, but I don't really understand now how to resolve this problem.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Using the_widget() is the correct way to "hard code" an arbitrary Widget in a template file:

<?php the_widget( 'foobar_widget', $instance, $args ); ?>

Since the_widget() simply returns if 'foobar_widget' is not a registered Widget, it is functionally equivalent to using an if ( function_exists( $function ) ) conditional.

You could look in core to see how the_widget() verifies 'foobar_widget' is a registered Widget ( if ! is_a( $widget_obj, 'foobar_widget' ) )); but really, why bother? Just call the_widget(). That's what it's there for.

share|improve this answer
    
Thanks for the answer. This is my designer's wish, he wants to use 'oldschool' variant of this method. Anyway, lead programmer used to call widgets in themes file in this 'oldschool' way, so I will ask him about it. I will write here about his solution. But for now, thank you. –  drake2300 Nov 30 '12 at 13:32
    
There is no "old school" method for this. You're simply being asked to use a method that doesn't apply. A Widget isn't a function, but rather an extension to a class. –  Chip Bennett Nov 30 '12 at 13:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.