De Belser Arne Profielfoto

Arne De Belser

How encapsulation can be used in WordPress development to make code more modular and easier to maintain

Prelude

Let’s say you want to create a custom widget that displays a list of recent posts. To do this create a class called Recent_Posts_Widget which extends the WP_Widget class. This class now encapsulates the functionality of the widget in that class. Let me show you:

				
					<?php

class Recent_Posts_Widget extends WP_Widget {
    public function __construct() {
        $widget_ops = array(
            'classname' => 'recent-posts-widget',
            'description' => 'Displays a list of recent posts.'
        );

        parent::__construct('recent_posts_widget', 'Recent Posts', $widget_ops);
    }
    
    public function widget($args, $instance) {
        $query_args = array(
            'post_type' => 'post',
            'posts_per_page' => $instance['number_of_posts']
        );

        $query = new WP_Query($query_args);
        
        // render the widget using the $query object and $args array
    }
    
    public function form($instance) {
        $number_of_posts = ! empty( $instance['number_of_posts'] ) ? $instance['number_of_posts'] : 5;

        ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>">
                    <?php _e( 'Number of posts to show:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'number_of_posts' ); ?>" name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>" type="number" value="<?php echo esc_attr( $number_of_posts ); ?>">
            </p>
        <?php 
    }
    
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['number_of_posts'] = ! empty( $new_instance['number_of_posts'] ) ? intval( $new_instance['number_of_posts'] ) : 5;
        
        return $instance;
    }
}

function register_recent_posts_widget() {
    register_widget('Recent_Posts_Widget');
}
add_action('widgets_init', 'register_recent_posts_widget');
				
			

In this example, the Recent_Posts_Widget class encapsulates the functionality of the widget, including the query arguments that are used to fetch the list of recent posts. By using a class to encapsulate this functionality, you can make the widget more modular and easier to reuse across your website. This is because we now own the code, we make the decisions as to which query args are used to fetch the posts.

Show me some magic…

For example, you could create a variation of the Recent_Posts_Widget class that displays a list of recent pages instead of posts, simply by changing the post type when you instantiate the class:
				
					<?php

class Recent_Pages_Widget extends Recent_Posts_Widget {
    
    public function __construct() {
        $widget_ops = array(
            'classname' => 'recent-pages-widget',
            'description' => 'Displays a list of recent pages.'
        );

        parent::__construct();

        $this->widget_options['classname'] = $widget_ops['classname'];
        $this->widget_options['description'] = $widget_ops['description'];
    }
    
    public function widget($args, $instance) {
        $query_args = array(
            'post_type' => 'page', // -> We swapped this out for 'page' which will now query posts with 'post_type', 'page' instead of 'post'
            'posts_per_page' => $instance['number_of_posts']
        );
        
        $query = new WP_Query($query_args);
        
        // render the widget using the $query object and $args array
    }
}

function register_recent_pages_widget() {
    register_widget('Recent_Pages_Widget');
}
add_action('widgets_init', 'register_recent_pages_widget');

				
			
In this example, the Recent_Pages_Widget class extends our own Recent_Posts_Widget class and overrides the widget method to display a list of recent pages instead of posts. By using inheritance, you can avoid duplicating code and only overwrite the things you need. This makes for writing nice and cleaner code.

Do you have any questions? Please feel free to leave a comment below, and I will do my best to respond as quickly as possible.

If you’re interested in more tutorials or engaging content, be sure to check out my YouTube channel

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x