Skip to content

Instantly share code, notes, and snippets.

@laytan
Last active September 13, 2019 19:21
Show Gist options
  • Save laytan/3c9f439cf931504ab718fe3b3cdd43ff to your computer and use it in GitHub Desktop.
Save laytan/3c9f439cf931504ab718fe3b3cdd43ff to your computer and use it in GitHub Desktop.
Easy wordpress social media icons, simply add an index to the theme_name_get_social_opts array and a everything to get it showing is handled.
<?php
if ( ! function_exists( 'theme_name_theme_customize_register' ) ) {
/**
* Register individual settings through customizer's API.
*
* @param WP_Customize_Manager $wp_customize Customizer reference.
*/
function theme_name_theme_customize_register( $wp_customize ) {
$wp_customize->add_section(
'theme_name_contact',
array(
'title' => __( 'Contact and social media settings', 'theme_name' ),
'priority' => 10,
)
);
$opts = theme_name_get_social_opts();
for ( $i = 0; $i < count( $opts ); $i++ ) {
$wp_customize->add_setting(
'theme_name_' . $opts[ $i ]['name'],
array(
'default' => null,
)
);
$wp_customize->add_control(
'theme_name_' . $opts[ $i ]['name'],
array(
'label' => sprintf( '%s ' . __( 'link', 'theme_name' ), $opts[ $i ]['name'] ),
'section' => 'theme_name_contact',
'type' => 'url',
)
);
}
}
}
add_action( 'customize_register', 'theme_name_theme_customize_register' );
<?php
if ( ! function_exists( 'theme_name_get_social_opts' ) ) {
/**
* Add to the array to add another social link
*/
function theme_name_get_social_opts() {
return array(
array(
'name' => 'instagram',
'icon-classes' => 'fa fa-instagram',
),
array(
'name' => 'twitter',
'icon-classes' => 'fa fa-twitter',
),
array(
'name' => 'facebook',
'icon-classes' => 'fa fa-facebook',
),
array(
'name' => 'youtube',
'icon-classes' => 'fa fa-youtube-play',
),
);
}
}
if ( ! function_exists( 'theme_name_get_social_links' ) ) {
/**
* Return the social links that are defined in the customizer.
*/
function theme_name_get_social_links() {
$available = theme_name_get_social_opts();
$return = array();
for ( $i = 0; $i < count( $available ); $i++ ) {
$user_input = get_theme_mod( 'theme_name_' . $available[ $i ]['name'] );
if ( strlen( $user_input ) > 0 ) {
$entry = [
'icon-classes' => $available[ $i ]['icon-classes'],
'url' => $user_input,
];
array_push( $return, $entry );
}
}
return $return;
}
}
<div class="footer-row-2 w-100 mx-md-5 mt-3 d-flex justify-content-center">
<?php
$socials = theme_name_get_social_links();
?>
<div class="footer-icons my-4 d-flex">
<?php
for ( $i = 0; $i < count( $socials ); $i++ ) : ?>
<a href="<?php echo esc_url( $socials[ $i ]['url'] ); ?>" class="bg-white mx-3 text-center d-block">
<i class="py-2 fa-2x <?php echo esc_attr( $socials[ $i ]['icon-classes'] ); ?>"></i>
</a>
<?php endfor; ?>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment