1

I'm taking a Udemy class to learn how to make Wordpress plugins and themes. The video I'm on is having me override the default search widget with a custom one via searchform.php. From what I've gathered, this file should automatically be loaded with the rest of the theme as long as I have followed these steps:

/theme_dir/functions.php

/* Some other code */
include ( get_theme_file_path('/includes/widgets.php') );
add_action("widgets_init", 'ju_widgets');

/theme_dir/includes/widgets.php

function ju_widgets() {
    register_sidebar([
        'name'          => __('My First Theme Sidebar', 'udemy'),
        'id'            => 'ju_sidebar',
        'description'   => __('Sidebar for the theme Udemy', 'udemy'),
        'before_widget' => '<div id="%1$s" class="widget clearfix %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4>',
        'after_title'   => '</h4>',
    ]);
}

/theme_dir/sidebar.php

<div class="sidebar nobottommargin col_last">
    <div class="sidebar-widgets-wrap">
        <?php
            if (is_active_sidebar('ju_sidebar')) {
                dynamic_sidebar('ju_sidebar');
            }
        ?>
    </div>
</div>

/theme_dir/searchform.php

<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>

<form role="search" method="get" class="search-form"
      action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <div class="input-group">
        <input type="search" id="<?php echo $unique_id; ?>"
               class="form-control" name="s"
               value="<?php the_search_query(); ?>"
               placeholder="<?php _e( 'Search', 'udemy' ); ?>"/>
        <span class="input-group-btn">
            <button type="submit" class="btn btn-danger"><i class="icon-search"></i></button>
        </span>
    </div>
</form>

/theme_dir/index.php

<!-- some theme html -->

<?php get_sidebar(); ?>

<!-- other theme html -->

No matter what I change in the searchform.php file, it doesn't replace the default search widget.

3
  • searchform.php is rendered via get_search_form if the widget doesn't use get_search_form and instead has its own separate search-form it wouldn't work. How old is the course you're studying & does it take into account that widgets have recently been replaced by gutenberg blocks?
    – admcfajn
    Nov 8 '21 at 22:31
  • Gutenberg blocks are further in the course. I guess the guy didn't bother to take out this lesson. Unless this course redeems its self between now and the end i'm giving it a 1 star. What a waste of a whole day trying to figure out what I did wrong. Except it wasn't me in the first place. From what I gathered so far the teacher is an idiot. This is coming from a seasoned Laravel dev, so I think I have a "cred" to judge him at this point. Thanks for the help
    – Daemonleak
    Nov 8 '21 at 23:20
  • Happy to help, welcome to WordPress.StackExchange
    – admcfajn
    Nov 9 '21 at 2:17
1

This way of working with widgets is outdated and I should be using Gutenberg blocks. Moral of the story, be careful when buying courses from Udemy, they may just be useless.

2
  • Well more out-of-date than useless. You should probably pass that on as feedback to Udemy.
    – Rup
    Nov 9 '21 at 9:45
  • True, I'll shoot him a message and see what he says.
    – Daemonleak
    Nov 9 '21 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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