Connect with us

WORDPRESS

Making a WordPress plugin extensible with PHP classes

Published

on

Making a WordPress plugin extensible with PHP classes

WordPress plugins can be extended with additional functionality, as demonstrated by popular plugins like WooCommerce and Gravity Forms. In the article “Architecting a WordPress plugin to support extensions,” we learn there are two primary ways to make a WordPress plugin extensible:

  1. By setting up hooks (actions and filters) for extension plugins to inject their own functionality
  2. By providing PHP classes that extension plugins can inherit

The first method relies more on documentation, detailing available hooks and their usage. The second method, by contrast, offers ready-to-use code for extensions, reducing the need for extensive documentation. This is advantageous because creating documentation alongside code can complicate the plugin’s management and release.

Providing PHP classes directly effectively replaces documentation with code. Instead of teaching how to implement a feature, the plugin supplies the necessary PHP code, simplifying the task for third-party developers.

Let’s explore some techniques for achieving this, with the ultimate goal of fostering an ecosystem of integrations around our WordPress plugin.

Defining base PHP classes in the WordPress plugin

The WordPress plugin will include PHP classes intended for use by extension plugins. These PHP classes might not be used by the main plugin itself but are provided specifically for others to use.

Let’s see how this is implemented in the open-source Gato GraphQL plugin.

AbstractPlugin class:

AbstractPlugin represents a plugin, both for the main Gato GraphQL plugin and its extensions:

abstract class AbstractPlugin implements PluginInterface
{
  protected string $pluginBaseName;
  protected string $pluginSlug;
  protected string $pluginName;

  public function __construct(
    protected string $pluginFile,
    protected string $pluginVersion,
    ?string $pluginName,
  ) {
    $this->pluginBaseName = plugin_basename($pluginFile);
    $this->pluginSlug = dirname($this->pluginBaseName);
    $this->pluginName = $pluginName ?? $this->pluginBaseName;
  }

  public function getPluginName(): string
  {
    return $this->pluginName;
  }

  public function getPluginBaseName(): string
  {
    return $this->pluginBaseName;
  }

  public function getPluginSlug(): string
  {
    return $this->pluginSlug;
  }

  public function getPluginFile(): string
  {
    return $this->pluginFile;
  }

  public function getPluginVersion(): string
  {
    return $this->pluginVersion;
  }

  public function getPluginDir(): string
  {
    return dirname($this->pluginFile);
  }

  public function getPluginURL(): string
  {
    return plugin_dir_url($this->pluginFile);
  }

  // ...
}

AbstractMainPlugin class:

AbstractMainPlugin extends AbstractPlugin to represent the main plugin:

abstract class AbstractMainPlugin extends AbstractPlugin implements MainPluginInterface
{
  public function __construct(
    string $pluginFile,
    string $pluginVersion,
    ?string $pluginName,
    protected MainPluginInitializationConfigurationInterface $pluginInitializationConfiguration,
  ) {
    parent::__construct(
      $pluginFile,
      $pluginVersion,
      $pluginName,
    );
  }

  // ...
}

AbstractExtension class:

Similarly, AbstractExtension extends AbstractPlugin to represent an extension plugin:

abstract class AbstractExtension extends AbstractPlugin implements ExtensionInterface
{
  public function __construct(
    string $pluginFile,
    string $pluginVersion,
    ?string $pluginName,
    protected ?ExtensionInitializationConfigurationInterface $extensionInitializationConfiguration,
  ) {
    parent::__construct(
      $pluginFile,
      $pluginVersion,
      $pluginName,
    );
  }

  // ...
}

Notice that AbstractExtension is included within the main plugin, providing functionality to register and initialize an extension. However, it is only used by extensions, not by the main plugin itself.

The AbstractPlugin class contains shared initialization code invoked at different times. These methods are defined at the ancestor level but are invoked by the inheriting classes according to their lifecycles.

The main plugin and extensions are initialized by executing the setup method on the corresponding class, invoked from within the main WordPress plugin file.

For instance, in Gato GraphQL, this is done in gatographql.php:

