Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Website CMS front-end code reading

The focus of this article is to show and interpret the process of WordPress from starting (that is, entering from the index.php entry) to displaying templates and content on web pages.This process can be divided into three stages, one isinitialization phase, that is, initialize constants, environment, load core files, etc.; the second isContent processing stage, that is, according to the user's request, call related functions to obtain and process data, and prepare data for front-end display;Theme application stage, At this stage, the data to be displayed has been prepared, and the corresponding theme template needs to be loaded according to the user's request, that is, the theme is routed.After these three stages, the page requested by the user can be fully displayed~_~
note: In order to make the thinking clearer and easier to understand, temporarily ignore the files related to background management, and files related to multiple blogs/sites!

initialization phase

Entering from the WordPress entry (in most cases index.php), until the end of this part, we see that WordPress mainly does the following things:Define constants and global variables;Set environment parameters;Make initialization judgment(e.g. whether WordPress is already installed);Load WordPress core files;Handle action and filter related transactions;Create functional objects(such as $wp, $wp_rewrite, etc.); thus this stage can be regarded as the initialization stage of WordPress. The initialization phase of WordPress is a rather cumbersome process, as detailed in the following list:

    1. Define the WP_USE_THEMES constant, when the constant is defined as false, that is, the theme is not used, the site will be displayed as blank; when it is true, it will be displayed normally.
    2. Set the $wp_did_header variable, which is equivalent to a flag to ensure that the wp-blog-header.php file is executed only once per refresh.
    3. Set the absolute path ABSPATH to the WordPress directory.
    4. Set the error reporting mode.
    5. Set configurable items in the wp-config.php file.
    6. Define WPINC constants, version-related variables, and assign values ​​to these variables.
    7. Call the wp_initial_constants() function to initialize the constants WP_MEMORY_LIMIT, WP_DEBUG, WP_CONTENT_DIR and WP_CACHE.
    8. Turn off runtime magic references, which will be added later via wpdb in wp-settings.php.
    9. Sets the default timezone for PHP5.
    10. Call the wp_unregister_GLOBALS() function to close register_globals.
    11. Unset global variables $wp_filter, $cache_lastcommentmodified, $cache_lastpostdate to make sure they don't exist.
    12. Call the function wp_fix_server_vars() to specify the $_SERVER variable settings.
    13. Call the function wp_check_php_mysql_versions() to check the required PHP version and MySQL extension or database drop-in.
    14. Call the function wp_favicon_request() to check if a request is received due to a missing favicon.ico.
    15. Check if you are in maintenance mode.
    16. Start the loading timer.
    17. Check if you are in WP_DEBUG mode.
    18. Whether and how to load the file is determined according to the WP_CACHE and WP_DEBUG constants:
      1. WP_CONTENT_DIR/advanced-cache.php belongs to drop-in and is used by advanced cache plugins.
    19. Call the wp_set_lang_dir() function to set the constant WP_LANG_DIR.
    20. Call the require_wp_db() function, import the wpdb class, or drop-in the database into db.php (if it exists).
    21. Call the wp_set_wpdb_vars() function to set the database table prefix and format specifiers for the database table columns
    22. Call the wp_start_object_cache() function to enable the WP object cache, or expand the object cache (if the corresponding drop-in exists).
    23. Load WP file:
      1. WPINC/default-filters.php, set default actions and filters for hooks in WP;
    24. If multisite is enabled, load the file; otherwise set the constant MULTISITE to false:
      1. WPINC/ms-blogs.php, defines a set of Site/blog functions for manipulating the blogs table and related data;
      2. WPINC/ms-settings.php (files involving multiple sites are not considered for now).

