• Resolved saxpaolo

    (@saxpaolo)


    Hello,
    I’ve got an option page (with option_key => ‘social_options’) that include a select field inside a repeated group ($social_group); btw, the group also include a social_media_url text_url field:

    This is the select:

    $social_options->add_group_field($social_group, array(
        'id' => $prefix . 'social_media_select',
        'name' => esc_html__('Choose social media', 'text_domain'),
        'description' => esc_html__('Choose the social media include', 'text_domain'),
        'type' => 'select',
        'show_option_none' => true,
        'default' => 'custom',
        'options_cb' => 'socialmedia_options'
    ));

    It callbacks a function containing the list of social media:

    function socialmedia_options()
    {
        return array(
            'facebook' => __('Facebook', 'text_domain'),
            ...
        );
    }

    In my template I’ve got the following:

    $social_data = get_option( social_options', array() );
    
    if ( ! empty( $social_data['social_network'] ) ) {
        echo "<ul>";
        foreach ( $social_data['social_network'] as $social ) {
            $social_list_item = '<li><a href="'; 
            $social_list_item .= $social['social_media_url']; 
            $social_list_item .= '" title="'; 
            $social_list_item .= $social['social_media_select']; 
            $social_list_item .= '" target="_blank"><i class="fab fa-'; 
            $social_list_item .= $social['social_media_select']; 
            $social_list_item .= '"></i></a></li>'; 
            echo $social_list_item; 
        }
        echo "</ul>"; 
    }

    This is what I get:

    <ul>
        <li><a href="#" title="facebook" target="_blank"><i class="fab fa-facebook"></i></a></li>
        ...
    </ul>

    What I’d like to get as well is the value of the socialmedia_options() callback function, in order to get the names of the social media, as follow:

    <ul>
        <li><a href="#" title="facebook" target="_blank"><i class="fab fa-facebook"></i> Facebook</a></li>
        ...
    </ul>

    Any help is more than welcome
    Cheers
    Paolo

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Since it looks like you already have the social media name available, since you output it as part of the title and the fontawesome classes, I’d say just concatenate $social['social_media_select'] once more in the right spot, with the added touch of passing it through the ucfirst php function. That’ll uppercase the first character for you.

    foreach ( $social_data['social_network'] as $social ) {
    	$social_list_item = '<li><a href="'; 
    	$social_list_item .= $social['social_media_url']; 
    	$social_list_item .= '" title="'; 
    	$social_list_item .= $social['social_media_select']; 
    	$social_list_item .= '" target="_blank"><i class="fab fa-'; 
    	$social_list_item .= $social['social_media_select']; 
    	$social_list_item .= '"></i>';
    	$social_list_item .= ucfirst( $social['social_media_select'] );
    	$social_list_item .= '</a></li>'; 
            echo $social_list_item; 
    }
    
    Thread Starter saxpaolo

    (@saxpaolo)

    Hello Michael, thanks for your quick reply – and thanks for CMB2 plugin too! ??

    I already thought about that …the fact is that the names used by Fontawesome not always match the actual name of the social. E.g.:

    “linkedin-in” for LinkedIn
    “snapchat-ghost” for Snapchat
    “stumbleupon-circle” for StumbleUpon
    etc.

    So I thought that the use of the “key=>value” pair could be a nice solution (I mean, it’s not a vital issue for the plugin I’m working on right now, but this situation could occur again in other cases and for more critical data, so I was courious if there is an solution or a better approach…)

    Thanks again…

    • This reply was modified 5 years, 9 months ago by saxpaolo.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    using your pseudo code, you’d do something like this:

    
    $social_options->add_group_field($social_group, array(
        'id' => $prefix . 'social_media_select',
        'name' => esc_html__('Choose social media', 'text_domain'),
        'description' => esc_html__('Choose the social media include', 'text_domain'),
        'type' => 'select',
        'show_option_none' => true,
        'default' => 'custom',
        'options_cb' => 'socialmedia_options'
    ));
    // It callbacks a function containing the list of social media:
    
    function socialmedia_options()
    {
        return array(
            'facebook' => __('Facebook', 'text_domain'),
            ...
        );
    }
    // In my template I’ve got the following:
    
    $social_data = get_option( 'social_options', array() );
    
    if ( ! empty( $social_data['social_network'] ) ) {
        $all_options = socialmedia_options();
        echo "<ul>";
        foreach ( $social_data['social_network'] as $social ) {
            $social_list_item = '<li><a href="'; 
            $social_list_item .= $social['social_media_url']; 
            $social_list_item .= '" title="'; 
            $social_list_item .= $social['social_media_select']; 
            $social_list_item .= '" target="_blank"><i class="fab fa-'; 
            $social_list_item .= $social['social_media_select']; 
            $social_list_item .= '"></i> ';
            $social_list_item .= $all_options[ $social['social_media_select'] ];
            $social_list_item .= '</a></li>'; 
    
            echo $social_list_item; 
        }
        echo "</ul>"; 
    }
    
    Thread Starter saxpaolo

    (@saxpaolo)

    @jtsternberg …sooooooo easy!!!!! ??

    That’s just perfect!

    Thanks for your support

    Paolo

    Plugin Author Justin Sternberg

    (@jtsternberg)

    ????

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to retrieve both keys and values from an callback function array used in a s’ is closed to new replies.