WordPress / Learn

WordPress.org Learn - The canonical source for the code and content behind https://learn.WordPress.org
270 stars 97 forks source link

Rendering plugin data #2182

Open jonathanbossenger opened 8 months ago

jonathanbossenger commented 8 months ago

Initial draft script:

# Rendering Information

When developing custom plugins, there may be instances where you need to render information on the front end of a WordPress site.

In this lesson, you'll learn about some common ways to implement rendering of information from a plugin.

## Theme template

As you learned in the [What is a plugin](01-what-is-a-plugin.md) lesson, a WordPress plugin adds new features or functionality to a WordPress site.

The most common way to display information from WordPress is to use specific theme template files.

For example, in the bookstore example plugin, you created a custom post type for books.

You could then create custom templates like an `archive-book.html` or `single-book.html` in the theme to render the books archive or an individual book's information.

Depending on what you are working on will determine how you implement this:

- If you're working on a custom site that you have full control over, and you're also building a custom theme, you can add these templates to the theme
- If you're working in customising an existing theme, you can add these templates to a child theme

Developing custom templates is outside the scope of this lesson, but you can learn more about this in the [Theme Developer Handbook](https://developer.wordpress.org/themes/), and more specifically the [Templates](https://developer.wordpress.org/themes/templates/) section of that handbook.

You can also watch the "An introduction to developing WordPress themes" module in the Beginner Developer Learning Pathway as well as the Intermediate Theme Developer Learning Pathway to learn more about developing custom themes and theme templates. 

## Custom Templates in the Site Editor

It is also possible to create a custom template in the Site Editor. 

When you register a custom post type, the Site Editor offers you the option to create templates for that custom post type.

For example, with your book custom post type enabled, navigating to the Templates section in the Site Editor and clicking Add New Template will give you options to add new archive and single item templates for your book.

You can then use the Template Editor to create and edit these templates.

For example, with a Single Book template created and saved, viewing the book post type in the front end of the site will use the Single Book template to render the book information.

## Custom Blocks

Another way to render information from a plugin is to use custom blocks.

Blocks are modular components used to compose and format content in a WordPress site, and are used to create rich and flexible layouts for post, pages, and just about any other WordPress content.

You could develop a custom block to render information from your plugin, which the users of your plugin could then implement in their own posts, pages, or custom templates.

You will learn more about blocks in the Introduction to WordPress Blocks section of the Beginner Developer Learning Pathway.