Skip to main content
fixed the indenting
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147
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
                wp_insert_post(
                    array(
                        '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() {
     
    }
}
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
            wp_insert_post(
                array(
                    '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() {
     
    }
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
                wp_insert_post(
                    array(
                        '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() {
     
    }
}
Source Link
Himanshu
  • 53
  • 1
  • 12

Working of foreach loop with array

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
            wp_insert_post(
                array(
                    '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() {
     
    }