Ethan Jinks O'Sullivan Ethan Jinks O'Sullivan - 3 months ago 21
PHP Question

How to use foreach and if statement within array of arrays?

I'm developing a WordPress plugin and using Tareq's WordPress Settings API to develop my settings page.

With the API, I am displaying checkbox items. However, I want to show additional checkboxes using the

foreach
and
if
statement.


  • foreach
    : this will display additional checkboxes for every field in the
    wp_get_user_contact_methods()

  • if
    : this will display an extra set of checkboxes if another plugin is activated



This is what I have right now going off of my own logic:

$settings_fields = array( // Parent array
'dsbl_basics' => array( // Child array
array( // Child's child array
'name' => 'text_val',
'label' => __( 'Text Input', 'dsbl' ),
'type' => 'text',
'default' => 'Title',
'sanitize_callback' => 'intval'
)
),
'dsbl_profile' => array( // Child array
array( // Child's child array
'name' => 'name',
'label' => __( 'Name', 'dsbl' ),
'type' => 'multicheck',
'options' => array(
'first_name' => 'First Name',
'last_name' => 'Last Name'
)
),
array( // Child's child array
'name' => 'contact_info',
'label' => __( 'Contact Info', 'dsbl' ),
'type' => 'multicheck',
'options' => array(
'url' => 'Website',
foreach ( wp_get_user_contact_methods() as $value => $label ) { // Additional contact fields
$value => $label
}
)
),
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) { // If plugin exists
array( // Child's child array
'name' => 'yoast_seo',
'label' => __( 'Yoast SEO', 'dsbl' ),
'type' => 'multicheck',
'options' => array(
'wpseo_author_title' => 'Title to use for Author page',
'wpseo_author_metadesc' => 'Meta description to use for Author page'
)
),
}
)
);


I know that my syntax is off which is giving me these errors:


Parse error: syntax error, unexpected
foreach
(T_FOREACH), expecting
)


Parse error: syntax error, unexpected
if
(T_IF), expecting
)



What is the proper approach to this?

Answer
$contactArray = [];
foreach ( wp_get_user_contact_methods() as $value => $label ) { 
    $contactArray[$value] = $label;
}
$contactArray['url'] = 'Website';

$settings_fields = array( // Parent array
    'dsbl_basics' => array( // Child array
        array( // Child's child array
            'name' => 'text_val',
            'label' =>  'Text Input',
            'type' => 'text',
            'default' => 'Title',
            'sanitize_callback' => 'intval'
        )
    ),
    'dsbl_profile' => array( // Child array
        array( // Child's child array
            'name' => 'name',
            'label' => 'dsbl',
            'type' => 'multicheck',
            'options' => array(
                'first_name' => 'First Name',
                'last_name' => 'Last Name'
            )
        ),
        array( // Child's child array
            'name' => 'contact_info',
            'label' => 'Contact Info', 
            'type' => 'multicheck',
            'options' => $contactArray
        )
    )
);

$yaostSEO = array( // Child's child array
    'name' => 'yoast_seo',
    'label' => 'Yoast SEO', 
    'type' => 'multicheck',
    'options' => array(
        'wpseo_author_title' => 'Title to use for Author page',
        'wpseo_author_metadesc' => 'Meta description to use for Author page'
    )
);

if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) {
    $settings_fields['dsbl_profile'][] = $yaostSEO;
}
Comments