admin管理员组文章数量:1426855
// 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.
本文标签: Customize plugin39s output filters or setters looking for an advice
版权声明:本文标题:Customize plugin's output: filters or setters: looking for an advice 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745442104a2658491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论