1

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?

5
  • 2
    .... Don't put foreach and if clauses inside of your array, that's not how it works. Commented Aug 30, 2016 at 11:48
  • @Epodax Instead of telling me what I cannot do, may you explain what else I could do to achieve the same results? Commented Aug 30, 2016 at 11:50
  • 1
    Move your foreach and if clause out of your array, assign the values from your foreach / if clause to variables and use those variables in your large array. - Please don't just throw code together and expect us to fix it. Commented Aug 30, 2016 at 11:52
  • 1
    @Epodax As you can see, I am not pasting my code and asking to do it for me. I am asking for the right approach to it since and I provide the code on what I am doing. Please, be more considerate when someone is asking for advice. Commented Aug 30, 2016 at 11:59
  • The right approach is to try and fix it yourself and when you have tried to fix it you come to SO and ask for help, in your question you show us what you have tried in order to fix the issue and why / what isn't working with the attempt you've made. Commented Aug 30, 2016 at 12:03

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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'
        )
    );
}

1 Comment

I added numbers, as that's what's PHP it self is doing when there are no keys, just values. Empty keys are actually numerical keys, hence we don't need to type them, nonetheless, php will add and parse them automatically. If You want to become good at what You do (php in this case) here > php.net/manual/en/index.php Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.