Change Favicon via CustomField in Settings Page
Support › MB Settings Page › Change Favicon via CustomField in Settings PageResolved
- This topic has 5 replies, 2 voices, and was last updated 2 years, 10 months ago by
Tobias Haas.
-
AuthorPosts
-
June 7, 2022 at 4:18 AM #36345
Tobias Haas
ParticipantHello 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 });
June 7, 2022 at 12:48 PM #36349Long Nguyen
ModeratorHi Tobias,
You can echo the variable
$favicon
without using the functionget_field()
, I think it comes from ACF.link.href = '<?php echo $favicon; ?>';
June 7, 2022 at 1:01 PM #36350Tobias Haas
ParticipantHey 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 });
June 7, 2022 at 6:47 PM #36356Tobias Haas
Participantok 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!
June 8, 2022 at 10:14 AM #36363Long Nguyen
ModeratorHi,
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 ?>';
June 8, 2022 at 1:59 PM #36368Tobias Haas
ParticipantThank you very much Long!
It finally works! You saved my day -
AuthorPosts
- You must be logged in to reply to this topic.