skip to Main Content

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?

2

Answers


  1. You cannot put foreach statements (nor while, for, do or any loop or control structure if/else) as array value or anywhere within array. Where that idea came from anyway? Something like this should do.

    $settings_fields = array( // Parent array
        'dsbl_basics' => array( // Child array
            0 => array( // Child's child array
                'name' => 'text_val',
                'label' => __( 'Text Input', 'dsbl' ),
                'type' => 'text',
                'default' => 'Title',
                'sanitize_callback' => 'intval'
            )
        ),
        'dsbl_profile' => array( // Child array
            0 => array( // Child's child array
                'name' => 'name',
                'label' => __( 'Name', 'dsbl' ),
                'type' => 'multicheck',
                'options' => array(
                    'first_name' => 'First Name',
                    'last_name' => 'Last Name'
                )
            ),
            1 => array( // Child's child array
                'name' => 'contact_info',
                'label' => __( 'Contact Info', 'dsbl' ),
                'type' => 'multicheck',
                'options' => array(
                    'url' => 'Website',
                    /* this isn't place for loops */
                )
            )
        )
    );
    
    
    if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) 
    {   // If plugin exists
        $settings_fileds['dsbl_profile'][2]  =  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'
            )
        );
    }
    
    Login or Signup to reply.
  2. $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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search