Support Forum
Support › MB Settings Page › Change Favicon via CustomField in Settings PageResolved
Hello dear Metabox team,
I try to change the favicon via custom field... but I fail... I put together a code here, but I don't know if it works like that.
Can you help me please? what am I doing wrong?
<?php $favicon = rwmb_get_value( 'favicon', ['object_type' => 'setting'], 'site_option' ) ?>
<?php
add_action('wp_head', function(){
?>
<script type="text/javascript">
var link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
}
link.href = '<?php echo get_field( "$favicon" ); ?>';
</script>
<?php
});
Hi Tobias,
You can echo the variable $favicon
without using the function get_field()
, I think it comes from ACF.
link.href = '<?php echo $favicon; ?>';
Hey Long, thanks
Yes, it is quite possible that this comes from ACF...
I changed it in my code but still get an error - I forgot to mention it in my first post.
Error Message:
Call to undefined function rwmb_get_value()
On line: 1
<?php $favicon = rwmb_get_value( 'favicon', ['object_type' => 'setting'], 'site_option' ) ?>
<?php
add_action('wp_head', function(){
?>
<script type="text/javascript">
var link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
}
link.href = '<?php echo $favicon; ?>';
</script>
<?php
});
ok got it! Now it should work.
But somehow it doesn't get any data... Am I not targeting the field correctly?
<?php
add_action('wp_head', function(){
$favicon = rwmb_get_value( 'favicon', ['object_type' => 'setting'], 'site_option' );
?>
<script type="text/javascript">
var link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
}
link.href = '<?php echo $favicon; ?>';
</script>
<?php
});
Thank you for support!
Hi,
1. I do not see the option name of the settings page, please add the option name for the settings page Administration. Get more details here https://docs.metabox.io/extensions/mb-settings-page/#using-meta-box-builder
2. And please follow this documentation to know how to get the image URL https://docs.metabox.io/fields/single-image/#template-usage
$favicon = rwmb_get_value( 'favicon', ['object_type' => 'setting'], 'your_option_name' );
$favicon_url = $favicon['url'];
link.href = '<?php echo $favicon_url ?>';
Thank you very much Long!
It finally works! You saved my day