• Resolved SunnyOz

    (@sunnyoz)


    I have created my own wordpress theme, and have used it for several websites. It includes several customised widgets.. some on the home page, and some just in the sidebars of specific pages. All has worked fine… until this latest new website.

    I am using the same theme, and the same function.php and a similar front.php page. But when I deleted 3 widgets in my function.php page (used on the previous website, not being used here), and tried to add a new widget specific to this website – nothing changes on the list of widgets in the admin dashboard (found under the appearance section).. I still see the old widgets listed, and I don’t see the new one.

    I am working on localhost right now, so I can’t give you a link, but here is all the relevant code (I think) that I have in function.php and front.php to display new widgets.

    FUNCTION.PHP:

    function create_widget( $name, $id, $description, $beforewidget, $beforetitle, $aftertitle ) {
    
    	register_sidebar(array(
    		'name' => __( $name ),	 
    		'id' => $id, 
    		'description' => __( $description ),
    		'before_widget' => $beforewidget,
    		'after_widget' => '</div>',
    		'before_title' => $beforetitle,
    		'after_title' => $aftertitle,
    	));
    
    }
    
    create_widget( 'Front Page Blog', 'front-blog', 'Displays just under hero image of the homepage', '<div class="widget">', '<h3>', '</h3>' );
    
    
      Note:

    I didn’t include the create_widget lines that I removed for the old widgets.. but even after I removed their create statements from the function.php page, they still show up in the list of widgets.

    FRONT.PHP:

              <div class="thumbnail-kenburn thumbnail-kenburnl">
    			<h1>Latest from the Blog</h1>
    
    			<?php if ( dynamic_sidebar ( 'front-blog' ) ); ?>
    
              </div>
    

    Obviously nothing will show up on the front.php (home) page, as I can’t customise the widget yet.. because it isn’t in the list on the admin widget area!.

    Do I need to change some other area of wordpress to add a new widget?

    I am sure it is something silly I have done or not done, but I can’t seem to figure it out after spending most of the day researching and playing with various suggested changes. I just don’t understand how I was able to add widgets in the past with this same theme and same files, and can’t do it now.

    Any guidance would be greatly appreciated.

    Cheers,
    SunnyOz

    • This topic was modified 5 years, 2 months ago by t-p. Reason: Moved to fixing wordpress. This is not a Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • First, let’s correct some terminology. The theme creates widget areas (sidebars), and the user puts widgets into the widget areas.

    You showed your function and how it is called, but is it on an action hook so that it is called at the correct time?

    Also, front.php is not in the template hierarchy, so you have to be using that as a page template to see it. The code you showed here has only the if statement to check the widget area, but not the output of the widget area.

    Thread Starter SunnyOz

    (@sunnyoz)

    Thanks for your quick reply @joyously.

    I guess I am a bit confused at how you stated the corrections.. but I will take the advice of someone who is more experienced than I.

    Note: I miss-typed the name of the home page.. you were right.. it is called: front-page.php.

    Since I am creating the website and content initially for the whole site, I will be creating the new widgets via function.php page. And I will be (instead of the user) be filling in the content of the widgets.. at least first time around. I agree in most cases, the widgets are used in the sidebar, but the one I am trying to add is going be be pulled into my home page. Since the home page is a static page, it is setup to use the font-page.php page as it’s page template.. hence the reason I am calling the widget from that page.

    The code I gave you has worked fine for several websites I have created using this same based template. You can see the website I used as a base here: oasisflighttraining.com.au – . If you look at the bottom of that home page (just before the footer), you will see 3 clickable images. They are all in their own widget area.. and they are called there from the front-page.php page. (These 3 widgets are actually the ones I am trying to remove for the new website.. at the same time trying to add a new / different widget to the home page.) Those 3 widgets were defined with a similar create statement in the function.php page..and that worked as it should, creating the ‘shell’ of the widget to be available for customisation in the admin area. The code that displayed those three widgets on the home page were written as follows:

    FRONT-PAGE.PHP:

        <div class="container">
          <div class="row">
            <div class="col-md-4 col-sm-4 col-xs-12">
              <div class="thumbnail-kenburn thumbnail-kenburnl">
    
    			<?php if ( dynamic_sidebar ( 'front-left' ) ); ?>
    
              </div>
            </div>
            <div class="col-md-4 col-sm-4 col-xs-12">
              <div class="thumbnail-kenburn thumbnail-kenburnc">
    
    		
    			<?php if ( dynamic_sidebar ( 'front-center' ) ); ?>
           
              </div>
            </div>
            <div class="col-md-4 col-sm-4 col-xs-12">
              <div class="thumbnail-kenburn thumbnail-kenburnr">
    
    			<?php if ( dynamic_sidebar ( 'front-right' ) ); ?>
    
              </div>
            </div>
          </div>
    

    I know you are saying that the code isn’t correct.. yet it does call in the correct widgets as defined in the function.php file.

    I don’t see an action hook in the Oasis site’s function.php.. this one does work.. so I am not sure how it does what it does? I’ll display the entire file so maybe you can see where the action call might be??

    FUNCTION.PHP:

    <?php
    
    function theme_styles() {
    
    	wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.min.css' );
    	wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );
    
    }
    add_action( 'wp_enqueue_scripts', 'theme_styles' );
    
    function theme_js() {
    
    	global $wp_scripts;
    
    	wp_register_script( 'html5_shiv', 'https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js', "", "", false );
    	wp_register_script( 'respond_js', 'https://oss.maxcdn.com/respond/1.4.2/respond.min.js', "", "", false );
    
    	$wp_scripts->add_data( 'html5_shiv', 'conditional', 'lt IE 9' );
    	$wp_scripts->add_data( 'respond_js', 'conditional', 'lt IE 9' );
    	
    	wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '', true );
    	wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'bootstrap_js'), '', true );
    	wp_enqueue_script( 'stickynav_js', get_template_directory_uri() . '/js/stickynav.js', array('jquery'), '', true );
    
    }
    add_action( 'wp_enqueue_scripts', 'theme_js', 'stickynav_js' );
    
    //add_filter( 'show_admin_bar', '__return_false' );
    
    add_theme_support( 'menus' );
    add_theme_support( 'post-thumbnails' );
    
    function register_theme_menus() {
    	register_nav_menus(
    		array (
    			'header-menu'	=> __( 'Header Menu' )
    		)
    	);
    }
    add_action( 'init', 'register_theme_menus' );
    
    add_action( 'init', 'my_add_excerpts_to_pages' );
    
    function my_add_excerpts_to_pages() {
    	add_post_type_support( 'page', 'excerpt' );
    }
    
    function create_widget( $name, $id, $description, $beforewidget, $beforetitle, $aftertitle ) {
    
    	register_sidebar(array(
    		'name' => __( $name ),	 
    		'id' => $id, 
    		'description' => __( $description ),
    		'before_widget' => $beforewidget,
    		'after_widget' => '</div>',
    		'before_title' => $beforetitle,
    		'after_title' => $aftertitle,
    	));
    
    }
    
    create_widget( 'Front Page Left', 'front-left', 'Displays on the left of the homepage', '<div class="widget">', '<h3>', '</h3>' );
    create_widget( 'Front Page Center', 'front-center', 'Displays in the center of the homepage', '<div class="widget">', '<h3>', '</h3>' );
    create_widget( 'Front Page Right', 'front-right', 'Displays on the right of the homepage', '<div class="widget">', '<h3>', '</h3>' );
    
    create_widget( 'Front Page Sidebar', 'front-side', 'Displays sidebar after header image', '<div class="widget sider">', '<div class="headline"><h3>', '</h3></div>' );
    
    create_widget( 'Footer Left', 'footer-left', 'Displays on the left of the footer', '<div class="widget">', '<h2>', '</h2>' );
    create_widget( 'Footer Center', 'footer-center', 'Displays in the center of the footer', '<div class="widget">', '<h2>', '</h2>' );
    create_widget( 'Footer Right', 'footer-right', 'Displays on the right of the footer', '<div class="widget">', '<h2>', '</h2>' );
    
    create_widget( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar', '<div class="widget blog-twitter">', '<div class="headline"><h3>', '</h3></div>' );
    create_widget( 'Page Sidebar contact', 'page2', 'Displays on the side of pages with a sidebar with contact button', '<div class="widget">', '<h3>', '</h3>' );
    create_widget( 'Page Sidebar short', 'short-testimonial', 'Displays on the side of short pages', '<div class="widget blog-twitter">', '<div class="headline"><h3>', '</h3></div>' );
    create_widget( 'Page Sidebar about', 'about-side', 'Displays on the side of the About page', '<div class="widget">', '<h3>', '</h3><br /><br />' );
    create_widget( 'Page Sidebar theory', 'theory-side', 'Displays on the side of the Aviation Theory page', '<div class="widget">', '<h3>', '</h3><br /><br />' );
    create_widget( 'Page Sidebar trial', 'trial-side', 'Displays on the side of the Trial Introductory Flight page', '<div class="widget">', '<h3>', '</h3><br /><br />' );
    
    create_widget( 'Blog Sidebar', 'blog', 'Displays on the side of pages in the blog section', '<div class="widget">', '<h3>', '</h3>' );
    
    create_widget( 'Off-Canvas Sidebar', 'offcanvas', 'Displays on the side of pages with an off-canvas sidebar', '<div class="widget">', '<h3>', '</h3>' );
    
    function ninja_forms_add_nf_styles( $form_id ) {
        if( $form_id == 1 ) {
    //        echo '<style>
    //            .ninja-forms-form-wrap{background:red}
    //        </style>';
    		wp_enqueue_style( 'ninja_forms-nf-css', get_template_directory_uri() . '/css/ninja_forms.css' );
        }
    }
    
    add_action ( 'ninja_forms_display_css', 'ninja_forms_add_nf_styles' );
    
    ?>
    

    So why does this code work for the Oasis site.. and not for the new site. They both have the same code structure.

    Can you give me any insight in how to fix the new site?

    Thanks,
    SunnyOz

    • This reply was modified 5 years, 2 months ago by SunnyOz. Reason: tried to provide link to website referenced

    My mistake for the sidebar call. I was thinking of another function that is typically used. Your if statement threw me off. This is what is usually done.

    if ( is_active_sidebar( 'side-widget-area' ) ) {
    	dynamic_sidebar( 'side-widget-area' );
    }

    Rather than explain all the changes needed, here is how I would change it (although I would remove the page excerpt thing since it’s not a theme functionality).

    <?php
    
    function my_theme_styles() {
    
    	wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.min.css' );
    	wp_enqueue_style( 'my_theme', get_template_directory_uri() . '/style.css' );
    
    }
    add_action( 'wp_enqueue_scripts', 'my_theme_styles' );
    
    function my_theme_js() {
    
    	wp_register_script( 'html5_shiv', 'https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js', "", "", false );
    	wp_register_script( 'respond_js', 'https://oss.maxcdn.com/respond/1.4.2/respond.min.js', "", "", false );
    
    	wp_script_add_data( 'html5_shiv', 'conditional', 'lt IE 9' );
    	wp_script_add_data( 'respond_js', 'conditional', 'lt IE 9' );
    	
    	wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '', true );
    	wp_enqueue_script( 'my_theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'bootstrap_js'), '', true );
    	wp_enqueue_script( 'stickynav_js', get_template_directory_uri() . '/js/stickynav.js', array('jquery'), '', true );
    
    }
    add_action( 'wp_enqueue_scripts', 'my_theme_js', 'stickynav_js' );
    
    function my_theme_setup() {
    	add_theme_support( 'title-tag' );
    	add_theme_support( 'post-thumbnails' );
    
    	register_nav_menus(
    		array (
    			'header-menu'	=> __( 'Header Menu' )
    		)
    	);
    }
    add_action( 'after_setup_theme', 'my_theme_setup' );
    
    function my_add_excerpts_to_pages() {
    	add_post_type_support( 'page', 'excerpt' );
    }
    add_action( 'init', 'my_add_excerpts_to_pages' );
    
    function my_create_widget_areas( $name, $id, $description, $beforewidget, $beforetitle, $aftertitle ) {
    
    	register_sidebar( 'Front Page Left', 'front-left', 'Displays on the left of the homepage', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    	register_sidebar( 'Front Page Center', 'front-center', 'Displays in the center of the homepage', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    	register_sidebar( 'Front Page Right', 'front-right', 'Displays on the right of the homepage', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    
    	register_sidebar( 'Front Page Sidebar', 'front-side', 'Displays sidebar after header image', 
    		'<div class="widget sider">', '</div>', '<div class="headline"><h3>', '</h3></div>' );
    
    	register_sidebar( 'Footer Left', 'footer-left', 'Displays on the left of the footer', 
    		'<div class="widget">', '</div>', '<h2>', '</h2>' );
    	register_sidebar( 'Footer Center', 'footer-center', 'Displays in the center of the footer', 
    		'<div class="widget">', '</div>', '<h2>', '</h2>' );
    	register_sidebar( 'Footer Right', 'footer-right', 'Displays on the right of the footer', 
    		'<div class="widget">', '</div>', '<h2>', '</h2>' );
    
    	register_sidebar( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar', 
    		'<div class="widget blog-twitter">', '</div>', '<div class="headline"><h3>', '</h3></div>' );
    	register_sidebar( 'Page Sidebar contact', 'page2', 'Displays on the side of pages with a sidebar with contact button', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    	register_sidebar( 'Page Sidebar short', 'short-testimonial', 'Displays on the side of short pages', 
    		'<div class="widget blog-twitter">', '</div>', '<div class="headline"><h3>', '</h3></div>' );
    	register_sidebar( 'Page Sidebar about', 'about-side', 'Displays on the side of the About page', 
    		'<div class="widget">', '</div>', '<h3>', '</h3><br /><br />' );
    	register_sidebar( 'Page Sidebar theory', 'theory-side', 'Displays on the side of the Aviation Theory page', 
    		'<div class="widget">', '</div>', '<h3>', '</h3><br /><br />' );
    	register_sidebar( 'Page Sidebar trial', 'trial-side', 'Displays on the side of the Trial Introductory Flight page', 
    		'<div class="widget">', '</div>', '<h3>', '</h3><br /><br />' );
    
    	register_sidebar( 'Blog Sidebar', 'blog', 'Displays on the side of pages in the blog section', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    
    	register_sidebar( 'Off-Canvas Sidebar', 'offcanvas', 'Displays on the side of pages with an off-canvas sidebar', 
    		'<div class="widget">', '</div>', '<h3>', '</h3>' );
    }
    add_action( 'widgets_init', 'my_create_widget_areas' );
    
    function my_ninja_forms_add_nf_styles( $form_id ) {
        if( $form_id == 1 ) {
    //        echo '<style>
    //            .ninja-forms-form-wrap{background:red}
    //        </style>';
    		wp_enqueue_style( 'ninja_forms-nf-css', get_template_directory_uri() . '/css/ninja_forms.css' );
        }
    }
    
    add_action ( 'ninja_forms_display_css', 'my_ninja_forms_add_nf_styles' );
    

    Having no trailing ?> is intentional. It reduces the chance of having errors because of white space being output.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Can’t add new or remove old widgets’ is closed to new replies.