0

The following function is pluggable as it contains a filter :

// Make function pluggable/overwritable
if ( ! function_exists( 'wpsight_layout_images' ) ) {

    function wpsight_layout_images() {

        $layout_images = array(
            'show_on_single'         => true,
            'size_archive'           => 'post-thumbnail',
            'align_archive'          => 'left',
            'size_single'            => 'post-thumbnail',
            'align_single'           => 'left',
            'size_widget'            => 'post-thumbnail',
            'align_widget'           => 'left',
            'size_archive_listings'  => 'post-thumbnail',
            'align_archive_listings' => 'right',
        );

        return apply_filters( 'wpsight_layout_images', $layout_images );

    }

}

My question is the following. Lets say I need to change the value of the two last variables size_archive_listings and align_archive_listings using the filter. Do I have to copy over the whole function or can some variables be changed independently ? What is the proper of using add_filter from a child theme functions file in this situation ?

1
  • 2
    The function is "pluggable" because it is wrapped in a check if( function_exists() ), letting it be defined in a file that loads before the one which contains this code. the fact that it also contains a filter is unrelated to it being pluggable.
    – Milo
    Oct 9, 2013 at 13:56

2 Answers 2

1

As noted in @Milo's comment, a function being "Pluggable" is entirely separate from it being filterable. A Pluggable function can be overridden wholesale, because of the if ( function_exists() ) conditional in which it's wrapped. A Plugin is filterable if its output is parsed through an apply_filters() call.

Since you only want to change a couple values in the returned array, you want to add a filter, rather than Plug the entire function.

Simply add the following in your Child Theme functions file:

function wpse117251_wpsight_layout_images( $layout_images ) {
    // Modify array values
    $layout_images['size_archive_listings'] = 'your-string-here';
    $layout_images['align_archive_listings'] = 'your-string-here';
    // Return the array
    return $layout_images;
}
add_filter( 'wpsight_layout_images', 'wpse117251_wpsight_layout_images' );
1

As Milo mentioned in his comment, the function is pluggable because it is wrapped in the function_exists() wrapper. To change the settings individually you would take advantage of the filter.

function wpa_117251( $layout_images ){
  $layout_images['size_archive_listings'] = 'medium';
  $layout_images['align_archive_listings'] = 'left';
  return $layout_images;
}
add_filter( 'wpsight_layout_images', 'wpa_117251' );
2
  • I guess you meant to write add_filter instead of add_action? ;-)
    – birgire
    Oct 9, 2013 at 14:43
  • Yup! Appreciate the edit. Oct 9, 2013 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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