Lưu ý rằng nếu chúng ta muốn sử dụng các lớp từ Simple Autoloader for WordPress 1 (ok)

Ví dụ 2:

C:\xampp\htdocs\test\wp-content\themes\html5blank\functions.php

require_once 'vendor/autoload.php';
spl_autoload_register( function($classname) {
    // Loads any class placed within the classes folder, and namespaced correctly
    $class      = str_replace( '\\', DIRECTORY_SEPARATOR, str_replace( '_', '-', strtolower($classname) ) );
    $classes    = dirname(__FILE__) .  DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
    // Loads classes from the vendor/makeitworkpress folder using autoloading
    $vendor     = str_replace( 'makeitworkpress' . DIRECTORY_SEPARATOR, '', $class );
    $vendor     = 'makeitworkpress' . DIRECTORY_SEPARATOR . preg_replace( '/\//', '/src/', $vendor, 1 ); // Replace the first slash for the src folder
    $vendors    = dirname(__FILE__) .  DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $vendor . '.php';
    // Check if the files exist
    if( file_exists($classes) ) {
        require_once( $classes );
    } elseif( file_exists($vendors) ) {
        require_once( $vendors );    
    }
} );
$fields = MakeitWorkPress\WP_Custom_Fields\Framework::instance();
$fields->add('options', array(
  'class'         => 'tabs-left',
  'id'            => 'wp_custom_fields_options',
  'title'         => __('Theme Options', 'textdomain'),
  'capability'    => 'manage_options',
  'menu_title'    => __('Theme Options', 'textdomain'),
  'menu_icon'     => 'dashicons-admin-generic',
  'menu_position' => 99,
  'location'      => 'menu',
  'sections'      => array(
    array(
      'id'     => 'section_one',
      'title'  => __('Section One', 'textdomain'),
      'icon'   => 'camera_enhance',
      'fields' => array(
        array(
          'columns'     => 'half',
          'id'          => 'checkbox',
          'title'       => __('Example Title', 'textdomain'),
          'description' => __('Example Description', 'textdomain'),
          'type'        => 'checkbox',
          'style'       => 'switcher',
          'options'     => array(
            array('id' => 'thing_one', 'label' => __('Label One', 'textdomain')),
            array('id' => 'thing_two', 'label' => __('Label Two', 'textdomain')),
          ),
        ),
      ),
    ),
  ),
));

Rồi copy theo cấu trúc này :)

Xem hình ảnh ở đây: https://makeitwork.press/scripts/wp-custom-fields/

C:\xampp\htdocs\test\wp-content\themes\html5blank\vendor\makeitworkpress\wp-custom-fields\framework.php

Kết quả là đây :)

wp-autoload

Simple Autoloader for WordPress. WP Autoload is maintained by Make it WorkPress.

Introduction

This is a php script that makes your PHP classes load automatically when they are initiated. Thus, you do not need to require them manually.

Usage of the autoloader script is pretty simple.

  • Paste this file in the root of your WordPress plugin or theme.

  • Namespace PHP classes according to their folder structure.

  • Classes are now automatically required when they are initiated.

  • Use one class per file!

Using WP Autoload

Now, let's say you are building a new WordPress theme. Within this theme, you are using a specific folder named classes to store all your classes. For this example, the theme has two classes which are placed in the following folders and a functions.php in root.

  • functions.php

  • classes\theme.php

  • classes\models\model.php

In the functions.php, we include the autoloader and call our classes:

spl_autoload_register( function($classname) {
    
    $class      = str_replace( '\\', DIRECTORY_SEPARATOR, str_replace( '_', '-', strtolower($classname) ) );
    $classes    = dirname(__FILE__) .  DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php'; 
    
    if( file_exists($classes) ) {
        require_once( $classes );
    }    
} );

$theme = new Theme();
$model = new Models\Model;

Because we have indicated the 'classes' folder in our autoloader, it will automatically require classes from within the classes folder and we don't have to require them manually.

The theme class is not namespaced, as it is in the root of our classes folder and the autoloader starts loading from that. It my look like this (for starters):

<?php
class Theme {
  public function __construct() {
  }
}

The model class is namespaced, because it is in a subfolder. The model.php file may look like this:

<?php
namespace Models;
use WP_Query as WP_Query;

class Model {
  public function __construct() {
  }
}

Please also note that if we want to use classes from WordPress or other applications, we have to indicate them at the top of the document using the use clausule as in the example of model.php.

Using WP Autoload with Composer

We may add components to our WordPress theme or plugin using composer. Simply said, composer is a kind of package maneger which you can use to register and add new modules to your code.

For example, we may want to include the code from a public repository in one of the themes we are developing. For example, the repository we build for adding custom fields.

Now we can do that by placing a composer.json file in the root of our theme. The file may look like this:

{
    "name": "makeitworkpress/waterfall",
    "description": "Waterfall is a theme with extensive customizer options and very easy to modify or extend for developers.",
    "repositories": [              
        {
            "type": "vcs",
            "url": "https://github.com/makeitworkpress/wp-custom-fields.git"
        }       
    ],
    "require": {
        "php": ">=7.0",
        "makeitworkpress/wp-custom-fields": "dev-master",
    },
    "homepage": "https://github.com/makeitworkpress/our-new-theme/",
    "authors": [ 
        {
            "name": "Make it WorkPress",
            "email": "hello@makeitworkpress.com",
            "homepage": "https://www.makeitworkpress.com"

        } 
    ]          
}

If we now go our terminal to the root folder of the theme we're developing. we can use composer update --prefer-dist to include this repository. The --prefer-dist will ignore all the GIT related files for the given repositories and prefer a distribution version. Unless you want to contribute to the repository, this is the preferred way to include extra modules. By default, composer places all the files within a vendor folder.

Now in our functions.php, we may add the following autloader:

spl_autoload_register( function($classname) {
    
    $class      = str_replace( '\\', DIRECTORY_SEPARATOR, str_replace( '_', '-', strtolower($classname) ) );
    $classes    = dirname(__FILE__) .  DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
    
    $vendor     = str_replace( 'makeitworkpress' . DIRECTORY_SEPARATOR, '', $class );
    $vendor     = 'makeitworkpress' . DIRECTORY_SEPARATOR . preg_replace( '/\//', '/src/', $vendor, 1 ); // Replace the first slash for the src folder
    $vendors    = dirname(__FILE__) .  DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $vendor . '.php';

    if( file_exists($classes) ) {
        require_once( $classes );
    } elseif( file_exists($vendors) ) {
        require_once( $vendors );    
    }
   
} );

This will both autoload any class called from the classes folder, if properly namespaced and also all the modules added by composer. Because we're using components from makeitworkpress, these are placed within the vendor/makeitworkpress folder.

Please note that composer also adds their own autoloading functions in the respective vendor folders. You also may require these and use them to autoload components.

Last updated