WordPress.org

Ready to get started?Download WordPress

Forums

Child function not overriding parent function / acts opposite (20 posts)

  1. Chandra M
    Member
    Posted 2 years ago #

    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.

  2. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Try hooking into after_setup_theme instead of switch_theme.

    I would not generally recommend using the switch_theme action hook.

  3. Chandra M
    Member
    Posted 2 years ago #

    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?

  4. Chandra M
    Member
    Posted 2 years ago #

    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.

  5. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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()?

  6. Chandra M
    Member
    Posted 2 years ago #

    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.

  7. Chandra M
    Member
    Posted 2 years ago #

    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. :)

  8. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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' ); ?>
  9. Chandra M
    Member
    Posted 2 years ago #

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

  10. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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.

  11. Chandra M
    Member
    Posted 2 years ago #

    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? :)

  12. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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

  13. Chandra M
    Member
    Posted 2 years ago #

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

  14. blitz999
    Member
    Posted 1 year ago #

    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'.

  15. Chandra M
    Member
    Posted 1 year ago #

    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.

  16. blitz999
    Member
    Posted 1 year ago #

    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.

  17. Chandra M
    Member
    Posted 1 year ago #

    Its not that easy. :) The main issue was to set the image dimensions in the Media page as you activate the theme. add_image_size doesn't do this. Even if we add that, we will need to manually change the sizes in the Settings->Media page. We just wanted to make things easier for the user and as you might know this is the most ignored part by WordPress users. :)

  18. blitz999
    Member
    Posted 1 year ago #

    OK, I se what you mean. I'd still use the add image size to make it easy for yourself, otherwise you've got to make a database call to change those settings?? Which is clunky of course.

  19. Chandra M
    Member
    Posted 1 year ago #

    Yea, we could use that but you are only creating multiple instances of images which could easily be done with just those sizes, you know. :) Anyway, we concluded we would leave it just as it is so its all good. We did spend like 2 weeks on this. :)

  20. blitz999
    Member
    Posted 1 year ago #

    I hear ya! Might be one to put on the WP feature request forum? There's a lot of interesting talk about how the media library works right now.

Topic Closed

This topic has been closed to new replies.

About this Topic