Skip to content


This section will introduce you to the WordpressService interface and its helper abstract class AbstractWordpressService.

All services that implement WordpressService interface will be automatically initialized by your theme's functions.php.


Create a file /src/Wordpress/Sidebars.php. This class will look something like this:


namespace App\Wordpress;

use Sword\SwordBundle\Service\AbstractWordpressService;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final class Sidebars extends AbstractWordpressService
    public function __construct(
        private readonly string $translationDomain,
    ) {

    public function initialize(): void
        add_action('widgets_init', [$this, 'registerSidebars']);

    public function registerSidebars(): void
            'name' => __('My new sidebar', $this->translationDomain),
            'id' => 'my-new-sidebar',
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget'  => '</aside>',
            'before_title'  => '<span class="widget-title">',
            'after_title'   => '</span><div class="is-divider small"></div>',

Now when you navigate to /wp-admin/widgets.php you get your new sidebar:

WordPress sidebar

Dependency injection

Classes implementing WordpressService can take advantage of Symfony's dependency injection.


Symfony services are initialized before WordPress is loaded, therefore you can't use the constructor to call WordPress hooks. Instead, call them in the initialize() method, which is called while WordPress is loading, in your child theme's functions.php.


WordpressService interface also allows you to define a loading priority.

To make a service to load with a high priority, implement getPriority() method:

final class Sidebars extends AbstractWordpressService
    // ...

    public function getPriority(): int
        return 100;


The default priority used by AbstractWordpressService is 0. Higher number means higher priority.

Released under the MIT License.