——If only basic functions are required (ie, the SHORTINIT constant is true), the wp-setting.php file will return to this point! ————

  1. Call the wp_not_installed() function to start the installer if WP has not been installed.
  2. Load the *most of WP* of WP:
    1. WPINC/capabilities.php, defines a set of classes WP_Roles, WP_Role, WP_User and functions for manipulating roles and permissions;
    2. WPINC/post.php, define a set of post-related functions, and add add_action( 'init', 'create_initial_post_types', 0 );
    3. WPINC/rewrite.php, defines a set of WP rewrite API and WP_Rewrite class, used to rewrite the link format; and this file also defines a set of constants EP_NONE, EP_PERMALINK, EP_ATTACHMENT, EP_DATE, EP_YEAR, EP_MONTH, EP_DAY, EP_ROOT , EP_COMMENTS, EP_SEARCH, EP_CATEGORIES, EP_TAGS, EP_AUTHORS, EP_PAGES, EP_ALL;
    4. WPINC/kses.php, defines a set of HTML/XHTML filter API, and defines a set of tag arrays, adds two actions: add_action('init', 'kses_init');add_action('set_current_user', 'kses_init' );
    5. WPINC/script-loader.php, the default loader API for scripts and stylesheets in WP, and loads the following files:
      • /class.wp-styles.php, defines the class WP_Styles, which inherits from WP_Dependencies and is used for backpressure style sheet queue;

      And added the following action and filter:

      • add_action( 'wp_default_scripts', 'wp_default_scripts' );
      • add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
      • add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
      • add_action( 'wp_default_styles', 'wp_default_styles' );
      • add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 );
    6. WPINC/update.php, defines a set of APIs for checking version upgrade information, adds a set of actions, and dispatches events:
      • add_action( 'admin_init', '_maybe_update_core' );
      • add_action( 'wp_version_check', 'wp_version_check' );
      • add_action( 'load-plugins.php', 'wp_update_plugins' );
      • add_action( 'load-update.php', 'wp_update_plugins' );
      • add_action( 'load-update-core.php', 'wp_update_plugins' );
      • add_action( 'admin_init', '_maybe_update_plugins' );
      • add_action( 'wp_update_plugins', 'wp_update_plugins' );
      • add_action( 'load-themes.php', 'wp_update_themes' );
      • add_action( 'load-update.php', 'wp_update_themes' );
      • add_action( 'load-update-core.php', 'wp_update_themes' );
      • add_action( 'admin_init', '_maybe_update_themes' );
      • add_action( 'wp_update_themes', 'wp_update_themes' );
    7. WPINC/canonical.php, defines a set of redirection canonical functions for handling redirection in WP, and adds action:
      • add_action('template_redirect', 'redirect_canonical');
    8. WPINC/shortcodes.php, defines a set of WP shortcode API, and defines the array $shortcode_tags, add filter:
      • add_filter('the_content', 'do_shortcode', 11);
    9. WPINC/media.php, define a set of media (video, picture, etc.) display related API and class WP_Embed, and create $wp_embed = new WP_Embed() variable, add shortcode:
      • add_shortcode('wp_caption', 'img_caption_shortcode');
      • add_shortcode('caption', 'img_caption_shortcode');
      • add_shortcode('gallery', 'gallery_shortcode');
    10. WPINC/widgets.php, defines a set of APIs and classes for creating dynamic sidebars, WP_Widget, WP_Widget_Factory, and defines a set of global variables;
  3. Load multisite specific files:
    1. WPINC/ms-functions.php,
    2. WPINC/ms-default-filters.php,
    3. WPINC/ms-deprecated.php.
  4. Call the wp_plugin_directory_constants() function, define constants that depend on the API to get default values, and define the plugin folder constants that must be used, which may be overridden in the sunset.php drop-in.
  5. Use the function wp_get_mu_plugins() to load the plugins that must be used, and trigger the mount point do_action( 'muplugins_loaded' );
  6. Call the function ms_cookie_constants() to set cookie constants for multisite.
  7. Call the function wp_cookie_constants() to define constants after multisite loading, cookie related constants may be overwritten in ms_network_cookies().
  8. Call the function wp_ssl_constants() to define and implement SSL constants.
  9. Load the file to create public global variables:
    1. WPINC/vars.php to create public variables for the rest of WP.
  10. Call the functions create_initial_taxonomies() and create_initial_post_types() to make taxonomies and posts visible to plugins and themes.Note to plugin authors: these are registered again when the hook is initialized.
  11. Active plugins are loaded using the function wp_get_active_and_valid_plugins().
  12. Call the function wp_set_internal_encoding() to set the internal encoding.
  13. This function is called if WP_CACHE is on and the wp_cache_postload() function exists.
  14. Trigger mount point do_action( 'plugins_loaded' ).
  15. Call the function wp_functionality_constants() to define constants that are not yet defined but affect functionality.
  16. Call the function wp_magic_quotes() to add magic quotes and assemble $_REQUEST ( $_GET + $_POST ).
  17. Trigger mount point do_action( 'sanitize_comment_cookies' ) (harmless comment cookies).
  18. Create a global WP query object $wp_the_query =& new WP_Query(), and save the reference of $wp_the_query to $wp_query, and use $wp_query to query.
  19. Create a WP rewrite object $wp_rewrite =& new WP_Rewrite() to create nice URLs.
  20. Create WP object $wp =& new WP().
  21. Create a WP Widget factory object $wp_widget_factory =& new WP_Widget_Factory().
  22. Trigger mount point do_action( 'setup_theme' ).
  23. Call the function wp_templating_constants() to set template related constants.
  24. Call the function load_default_textdomain() to load the default text localization domain.
  25. Find blog locale $locale = get_locale().
  26. Create localization object $wp_locale =& new WP_Locale() to load localization domain data and various strings.
  27. Load the library file of the active theme:
    1. TEMPLATEPATH/functions.php, loads the functions that come with the template.
  28. Trigger mount point do_action( 'after_setup_theme' ).
  29. Register the shutdown function register_shutdown_function( 'shutdown_action_hook' ).
  30. Create the current user $wp->init().
  31. The mount point do_action( 'init' ) is triggered, at this stage most of the WP is loaded and the user is authenticated. WP will continue to load mounters on init hooks, such as widgets and various plugin instances.If you want to insert an action after WP loads, use the wp_loaded hook below.
  32. If it is multi-site, check the status of the site.
  33. Trigger the mount point do_action('wp_loaded'), when WP, all plugins and themes are fully loaded and instantiated, the hook will be dismissed. Ajax requests should use wp-admin/admin-ajax.php, admin-ajax.php can handle requests from users who are not logged in.

