0

I'm using Tareq's WordPress Settings API to develop a plugin with a 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?

0

Figured it out as per this answer.

You cannot put any loop or control structure if/else as array value or anywhere within array().

Example

$contact_list = [];
$contact_list['url'] = 'Website';
foreach ( wp_get_user_contact_methods() as $value => $label ) { 
    $contact_list[$value] = $label;
}
$settings_fields = array( // Parent array
    'dsbl_basics' => array( // Child array
        array( // Child's child array
            'name' => 'text_val',
            'label' => 'Text Input'
        )
    ),
    'dsbl_profile' => array( // Child array
        array( // Child's child array
            'name' => 'name',
            'label' => 'dsbl',
            'options' => array(
                'first_name' => 'First Name',
                'last_name' => 'Last Name'
            )
        ),
        array( // Child's child array
            'name' => 'contact_info',
            'label' => 'Contact Info', 
            'options' => $contact_list
        )
    )
);
$yoast_plugin = array( // Child's child array
    'name' => 'yoast_seo',
    'label' => 'Yoast SEO', 
    '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'][1] = $yoast_plugin;
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.