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.

Category:
Articles
Tags:

Join the conversation! 1 Comment

  1. One thing I’ve recently learned about using “the_content” filter is that the callback function can be called multiple times per page load.

    So if you have this:

    add_filter( 'the_content', array( $this, 'the_content' ) );

    function the_content( $content ) {

    if ( is_singular() ) {
    $content .= get_custom_content();
    }

    return $content;

    }

    Then get_custom_content() may get called 2+ times.

    Recently I started using a few more conditionals in my “the_content” callback functions like this:

    add_filter( 'the_content', array( $this, 'the_content' ) );

    function the_content( $content ) {

    if ( in_the_loop() && is_singular() && is_main_query() ) {
    $content .= get_custom_content();
    }

    return $content;

    }

    Additionally, if I’m targeting a specific post or page ID, then I will also add a conditional like get_the_ID() == 123.

    This addition of more conditionals in my code prevented unnecessary extra calls to functions and extra data processing.

    Not sure if there is a better way but this method worked for me.

    Eric

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.