• Resolved leanne63

    (@leanne63)


    In a custom theme, I’ve created a section in Customizer where the user can add any social media links they have (eg, the URLs for their Facebook, Twitter, email, etc).

    I’ve called the section ‘social-media’.

    I can access the individual items, of course, via something like:
    $twitter_url = get_theme_mod('twitter-url');.

    But, how can I retrieve a count of settings that contain values?

    For example, maybe they’ve only entered their Facebook and Twitter URLs, but nothing else. In that case, I’d like to be able to do something like:
    $setting_count = count( get_theme_mods( 'social-media') );
    and get a result of 2.

    However, it appears that get_theme_mods() returns an array that contains a list of ALL settings, regardless of section they’re in. Does something exist that will let me get the count for a particular section?

Viewing 1 replies (of 1 total)
  • Thread Starter leanne63

    (@leanne63)

    To solve this, I did the following:

    • Changed my customize_register function so all of my custom settings’ ids were prefixed with their section’s id. (For example, I changed the ‘twitter_url’ setting id to ‘social_media_twitter_url’. I changed the Facebook setting id from ‘facebook_url’ to ‘social_media_facebook_url’, and so on.).
    • Wrote a function that accepts a section id (such as ‘social_media’), then searches the results of get_theme_mods(), and returns a new array with only the items that match the provided section id. (For example, get_section_mods( ‘social_media’ ) returns array([‘social_media_twitter_url]=>’https://twitter.com/…’ [social_media_facebook_url]=>’https://facebook.com/…’, and so on.)

    For my purposes, I also coded the function to check for a value for the found ids. If a setting doesn’t have a value, the function doesn’t include it in the returned array.

Viewing 1 replies (of 1 total)
  • The topic ‘How to get count of settings set within a Customizer section?’ is closed to new replies.