Referencing Data from Settings Page

Support MB Settings Page Referencing Data from Settings Page

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2067
    87Pancakes87Pancakes
    Participant

    Hi,

    I'm trying to call a field from the settings page but it's just breaking my theme.

    <?php $settings = get_option( 'my_options' );
     
    $field_id = 'facebook-url';
    if ( isset( $settings[$field_id] ) )
    {
        return $settings[$field_id];
    } 
    
    ?>
    #2068
    87Pancakes87Pancakes
    Participant

    Any help would be greatly appreciated. 🙂 Thank-you.

    #2071
    Anh TranAnh Tran
    Keymaster

    Hi, where do you put the code above into your theme? Maybe you should not return the result as you will use it in another way (display it or use as a variable). Using return in a PHP file will stop execution of the following code.

    #2072
    87Pancakes87Pancakes
    Participant

    Hi, it's in the header.

    I'm just using the code on this page; https://metabox.io/docs/mb-settings-page/

    I have fixed it now using the following code;

    <?php $field_id = 'twitter-url';
     if ( isset( $settings[$field_id] ) ) {
     echo '<li><a href=" ' . $settings[$field_id] . '"><i class="fa fa-twitter"></i></a></li>'; } ?>

    How do I echo nothing if the field is empty?

    #2083
    Anh TranAnh Tran
    Keymaster

    Glad to see you figured it out. To echo nothing if the field is empty, you just need to change the code a little bit:

    <?php
    $field_id = 'twitter-url';
    if ( !empty( $settings[$field_id] ) ) {
        echo '<li><a href=" ' . $settings[$field_id] . '"><i class="fa fa-twitter"></i></a></li>';
    }
    ?>
Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Referencing Data from Settings Page’ is closed to new replies.