When we – or the people for whom we’re working – create WordPress page content, it’s usually through the editor or maybe through some other client front-end that we’ve created.

But what about the case where there’s something specified using a custom settings page that controls the content displayed on a specific page?

This is getting into custom implementation territory so let me try to give a bit more context to this whole situation.

Adding Custom WordPress Page Content

Let’s say you have a custom settings page – maybe you’ve built it, maybe it’s come from a plugin, or maybe another developer has created it – and it gives the option to append content to one of the pages that exists in the database.

WordPress Page Content: From DB to Page

That is, let’s say it’s setup like this:

  • there’s a select element that provides a list of all of the pages in the WordPress installation,
  • the user chooses the page for which they want to display the content,
  • in this example, we’ll list all of the users having the ‘editor’ role at the bottom of the page.

Usually, we can take advantage of the the_content hook – and we still can (and will) – but if you’re working with part of a pre-existing solution that’s written using object-oriented code, then you need to approach it a little bit different.

  1. Create a class.
  2. Read the database options and register your hooks in an initialization method.
  3. Create a function to query all of the users.
  4. Append each user to the content.

Here’s how I follow the above steps.

Note the code you’re going to read is more-or-less demonstration code. There are things such as sanitization, validation, and what not that would need to be handled in a production environment.

This is meant to be a high-level example of how this can work.

1. Read The Database, Register a Hook

In the init function, we can read the value from the database.

As long as it’s not 0, then we can setup a call to the function in the call to begin reading the user information.

2. Retrieve The Users

In the example below, I’m grabbing the users having the editor role and I’m ordering them by their ID.

I return the query and then it will be passed to the next function.

3. Render The Function

Again, before reading this note, that that markup returned from this function must be sanitized before rendering it to the browser. This is just a demo (and the code comment shows that).

And using this, you can print out the result of the query to the page.

It’s a Demo

Remember: The above code is for demonstration purposes. It may need additional refactoring for your use, it needs additional sanitization functions, and it needs to be displayed with whatever markup you deem fit for your purposes.

The point I’m trying to demonstrate, though, is that you can take data in the database and then, based on a value, append it to an existing page without having to muck with any templates or anything.