spl_autoload_register (ok)

https://www.php.net/manual/en/function.spl-autoload-register.php

Đăng ký một chức năng để được tự động tải.

spl_autoload_register( callable $autoload_function, bool $throw = true, bool $prepend = false )
$autoload_function :The function to register.
$throw: Liệu hàm có nên ném ngoại lệ hay không nếu hàm không thể gọi được.
$prepend: Liệu các chức năng nên được chuẩn bị cho ngăn xếp.

Example:

function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');

Cách sử dụng spl_autoload_register cho wordpress đã thực thiện

C:\xampp\htdocs\sputnick\wp-content\themes\b_theme\functions.php

function my_autoloader($class) {
  if(file_exists(__DIR__ .'\\classes\\'. $class . '.php')) {
    include_once __DIR__ .'\\classes\\'. $class . '.php';
  }
}
spl_autoload_register('my_autoloader');

C:\xampp\htdocs\sputnick\wp-content\themes\b_theme\classes\home_xn.php

<?php
class home_xn extends WP_Widget {
  function __construct() {
    parent::__construct('home_xn', 'Xem nhiều', array('description' => ''));
  }
  function widget($args, $instance) {
    extract($args);
    $html = "";
    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
    $sp    = apply_filters('widget_text', $instance['sp'], $instance);
    $html .= '<div class="sidebar-title title_sec"><a href="#"><?php echo ($title); ?></a></div>';
    $html .= '<div class="widget-content">';
      $new = new WP_Query('showposts=5&meta_key=post_views_count&orderby=meta_value_num&order=DESC');
      while ($new->have_posts()): $new->the_post();
  	      $html .= '<div class="single-post">';
  	      $html .= '<div class="row">';
  	        $html .= '<div class="col-md-4">';
  	          $html .= '<div class="img">';
  	            $html .= '<a href="<?php the_permalink();?>">'.the_post_thumbnail().'</a>';
  	          $html .= '</div>';
  	        $html .= '</div>';
  	        $html .= '<div class="col-md-8">';
  	          $html .= '<h3 class="title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>';
  	          $html .= '<small class="text-muted"><i class="fa fa-clock-o"></i> <?php the_time('d/m/Y')?></small>';
  	        $html .= '</div>';
  	      $html .= '</div>';
  	    $html .= '</div>';
  	  endwhile;
    $html .= '</div>';
    echo $html;
  }
  function update($new_instance, $old_instance) {
    $instance          = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['sp']    = $new_instance['sp'];
    return $instance;
  }
  function form($instance) {
    $instance = wp_parse_args((array) $instance, array('title' => '', 'sp' => ''));
    $title    = strip_tags($instance['title']);
    $sp       = ($instance['sp']);
    ?>
  <p>
    <label for="<?php echo $this->get_field_id('title'); ?>">
    <?php _e('Tiêu đề :');?> </label>
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title ?>" />
  </p>
<?php
}
}
?>
if ( ! function_exists( 'widgets_init_custom' ) ) :
function widgets_init_custom() {
    register_widget('home_xn');
}
endif;
add_action( 'widgets_init', 'widgets_init_custom');

Last updated