ThemeShaper Forums » Thematic

[closed]

Set newly created widget as a standard widget in Primary Aside

(25 posts)
  • Started 11 years ago by bogh
  • Latest reply from sylvainww
  • This topic is resolved
  1. bogh
    Member

    Hi,

    I have started working with thematic one month ago, basically I want to create a child-theme with some widgets.
    My issue is that I can't find a method to add my newly created widgets to appear from standard in Primary or Secondary Aside, as the widgets "Search", "Pages", "Categories", "Archives".
    And how I can make some standard widgets to not be standard.

    This could not be a Thematic thing, but I haven't found a answer nowhere, so I would really appreciate your help.
    I hope you understand what I am trying to say...

    Thank you.

    Posted 11 years ago #
  2. thematic sets its default widgets with the function thematic_init_presetwidgets() in widget-extensions.php

    you can override this function by creating your own childtheme_override_init_presetwidgets()

    Posted 11 years ago #
  3. bogh
    Member

    Thank you Helga, this is very helpful.

    Posted 11 years ago #
  4. bogh
    Member

    Please tell me if I am using this right, because I can't figure it out.
    I saw something like that in some of your old posts, but is not working for me at all.
    I have changed the name every time I've tested the theme, but it takes the default thematic widget configuration or the configuration of the last theme activated.

    function childtheme_override_init_presetwidgets() {
    	$childtheme_override_init_presetwidgets = array (
    		'primary-aside'  => array( 'meta', 'meta'),
    		'secondary-aside'  => array( 'meta' ),
    		'1st-subsidiary-aside'  => array( 'meta' ),
    		'2nd-subsidiary-aside'  => array( 'meta' ),
    		'3rd-subsidiary-aside'  => array( 'meta' )
    
    	);
    return $childtheme_override_init_presetwidgets;
    }
    add_filter('thematic_preset_widgets','childtheme_override_init_presetwidgets' );

    I appreciate your help,
    Thank you a lot.

    Posted 11 years ago #
  5. where did you get a filter from? there's no apply_filters in the thematic_init_presetwidgets() function... and apply_filters is your signal that you can add a filter.

    overrides replace a function w/o needing to add it back (either with add_action or add_filter). when you use the override, it is customary to copy the existing function into your functions.php, then rename it following the override naming convention. (ie replacing thematic_ with childtheme_override_ )

    i honestly have no idea how you've come up w/ what you've come up with but it is completely wrong and doesn't look anything like the original thematic_init_presetwidgets() function.

    function childtheme_override_init_presetwidgets() {
    		update_option( 'widget_search', array() );
    		update_option( 'widget_pages', array() );
    		update_option( 'widget_categories', array() );
    		update_option( 'widget_archives', array() );
    		update_option( 'widget_links', array() );
    		update_option( 'widget_rss-links', array() );
    		update_option( 'widget_meta', array() );
    	}

    totally untested, so no idea if that does anything either.

    Posted 11 years ago #
  6. middlesister
    Member

    I think adding your widgets is a two step process. You need the function Helga showed you to register the widget, but the part that is responsible for connecting your widget with the widget area is at the end of thematic_widgets_init. There you will see the filter called thematic_preset_widgets. The array you want to filter looks like this

    $preset_widgets = array (
    	'primary-aside'  => array( 'search-2', 'pages-2', 'categories-2', 'archives-2' ),
    	'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' )
    );

    So filter it like this

    childtheme_filter_preset_widgets( $preset_widgets ) {
    	$preset_widgets = array (
    		'primary-aside'  => array( 'search-2', 'pages-2', 'categories-2', 'archives-2' ),
    		'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' ),
    		'1st-subsidiary-aside' => array( 'widget-id' )
    	);
    	return $preset_widgets;
    }
    add_filter('thematic_preset_widgets','childtheme_filter_preset_widgets');

    I think it registers the widgets using widget-id's so you need to use the id of the widget you register in your childtheme_override_init_presetwidgets(). And if you want to remove a widget from the preset, just remove the id from the array.

    Posted 11 years ago #
  7. ahhh.... so it is. missed that part, which is what happens when you don't test. apologies for sounding short in my last message. i didn't look at the code enough.

    try out middlesister's code and report back.

    Posted 11 years ago #
  8. bogh
    Member

    Thank you both for your answers, but it is not working yet.

    Please see the code below, I have added this one to the functions.php, I think it is almost what I need, at least it seems plausible, but something might by wrong, maybe a wrong filter or hook.

    I have replaced thematic_ with childtheme_override_ as Helga said, the code is part of widgets-extensions.php

    function childtheme_override_init_presetwidgets() {
    		update_option( 'widget_about', array( 2 => array( 'title' => '' ), '_multiwidget' => 1 ) );
    		update_option( 'widget_pages', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_categories', array( 2 => array( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
    		update_option( 'widget_archives', array( 2 => array( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
    		update_option( 'widget_links', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_rss-links', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_meta', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    }
    add_action( 'childtheme_override_presetwidgets', 'childtheme_override_init_presetwidgets' );
    
    function childtheme_override_widgets_init() {
    
    	// Pre-set Widgets
    	$preset_widgets = array (
    		'primary-aside'  => array( 'about', 'pages-2', 'categories-2', 'archives-2' ),
    		'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' )
    		);
    
        if ( isset( $_GET['activated'] ) ) {
        	childtheme_override_presetwidgets();
      		update_option( 'sidebars_widgets', apply_filters('childtheme_override_preset_widgets',$preset_widgets ));
      	}
    
    }

    Can you please tell me where I can read about hooks, filters, args, maybe some kind of tutorial, because I still can't make difference between them.

    Thank you :)

    Posted 11 years ago #
  9. bogh
    Member

    I have tried to make the second part like it follows, but still no luck

    function childtheme_override_widgets_init($preset_widgets) {
    
    	// Pre-set Widgets
    	$preset_widgets = array (
    		'primary-aside'  => array( 'about', 'pages-2', 'categories-2', 'archives-2' ),
    		'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' )
    		);
    	return $preset_widgets;
    }
    add_filter('thematic_preset_widgets','childtheme_override_widgets_init');
    Posted 11 years ago #
  10. good start, b/c childtheme_override_widgets_init does not exist in the 'override' sense. but, middlesister had a typo (one that i make often myself). it should be add_filter and not apply_filter since we're trying to add our filter function to the filter hook (which is signified by apply_filters in the core). i have amended her code and your code, try that.

    i wrote this post to try to explain filters/actions.
    http://forums.themeshaper.com/topic/need-help-understanding-actions-vs-filters-and-changing-post-meta-and-utility#post-22638

    it might not be immediately clear, but kyle asked some good newbie questions and i tried to explain further. i think it is pretty comprehensive. it is a lot of reading, but you are trying to learn a new language here.... you aren't going to get it in 10 minutes. (though if you do, please share your secret b/c it took me a year to fully get filters!)

    if that change still doesn't work i'll try to test something locally....

    Posted 11 years ago #
  11. middlesister
    Member

    Thanks Helga, I just saw that myself. Annoying to not be able to edit your own mistakes.

    @bogh:
    The childtheme_override would be for the first part. Your childtheme_override_init_presetwidgets() looks good to me, the only thing you have changed is added the about widget, right? Just remove the add_action part, you don't need any add_action or add_filter for the childtheme_override functions.

    I don't know what id your about widget gets, but following the same principle as the others, try changing the second part to

    function childtheme_filter_preset_widgets( $preset_widgets ) {
    	$preset_widgets = array (
    		'primary-aside'  => array( 'about-2', 'pages-2', 'categories-2', 'archives-2' ),
    		'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' ),
    	);
    	return $preset_widgets;
    }
    add_filter('thematic_preset_widgets','childtheme_filter_preset_widgets');

    I suggest you do not use the word override in your filters. Even though you can, and it would work, it is bound to be confusing in the end. The childtheme_override functions of thematic are separate from the actions and filters.

    Posted 11 years ago #
  12. bogh
    Member

    Thank you again for your time, but still no luck for me. I think I am getting more and more confuse with this, maybe because I am not good with this hooks, filters etc....

    Helga, I would appreciate if you will try to test this, I am working only on this for about 3-4 days now, every time I feel like it is good but it is still not.
    I am sure that this will help others too.

    And thanks for sharing your post Helga, I will try to learn this as more as I can, I just hope to not take me more than a year to understand it :)

    Posted 11 years ago #
  13. the childtheme_override_init_presetwidgets() function just sets the titles and settings of the widgets. it is the filter that "sets" the widgets on activation. middlesister forgot to write function before the function, but that filter does set the widgets as advertised.

    if your new widget isn't getting set then i'd guess it is some sort of disconnect between the name of the widget and the options you are updating.... and i'm looking at this now and am a little confused by it myself.

    Posted 11 years ago #
  14. bogh
    Member

    My widgets are made after the model from widgets.php, and they are working fine, except the fact that they are not showing by default where I want.
    I have tried lot of times only with the default widgets, but no result. Maybe you will have a better one.

    Posted 11 years ago #
  15. bogh
    Member

    Hi again,

    I am wondering if you have found a solution for this...
    I have tried few more times, but nothing.

    Thank you.

    Posted 11 years ago #
  16. was that a week ago already? geez. but the answer was no, i was never able to get this to work with anything other that the default widgets. sorry.

    Posted 11 years ago #
  17. middlesister
    Member

    I had to try this, and the code above actually works. First of all, if you use a custom widget make sure you have registered it on the widget_init hook. If you can see your widget among the available widgets then it is registered, so you don't have to worry about that.

    I think the key for everything to come together is to get the name of the update_option right. This is the code that worked for me:

    function childtheme_filter_preset_widgets( $preset_widgets ) {
    	$preset_widgets = array (
    		'primary-aside'  => array( 'mytestwidget-2', 'pages-2', 'categories-2', 'archives-2' ),
    		'secondary-aside'  => array( 'links-2', 'rss-links-2', 'meta-2' ),
    	);
    	return $preset_widgets;
    }
    add_filter('thematic_preset_widgets','childtheme_filter_preset_widgets');
    
    function childtheme_override_init_presetwidgets() {
    		update_option( 'widget_mytestwidget', array( 2 => array( 'title' => '1st' ), '_multiwidget' => 1 ) );
    		update_option( 'widget_pages', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_categories', array( 2 => array( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
    		update_option( 'widget_archives', array( 2 => array( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
    		update_option( 'widget_links', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_rss-links', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    		update_option( 'widget_meta', array( 2 => array( 'title' => ''), '_multiwidget' => 1 ) );
    }
    add_action( 'childtheme_override_presetwidgets', 'childtheme_override_init_presetwidgets' );

    Where mytestwidget is the name of the widget you want to add (that would probably be the lowercase name of the widget class of your custom widget). The number is important. You can add several instances of the widget in your update_option like this

    update_option( 'widget_mytestwidget', array( 2 => array( 'title' => '1st widget' ), 3 => array( 'title' => '2nd widget' ), '_multiwidget' => 1 ) );

    And then you could place them in the sidebars with

    $preset_widgets = array (
    	'primary-aside'  => array( 'mytestwidget-2', 'pages-2', 'categories-2', 'archives-2' ),
    	'secondary-aside'  => array( 'mytestwidget-3', 'links-2', 'rss-links-2', 'meta-2' ),
    	);
    Posted 11 years ago #
  18. bogh
    Member

    Thank you for testing, this works exactly as I wanted.

    Thanks again.

    Posted 11 years ago #
  19. middlesister for the win!

    Posted 11 years ago #
  20. Hi !

    After several hours of struggle I managed to make middlesister's code work. With one exception, which gave trouble in the first place : to make it work I have to remove the if ( isset( $_GET['activated'] ) ) {</code in the widget-extensions.php of the thematic framework :


    if ( isset( $_GET['activated'] ) ) {
    thematic_presetwidgets();
    update_option( 'sidebars_widgets', apply_filters('thematic_preset_widgets',$preset_widgets ));
    }

    Only then my own preset widgets appear (and not the default one) when I install my theme from scratch Of course I realize that's not the right solution since now nothing I put in my widgets is saved. But do you have any idea where that could come from ?

    Thanks ! And sorry if my question seems stupid

    Posted 11 years ago #
  21. Hi again,

    Any ideas on how I can solve this problem ? Thanks !

    Posted 11 years ago #
  22. i'm confused, b/c middlesister's code solved the problem for me. what isn't it doing for you?

    the isset( $_GET['activated'] ) is there to only run the code on theme activation. if you remove it, it will run every time and as you have discovered, any changes to your widget set up will be lost.

    Posted 11 years ago #
  23. Hi, Thanks for the answer. The problem that still remains for me is that for any new installation of my theme (available here : http://www.apptamin.com/websites-mobile-apps/apptamin-a-more/) the widgets in the widget-areas are not the ones I want, but the default (thematic ?) ones : search, meta, etc.

    Posted 11 years ago #
  24. i just activated your theme locally. the active widgets are definitely NOT the thematic defaults, so your attempt to set default widgets appears to be working just fine.

    Posted 11 years ago #
  25. Thanks for helping me out !

    So you mean that when you activated the them, the custom widget areas were not filled with the usual thematic widgets (like meta, links, search, etc.) ? Because it keeps happening to me and although it's kinda getting me crazy !

    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.