wp-shortcake / shortcake-bakery

A fine selection of shortcodes for WordPress
42 stars 16 forks source link

Allow filtering shortcode output HTML classes #140

Closed montchr closed 8 years ago

montchr commented 8 years ago

I'm working on some new functionality on my site for which this plugin will be a huge help. But I need to be able to edit the classes of the output HTML.

For example, in class-instagram.php L70:

        return sprintf( '<iframe class="shortcake-bakery-responsive" src="%s" width="612" height="710" frameborder="0" scrolling="no"></iframe>',
            esc_url( sprintf( 'https://instagram.com/p/%s/embed/', $photo_id ) )
        );

I'd like to be able to add additional classes, maybe something like:

$classes = apply_filters( 'shortcake_instagram_classes', '' );

        return sprintf( '<iframe class="shortcake-bakery-responsive ' . $classes .'" src="%s" width="612" height="710" frameborder="0" scrolling="no"></iframe>',
            esc_url( sprintf( 'https://instagram.com/p/%s/embed/', $photo_id ) )
        );

I also think there should be some standardized classes across all of these shortcodes. The Soundcloud <iframe>, for example, has no classes at all, which makes targeting with CSS near impossible.

I'd be willing to open a PR for this if desired.

danielbachhuber commented 8 years ago

Rather than refactoring everything, what if we had a filter on do_shortcode_callback() which let you manipulate the return value however you liked?

https://github.com/wp-shortcake/shortcake-bakery/blob/master/inc/class-shortcake-bakery.php#L94-L107

montchr commented 8 years ago

Yes that does sound a lot simpler. :+1:

I do still think standardizing classes would be good, but that's probably better off as a new issue. Having a filter for output would negate an immediate need for changing other classes around too.

montchr commented 8 years ago

Would that be something like changing https://github.com/wp-shortcake/shortcake-bakery/blob/master/inc/class-shortcake-bakery.php#L106 from:

return $class::callback( $attrs, $content, $shortcode_tag );

to:

$output = $class::callback( $attrs, $content, $shortcode_tag );
return apply_filters( 'shortcake_bakery_output_callback', $output, $attrs, $content, $shortcode_tag );
danielbachhuber commented 8 years ago

Yep, although I'd do:

return apply_filters( 'shortcake_bakery_shortcode_callback', $output, $shortcode_tag, $attrs, $content );
montchr commented 8 years ago

Cool. I'll get on this tomorrow morning. Having some trouble actually loading the plugin as it doesn't want to work as a submodule within our theme (though it does work as a normal plugin)…