// This class has 3 properties defining 3 differend markup elements.
// I need to be able to change the output of $x, $y and $z easilly.
class XYZ {
protected static $x = '<div class="something_wrap">%s</div>';
protected static $y = '<div class="something_inner">%s</div>';
protected static $z = '<div class="something_inner">%s</div>';
}
SHORT VERSION:
A plugin I'm developing must be highly customizable in the output. I coulde either use WordPress' filters or define some setters. I wonder which approach is the best one and why.
LONG VERSION:
I would like to use this class inside of a plugin on different websites. On each the markup will be sligtly different (in order to accomodate layout/spacing differences) and must therefore be easily customizable. A solution could be to use some static setters by which define the markup. For example:
// I can define a "global/site-wide" markup with just three lines in my functions.php.
// I could then hook those three lines in a wp_head hook, just to make sure they won't run more than once.
XYZ::setStaticX( '<div class="something_wrap row col-12 p-5">%s</div>' );
XYZ::setStaticY( '<div class="something_inner col-12 col-sm-6">%s</div>' );
XYZ::setStaticZ( '<div class="something_inner col-12 col-sm-6">%s</div>' );
// If I ever require a different "X" markup for a specific instance of the class, I can do this:
$example = new XYZ;
$example->setX( '%s' );
An alternative would be using Wordpress' filters:
// Again, I can define a "global/site-wide" behaviour like this:
add_filter( 'xyz_filter_x', function( $value ) {
return '<div class="something_wrap row col-12 p-5">%s</div>';
}, 10 );
add_filter( 'xyz_filter_y', function( $value ) {
return '<div class="something_inner col-12 col-sm-6">%s</div>';
}, 10 );
add_filter( 'xyz_filter_z', function( $value ) {
return '<div class="something_inner col-12 col-sm-6">%s</div>';
}, 10 );
// And then if I need to customize something, I could change the first filter to be something like this:
add_filter( 'xyz_filter_x', function( $value, $id ) {
if ( $id === 5 ) {
return '%s';
} else {
return '<div class="something_wrap row col-12 p-5">%s</div>';
}
}, 10, 2 );
I believe the second approach conforms to WordPress guidelines, but everything becomes hard to read once you define 20, 30, 40 or more filters in functions.php
. On the other hand the first approach seems more readable and clean to me, but I wonder if there are any drawbacks to this specific solution.
This leaves me wondering which approach is the best one and why.
// This class has 3 properties defining 3 differend markup elements.
// I need to be able to change the output of $x, $y and $z easilly.
class XYZ {
protected static $x = '<div class="something_wrap">%s</div>';
protected static $y = '<div class="something_inner">%s</div>';
protected static $z = '<div class="something_inner">%s</div>';
}
SHORT VERSION:
A plugin I'm developing must be highly customizable in the output. I coulde either use WordPress' filters or define some setters. I wonder which approach is the best one and why.
LONG VERSION:
I would like to use this class inside of a plugin on different websites. On each the markup will be sligtly different (in order to accomodate layout/spacing differences) and must therefore be easily customizable. A solution could be to use some static setters by which define the markup. For example:
// I can define a "global/site-wide" markup with just three lines in my functions.php.
// I could then hook those three lines in a wp_head hook, just to make sure they won't run more than once.
XYZ::setStaticX( '<div class="something_wrap row col-12 p-5">%s</div>' );
XYZ::setStaticY( '<div class="something_inner col-12 col-sm-6">%s</div>' );
XYZ::setStaticZ( '<div class="something_inner col-12 col-sm-6">%s</div>' );
// If I ever require a different "X" markup for a specific instance of the class, I can do this:
$example = new XYZ;
$example->setX( '%s' );
An alternative would be using Wordpress' filters:
// Again, I can define a "global/site-wide" behaviour like this:
add_filter( 'xyz_filter_x', function( $value ) {
return '<div class="something_wrap row col-12 p-5">%s</div>';
}, 10 );
add_filter( 'xyz_filter_y', function( $value ) {
return '<div class="something_inner col-12 col-sm-6">%s</div>';
}, 10 );
add_filter( 'xyz_filter_z', function( $value ) {
return '<div class="something_inner col-12 col-sm-6">%s</div>';
}, 10 );
// And then if I need to customize something, I could change the first filter to be something like this:
add_filter( 'xyz_filter_x', function( $value, $id ) {
if ( $id === 5 ) {
return '%s';
} else {
return '<div class="something_wrap row col-12 p-5">%s</div>';
}
}, 10, 2 );
I believe the second approach conforms to WordPress guidelines, but everything becomes hard to read once you define 20, 30, 40 or more filters in functions.php
. On the other hand the first approach seems more readable and clean to me, but I wonder if there are any drawbacks to this specific solution.
This leaves me wondering which approach is the best one and why.
Share Improve this question edited Jun 3, 2019 at 7:39 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Jun 3, 2019 at 7:19 CerereCerere 2314 silver badges11 bronze badges1 Answer
Reset to default 0Questions of the 'which is best' type are impossible to answer, because they depend on the eventual use case. If the users who are building the individual sites are familiar with object oriented programming the first approach may be cleanest. If they are less schooled in OOP, you might go for the filters.
But I think you are overlooking the easiest approach, namely delegating everything to the options page of your plugin. If all you need is 20 to 40 different classes to accommodate layout issues, you can simply define an array with default variables/classes inside your object class and, on initialization, look if some of those need to be replaced by values in the options array.