I'm trying to understand a piece of code I found online to create customized user login page. Here I'm unable to understand how foreach loop works (specially this line: $page_definitions as $slug => $page in foreach argument). I'm very new to working with arrays and am trying to learn how it works.

class Personalize_Login_Plugin {

    public static function plugin_activated() {
        // Information needed for creating the plugin's pages
        $page_definitions = array(
            'member-login' => array(
                'title' => __( 'Sign In', 'personalize-login' ),
                'content' => '[custom-login-form]'
            'member-account' => array(
                'title' => __( 'Your Account', 'personalize-login' ),
                'content' => '[account-info]'

        foreach ( $page_definitions as $slug => $page ) {
            // Check that the page doesn't exist already
            $query = new WP_Query( 'pagename=' . $slug );
            if ( ! $query->have_posts() ) {
                // Add the page using the data from the array above
                        'post_content'   => $page['content'],
                        'post_name'      => $slug,
                        'post_title'     => $page['title'],
                        'post_status'    => 'publish',
                        'post_type'      => 'page',
                        'ping_status'    => 'closed',
                        'comment_status' => 'closed',
    public function __construct() {

  • 1
    have you read the documentation for foreach? This is the second syntax noted on that page, with an associative array.
    – Milo
    Commented Apr 14, 2017 at 19:20
  • Hi @Milo , thank you for clarifying. The documentation was very helpful. I learned foreach loop from codecademy where only first syntax was mentioned. That's why I got so confused.
    – Himanshu
    Commented Apr 15, 2017 at 18:06
  • @Himanshu - just wanted to be sure to alert you to corrections to my answer below, which I somehow managed to get wrong the first time, before you accepted it. I feel very stupid about that. The point is that the keys literally are replicated as the new page slugs. I misstated the working of the code the first time as though their values within the arrays would be used (which wouldn't make any sense).
    – CK MacLeod
    Commented Apr 16, 2017 at 15:59
  • Just wondering what this does? 'title' => __( 'Your Account', 'personalize-login' ),
    – Himanshu
    Commented Apr 17, 2017 at 18:35

1 Answer 1


When I first started working with foreach loops, this particular syntax seemed mysterious to me, too, and it wasn't until I needed it that its usefulness became obvious. Works that way for a lot of "things you see in code."

Anyway, to answer the question, in the sample code, using as $key => $value makes the key ($slug) available as a variable in the ensuing code block. The $page_definitions array has two top-level keys, 'member-login' and 'member-account' which (after an already-exists? check) become the slugs for two new pages that the function creates (inserts) with the values of the two-member ('title' and 'content') arrays or sub-arrays as the new pages' title and content: one entitled "Sign In" with content the shortcode [custom-login-form], and one entitled "Your Account" with [account-info] shortcode.

So, if I were on that site, after the function ran, I'd find two pages, one with a slug of siteurl/member-login, entitled Sign In, showing a custom login form, etc., and a second page with a slug siteurl/member-account, with content created by the account-info shortcode. The two shortcodes probably produce differentiated content based on logged-in status and identity of the user.

Got it? [EDITED AND FIXED after I screwed up the answer the first time!]

Your Answer

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

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