Content processing stage

At this stage, the wp() function is called to query the database content, and the query content is assigned to some global variables, so that it is convenient to use the template tag in the template to obtain the corresponding data and display it on the front end.

  1. Call the wp() function.
  2. Call the $wp->main() function.
  3. Initialize, call the wp_get_current_user() function (in pluggable.php) to get the current user information, that is, set the global variable $current_user.
  4. Call the parse_request() function to parse the query parameters.
  5. Call the send_headers() function to send additional http headers.
  6. Call the query_posts() function to perform query operations.
  7. Call the handle_404() function to handle 404 errors.
  8. Call the register_globals() function to register global variables.
  9. Call the do_action_ref_array() function (in the wp-includes/plugin.php file) and set this object as the parameter of the function on the wp hook.

Theme application stage

The routing and loading of the WordPress theme is mainly carried out in the WPINC/template-loader.php file, which is equivalent to a template loading router, which loads the corresponding template according to the url.

  1. If the WP_USE_THEMES constant is true, trigger the mount point do_action('template_redirect');
  2. Process feeds and trackbacks according to the results of the judgment functions is_robots(), is_feed() and is_trackback(), even if no theme is used;
  3. If the WP_USE_THEMES constant is true, call the corresponding function to get the theme according to the result of the following judgment function:
    • is_404()–>get_404_template()
    • is_search()–>get_search_template()
    • is_tax()–>get_taxonomy_template()
    • is_front_page()–>get_front_page_template()
    • is_home()–>get_home_template()
    • is_attachment()–>get_attachment_template()
    • is_single()–>get_single_template()
    • is_page()–>get_page_template()
    • is_category()–>get_category_template()
    • is_tag()–>get_tag_template()
    • is_author()–>get_author_template()
    • is_date()–>get_date_template()
    • is_archive()–>get_archive_template()
    • is_comments_popup()–>get_comments_popup_template()
    • is_paged()–>get_paged_template()
    • Other–>get_index_template()
  4. If applying a filter to a theme succeeds apply_filters( 'template_include', $template ), load the theme.

After the template routing is completed, the called template will be loaded, and the template tag in the template will also access the global variables set in the content preparation stage, obtain the required data, and output the data to the template, and finally complete the style + Content is displayed on the front end!
Source of this article: www.cnblogs.com/vicenteforever/category/224217.html

Back to Top