admin管理员组文章数量:1326614
my plugin ships with certain images that are shown in the frontend when using the extension. The images are good, but in certain circumstances the website owner might want to change it.
What's the best way to do so? I can think of the following two ways, is there a better one? Specifically I would love the idea to allow a short function in PHP living outside my plugin to change it.
The two ideas I have
- ensure proper CSS IDs so that it's easy to replace the image with a custom CSS
- implement the functionality to upload a different logo in the backend and then check if this was done and either show the default one or the uploaded one.
Thanks a lot for any pointers,
Hartwig
my plugin ships with certain images that are shown in the frontend when using the extension. The images are good, but in certain circumstances the website owner might want to change it.
What's the best way to do so? I can think of the following two ways, is there a better one? Specifically I would love the idea to allow a short function in PHP living outside my plugin to change it.
The two ideas I have
- ensure proper CSS IDs so that it's easy to replace the image with a custom CSS
- implement the functionality to upload a different logo in the backend and then check if this was done and either show the default one or the uploaded one.
Thanks a lot for any pointers,
Hartwig
Share Improve this question asked Aug 3, 2020 at 9:30 HartwigHartwig 1032 bronze badges 2- So you're expecting people to use code or plugins to customise this? You could just add a filter that people can hook to change the URLs. – Rup Commented Aug 3, 2020 at 10:23
- How would that look like? – Hartwig Commented Aug 3, 2020 at 12:01
1 Answer
Reset to default 2Specifically I would love the idea to allow a short function in PHP living outside my plugin to change it.
This is exactly what filters are for. They allow code inside your plugin to call out to anyone else who wants to register a handler function for that filter. You probably have done this with the Wordpress 'system' actions and filters by adding your own code using add_filter
, and it's easy to make a new one of those filters that anyone else can call.
E.g., in your plugin perhaps you have this right now:
<img src="path/to/plugin/logo.jpg">
So first you need to make sure whatever you want to let other people filter is in a variable:
<?php
$my_plugin_logo = "path/to/plugin/logo.jpg";
echo "<img src=\"" . $my_plugin_logo ."\">";
Then allow any other code to filter it:
<?php
$my_plugin_logo = "path/to/plugin/logo.jpg";
$filtered_logo = apply_filters('myplugin_filter_logo', $my_plugin_logo);
echo "<img src=\"" . $filtered_logo ."\">";
This calls any code that's registered a function against the new filter you've just made called 'myplugin_filter_logo'
.
E.g., your users could put this in their own functions.php or in a new plugin to change the logo:
add_filter('myplugin_filter_logo', 'bobs_new_logo', 10, 1);
function bobs_new_logo($old_logo_path) {
return "path/to/bobs/logo.gif";
}
Note:
- The filter functions gets your old value, but as in this case doesn't have to use it
- If there are no filter functions registered, the default value that you passed in will be returned
本文标签: customizationBest way to allow overwriting images shipped with the plugin
版权声明:本文标题:customization - Best way to allow overwriting images shipped with the plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213424a2434131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论