• Here is what I have in parent functions.php

    // Set image sizes upon theme activation
    if(! function_exists( 'theme_image_setup' )):
        function theme_image_setup() {
            // updating thumbnail and image sizes
            update_option('thumbnail_size_w', 200);
            update_option('thumbnail_size_h', 150);
            update_option('medium_size_w', 620);
            update_option('medium_size_h', '');
            update_option('large_size_w', 940);
            update_option('large_size_h', '');
        }
    endif;
    
    add_action( 'switch_theme', 'theme_image_setup' );

    and in child

    function theme_image_setup() {
        // updating thumbnail and image sizes
        update_option( 'thumbnail_size_w', 150, true );
        update_option( 'thumbnail_size_h', 150, true );
        update_option( 'medium_size_w', 620, true );
        update_option( 'medium_size_h', '', true );
        update_option( 'large_size_w', '', true );
        update_option( 'large_size_h', 500, true );
    }

    This looks like it will work perfectly, child overriding parent theme. The problem is it is reverse. When I activate parent theme, the settings from child is coming and vice versa. This code updates the media setting under Settings -> Media. What am I doing wrong? Any help is greatly appreciated. Thanks.

Viewing 15 replies - 1 through 15 (of 19 total)
  • Try hooking into after_setup_theme instead of switch_theme.

    I would not generally recommend using the switch_theme action hook.

    Thread Starter Chandra M

    (@nhuja)

    Thank you Chip.

    The problem is I want to change the image sizes later. If I use after_setup_theme, it works (as did init which I was using) but then its fixed and can’t change it manually (Settings -> Media). If I use switch_theme, I can change the values under Settings -> Media yet fix the image size as the theme needs when activated. But the only issue in this approach is its showing just the opposite. If I activate parent theme, it shows child media settings and vice versa. Any other activation hook available?

    Thread Starter Chandra M

    (@nhuja)

    I am trying to set this up because, many people forget to update the media settings (or inherit from previous themes) and they complain about incorrect image sizes. If I can work this out, it would help those people and then if anyone wants to customize, they can change as well (without having to change the above code). It would be great to have this integrated to WP core if it helps so no user will have to think about that particular page for making the theme work.

    Right, but switch_theme isn’t working. ??

    Eventually, Themes will get activation/deactivation/delete hooks, as Plugins currently have. At that time, such hooks would be the correct ones to use here.

    Perhaps a more important question: why is your Theme changing the defaults for the built-in image sizes, rather than creating its own custom sizes via add_image_size()?

    Thread Starter Chandra M

    (@nhuja)

    Its because, I am using

    the_post_thumbnail('thumbnail');

    or medium / large and I can’t use those names in add_image_size. If I use custom names, users won’t be able to change them (without changing the code). Eventually, Settings -> Media is the only option people can change if they don’t know code and users really don’t know. ??

    Just trying to make things simpler as much as possible: Activate: theme image sizes. Want to change go to Media Settings. Thats all.

    Thread Starter Chandra M

    (@nhuja)

    And to add changing code in parent/child theme isn’t an option because with every update of the theme, they will have to go and make that change. Thats pretty annoying, you know. ??

    Just create your own image size, e.g.:

    add_image_size( 'theme-slug-post-thumbnail', 123, 456, true );

    Then, when you call the_post_thumbnail(), pass it your custom image size:

    <?php the_post_thumbnail( 'theme-slug-post-thumbnail' ); ?>
    Thread Starter Chandra M

    (@nhuja)

    And how do we change the size if you want without changing the code?

    And how do we change the size if you want without changing the code?

    You’re the Theme developer; why wouldn’t you know the image size you need? If you’re calling the_post_thumbnail(), then you are outputting the image as part of the Theme design.

    Thread Starter Chandra M

    (@nhuja)

    I am trying to make things easy for the non developers who use the theme. I guess you didn’t understand my question. I just want to know why switch_theme doesn’t work as it should? ??

    I guess you didn’t understand my question. I just want to know why switch_theme doesn’t work as it should? ??

    Well, let’s investigate. Consider what you said here:

    This looks like it will work perfectly, child overriding parent theme. The problem is it is reverse. When I activate parent theme, the settings from child is coming and vice versa.

    When the Parent Theme is active, nothing from the Child Theme should ever even be available to the Parent Theme. The Parent Theme doesn’t even have a way of knowing that the Child Theme exists.

    The only way for what you’re describing to happen would be for the Child Theme to be active at the time that switch_theme action is fired.

    Thus, I would guess that the observed behavior indicates that the your action is firing before the Theme is actually switched during the do_action( 'switch_theme' ).

    You can try a couple things:

    1) Add a high priority, such as 99, to your add_action() call
    2) Try using a different hook, such as after_switch_theme

    Thread Starter Chandra M

    (@nhuja)

    Alright, I am in a conclusion that this is not possible unless we write more advanced coding. Thanks for you help Chip.

    I’m with you Chip, it’s after setup them, and NOT switch theme. I can’t actually think of a valid reason to use switch theme in a child theme anyway?

    Chandra, did you figure this out? The code is easier, and not more ‘advanced’.

    Thread Starter Chandra M

    (@nhuja)

    We tried many things but couldn’t make it work so we left it as it is. Theoritically after_setup_theme should have worked perfectly, but it somehow doesn’t. Maybe a bug or something.

    Thanks for asking blitz999.

    No worries, I have to say I had an issue with using after setup them and TwentyTwelve today.

    My easy workaround was to simply use add_image_size. This works a treat.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Child function not overriding parent function / acts opposite’ is closed to new replies.