Change Favicon via CustomField in Settings Page

Support MB Settings Page Change Favicon via CustomField in Settings PageResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #36345
    Tobias HaasTobias Haas
    Participant

    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
    });
    #36349
    Long NguyenLong Nguyen
    Moderator

    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; ?>';

    #36350
    Tobias HaasTobias Haas
    Participant

    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
    });
    #36356
    Tobias HaasTobias Haas
    Participant

    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
    });

    Settings Page
    custom field
    custom field2

    Thank you for support!

    #36363
    Long NguyenLong Nguyen
    Moderator

    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  ?>';
    
    #36368
    Tobias HaasTobias Haas
    Participant

    Thank you very much Long!
    It finally works! You saved my day

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.