$pluginFile = __FILE__;
$pluginVersion = '2.4.0';
$pluginName = __('Gato GraphQL', 'gatographql');
PluginApp::getMainPluginManager()->register(new Plugin(
  $pluginFile,
  $pluginVersion,
  $pluginName
))->setup();

setup method:

At the ancestor level, setup contains the common logic between the plugin and its extensions, such as unregistering them when the plugin is deactivated. This method is not final; It can be overridden by the inheriting classes to add their functionality:

abstract class AbstractPlugin implements PluginInterface
{
  // ...

  public function setup(): void
  {
    register_deactivation_hook(
      $this->getPluginFile(),
      $this->deactivate(...)
    );
  }

  public function deactivate(): void
  {
    $this->removePluginVersion();
  }

  private function removePluginVersion(): void
  {
    $pluginVersions = get_option('gatographql-plugin-versions', []);
    unset($pluginVersions[$this->pluginBaseName]);
    update_option('gatographql-plugin-versions', $pluginVersions);
  }
}

Main plugin’s setup method:

The main plugin’s setup method initializes the application’s lifecycle. It executes the main plugin’s functionality through methods like initialize, configureComponents, configure, and boot, and triggers corresponding action hooks for extensions:

abstract class AbstractMainPlugin extends AbstractPlugin implements MainPluginInterface
{
  public function setup(): void
  {
    parent::setup();

    add_action('plugins_loaded', function (): void
    {
      // 1. Initialize main plugin
      $this->initialize();

      // 2. Initialize extensions
      do_action('gatographql:initializeExtension');

      // 3. Configure main plugin components
      $this->configureComponents();

      // 4. Configure extension components
      do_action('gatographql:configureExtensionComponents');

      // 5. Configure main plugin
      $this->configure();

      // 6. Configure extension
      do_action('gatographql:configureExtension');

      // 7. Boot main plugin
      $this->boot();

      // 8. Boot extension
      do_action('gatographql:bootExtension');
    }

    // ...
  }
  
  // ...
}



Source link

Keep an eye on what we are doing
Be the first to get latest updates and exclusive content straight to your email inbox.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address

WORDPRESS

6 Surprising Things You Can Do on WordPress.com Without a Plugin  – WordPress.com News

Published

on

By

6 Surprising Things You Can Do on WordPress.com Without a Plugin  – WordPress.com News

Newsletters? Spam protection? Image galleries? We have you covered, no plugin needed.

“WordPress” and “plugins” are often thought to be synonymous. The two go together like peanut butter and jam, like summer and watermelon, like yin and yang . . . you get the idea. It often seems like you can’t have one without the other. While plugins are indeed one way to supercharge your WordPress.com site, the truth is that there’s a lot you can do without ever installing a plugin. In fact, I can almost guarantee that our out-of-the-box WordPress.com experience is more powerful than you think.

(Quick reminder: WordPress plugins are available and installable on our Creator and Entrepreneur plans.) 

Today, we’d like to highlight six surprising things you can do with WordPress.com from the moment you start building a website. 

Sell digital products and accept donations 

There’s no plugin needed to make a living or earn some cash on the side by selling digital products like ebooks, songs, or photographs on WordPress.com. With our built-in payment blocks Payment Buttons, Pay with PayPal, and Donations Form, you’re one click away from collecting money on your website. Best of all? Most of these blocks can be used on any plan, including Free, the exception being the PayPal block, which requires the Explorer plan or above. 

Simply connect your Stripe account to get started selling today.

Prevent spam 

6 Surprising Things You Can Do on WordPresscom Without a

Just like the spam comments and messages you’re trying to block, the number of anti-spam plugins has proliferated in recent years. Luckily, you don’t need any of them, because Akismet, an Automattic product, is baked into every WordPress.com website and provided at no extra cost. With advanced filtering that blocks 99.99% of spam, you’ll never have to worry about unwanted visitors again.  

Install SSL certificates 

1719464163 886 6 Surprising Things You Can Do on WordPresscom Without a

On WordPress.com websites, SSL certificates are provided free of charge and automatically installed for you. This feature provides important security against hackers and other malicious actors, particularly if your website collects user information of any kind. At other hosts, you’ll often have to either pay extra or install your own (expensive) plugin in order to add an SSL certificate. Not at WordPress.com. Learn more about our SSL process here.  

Send newsletters 

6 Surprising Things You Can Do on WordPresscom Without a

Since 2009 WordPress.com has had the built-in functionality of sending new posts as emails. That’s right, you don’t need a third-party service or platform (like Mailchimp or Substack) to send newsletter emails to your audience. Using a Subscribe block gives visitors a simple and convenient way to enter their email and get your posts right to their inbox. 

You can also set up paywalls (with the Paywall block) and paid content tiers, allowing for multiple subscription levels. Additionally, you can view and manage subscriber details from the Subscribers page (found under “Jetpack” on the left-side menu). Learn more at WordPress.com/newsletters

Embed videos 

1719464164 314 6 Surprising Things You Can Do on WordPresscom Without a

Videos can be a vitally important part of your website and content flow, but uploading them can be a pain in the neck—if you’re not using WordPress, that is. If you’re embedding a video from another source, like Vimeo or YouTube, use our built-in blocks of the same name. And here’s a helpful tip: you don’t even need to select the block first. Simply copy and paste the video link right into the editor, and WordPress will automagically do the rest. 

For embedding your original video files (.mov, .mp4, .mwv, etc.), Automattic’s very own VideoPress block offers a straightforward and robust solution. With caption and chapter support, as well as detailed data and insights on views, once you try out VideoPress you won’t look back. This feature is available on Explorer plans and above. 

Insert impressive image galleries 

1719464164 108 6 Surprising Things You Can Do on WordPresscom Without a.webp

Well-done imagery on a website can mean the difference between an engaged visitor and a bounced visitor. Rather than experimenting with overly complicated plugins, use the various image blocks that come with WordPress. Our Gallery, Slideshow, and Image Compare blocks are especially fun and offer a range of easy-to-use customizations that don’t overwhelm. Plus, these blocks are always optimized for mobile.   

Start building today 

This is just a sampling of what you can do with WordPress.com. We didn’t even mention some of our favorite blocks, including: table of contents, music/podcast player, countdown timer, tables, and so much more. 

Ready to explore these powerful built-ins? Get started today:  

You may still find that using plugins solves your specific needs a bit better than what’s already built into the editor. If that’s the case, consider our world-class Creator or Entrepreneur plan. 


Join 111.6M other subscribers

Source link

Keep an eye on what we are doing
Be the first to get latest updates and exclusive content straight to your email inbox.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address
Continue Reading

WORDPRESS

New WordPress.com Themes for June 2024 – WordPress.com News

Published

on

By

New WordPress.com Themes for June 2024 – WordPress.com News

Five of our favorite new themes.

The WordPress.com team is always working on new design ideas to bring your website to life. Check out the latest themes in our library, including great options for crafters, legal firms, and artists.


Craftfully is a magazine-style WordPress theme designed for all things homemade, DIY, crafts, and hobbies. Utilizing plenty of white pace, lighter colors, and playful fonts, this theme delightfully evokes a sense of playfulness and DIY creativity. Below a prominent featured section and newsletter sign-up box, a two-column layout displays the “Latest Posts” as well as a short bio and social links. Craftfully is more than just a theme, it’s a canvas for your imagination.

Click here to view a demo of this theme.


New WordPresscom Themes for June 2024 – WordPresscom News

Though Portia is simple in its design and layout, it sends a powerful message: your firm is the best in the industry. With a focus on conveying professionalism, stability, and dependability, Portia is built with legal firms in mind, but is versatile enough to work for any business that relies on trustworthiness and respectability. You won’t find any rock-the-boat design elements here. It’s all about communicating the right information at the right time.

Click here to view a demo of this theme.


New WordPresscom Themes for June 2024 – WordPresscom News

Kiosko is a sleek and modern WooCommerce theme tailored for online stores specializing in art prints and home goods. With its high contrast black and white design, Kiosko offers a minimalist aesthetic that puts the spotlight on your products. It’s straightforward and effective, meaning you can focus on your art and your sales rather than your infrastructure.

Click here to view a demo of this theme.


Our newest collection of beautiful, opinionated themes for bloggers, businesses, and creatives.

Dark Academia is a blog theme with a dark, moody aesthetic. Its sophisticated layout will especially stand out to visitors. The sticky left half features your site’s name and the primary navigation menu, while the right half scrolls through your latest posts. This style is perfect for blogs focused on literature, history, and fashion, and it’s naturally deal for those who appreciate the Dark Academia vibe. The elegant and immersive reading experience will make your site memorable for anyone who happens by it.

Click here to view a demo of this theme.


1719291364 318 New WordPresscom Themes for June 2024 – WordPresscom News

OnyxPulse is a sleek WordPress theme with a modern, minimalist design, perfect for blogs about design, future trends, and innovation. Its grid layout and high-contrast visuals are ideal for showcasing cutting-edge content and engaging a tech-savvy audience. The striking black-and-white color palette and sharp design elements are sure to catch your visitors’ eye. For the typography, we’ve opted for Chakra Petch, a square sans-serif font; its sharply tapered corners are a perfect match for this theme.

Click here to view a demo of this theme.


To install any of the above themes, click the name of the theme you like, which brings you right to the installation page. Then click the “Activate this design” button. You can also click “Open live demo,” which brings up a clickable, scrollable version of the theme for you to preview.

Premium themes are available to use at no extra charge for customers on the Explorer plan or above. Partner themes are third-party products that can be purchased for $99/year each on the Creator plan and above.

You can explore all of our themes by navigating to the “Themes” page, which is found under “Appearance” in the left-side menu of your WordPress.com dashboard. Or you can click below:


Join 111.5M other subscribers

Source link

Keep an eye on what we are doing
Be the first to get latest updates and exclusive content straight to your email inbox.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address
Continue Reading

WORDPRESS

Five Takeaways from WordCamp Europe 2024 (From a First-Time WordCamp Attendee) – WordPress.com News

Published

on

By

Five Takeaways from WordCamp Europe 2024 (From a First-Time WordCamp Attendee) – WordPress.com News

This year’s WordCamp Europe was held in Torino, Italy, the capital city of the Piedmont region in northern Italy. Torino is known for its rich automotive history, beautiful architecture, and, of course, incredible food.

From June 13-15, 2024, over 2,500 folks from the WordPress community, including many of us from the WordPress.com team, came together to learn, connect, and give back to the WordPress project that powers over 43% of the entire internet.

I joined the WordPress.com team back in January of this year, so WordCamp Europe 2024 was my first WordCamp experience. In today’s post, I thought it might be interesting to hear about the conference from a first-timer, especially if you’re considering attending a WordCamp or WordPress meetup in the future. 

Here are my top five takeaways from my very first WordCamp:

1. In-person connection is powerful.

If your typical workday looks similar to mine––sitting at a desk at your house all day by yourself––going to a conference as large as WordCamp Europe may be a wee bit out of your comfort zone. It certainly was out of mine.

That said, I’ve recently found myself craving in-person connection after the pandemic and working almost exclusively from home for over eight years. Not only did attending this conference just get me out of my normal routine, it allowed me to connect with folks who love the tool I’ve used personally and professionally for over a decade: WordPress.

I staffed the WordPress.com booth, so I had a ton of opportunities to chat with other business owners, developers, creators, and makers over the course of the conference. I actually talked with a few fellow self-taught women developers like me, and I walked away feeling inspired, motivated, and just really thankful to be a part of this community.

But the best conversations happened in places I wouldn’t have expected: over spritzes, grabbing a cafe at the venue, or just walking around the city.

Embracing connection was a big focus of Matt’s final keynote speech during the event, and I couldn’t agree more. Events like WordCamps allow for swapping ideas, collaborating and troubleshooting, and experiencing a sense of community that you just don’t get while sitting behind a computer. 

That said, if large-scale conferences like a flagship WordCamp just aren’t for you, try checking out a local WordPress meetup to connect with other like-minded folks in your community.

2. It takes a ton of people to make WordCamps great.

One of the most moving parts of WordCamp Europe was at the very end when all of the volunteers and organizers were called to the stage; it’s truly amazing just how many people need to be involved to make an event like WordCamp actually happen.

People in black tshirts on a stage with a blue background and other people sitting in chairs facing the stage
Screenshot from the WordCamp Europe 2024 livestream on YouTube

Everything was smooth and well-organized, and the volunteers and organizers could not have been more helpful. Their enthusiasm about the event, WordPress, and community in general throughout the conference was infectious.

WordCamps and local WordPress meetups are always looking for volunteers; donating your time and expertise for events and meetups like this are a great way to give back to the WordPress project and community.

And if you’re an organizer of your local WordPress meetup, check out this post for information on how you can get a free WordPress.com website for your local meetup.

3. Contributor Day isn’t intimidating for a first-timer.

I’ve never contributed to WordPress core, but it was one of my goals for this year. That’s why I was so excited to participate in Contributor Day at WordCamp Europe.

If you’re unfamiliar with Contributor Day, it’s an event that usually kicks off a WordCamp. Teams focus on contributing to the WordPress open source project, with groups focused on code, support, translations, sustainability, inclusion, and more.

After listening to all of the team presentations, I decided to join the Accessibility team. Accessibility is something that has always interested me, but it’s also something I don’t have a ton of experience with.

That said, my inexperience wasn’t just accepted, it was actively welcomed.

people sitting around circular tables and #wceu on a wall

Once I got to the Accessibility team table, I was immediately greeted and welcomed. Then I paired with a fellow contributor, Marco Acato from Acato Digital Agency, to test the accessibility of a new theme for the WordPress theme repository.

I learned so much, asked a ton of questions, and felt surprisingly accomplished after just a few hours of testing this theme. We were actually able to publish feedback for the theme developer at the end of the day as well. 

Contributor Day gave me an even deeper appreciation for the entire community that supports the WordPress project every single day. So much work and effort goes into maintaining and improving this tool that millions of websites across the world rely on to run their businesses, amplify their messages, and stay in touch with others. I felt so grateful to have been a part of it during Contributor Day and would encourage any other first-timers to attend a Contributor Day in the future as well.

Luckily, WordPress core is always looking for volunteers and contributors; check out this guide or the new Contributor Mentorship Program if you’re interested in becoming a contributor yourself.

4. Torino was a great host city, and the WordCamp team made navigating a new city easy.

Between attending Contributor Day and sessions, to checking out sponsor booths and attending side events, I didn’t think we’d have a ton of time to actually see the city or Torino.

I actually had plenty of time to explore with my coworkers, eat pizza every single day, and scope out the best gelato spots outside of conference hours. 

a gelato cup filled with two different colors of gelato with a spoon sticking out of it

The WordCamp Europe team did a great job preparing attendees to make the most of our time in the city as well; their travel guides helped me feel confident navigating the city and finding some of the foods that come from this area in Italy.

As a first-timer in Torino, I really appreciated the extra work that the WordCamp team did to ensure everyone had a chance to explore and experience the city.

5. Pizza really is poetry.

I would be remiss to not mention the food that we ate during our time in our host city! While we like to say that “Code is poetry” around here, so is pizza. 

One of my very favorite memories from the event was the branding. The design team for WordCamp Europe 2024 added subtle nods to our host country throughout the venue, which was incredibly clever and well-done.

a sign that says 'Code is Poetry (...but so is pizza)' and #wceu

And while pizza is indeed poetry, it’s even better when shared amongst coworkers, friends, and people who get excited about the same things that excite you.

five people sitting at a table with three large pizzas on top

Wrapping up

I loved my time at WordCamp Europe, and I’m already looking forward to the next time I can connect face-to-face with the WordPress community. 

Were you at WordCamp Europe this year? Leave a comment with your favorite memory from the event below.


Join 111.4M other subscribers

Source link

Keep an eye on what we are doing
Be the first to get latest updates and exclusive content straight to your email inbox.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address
Continue Reading

Trending