I am new to WordPress development and am creating a simple plugin that creates a custom post type of event that has the following additional fields:

  • event_date
  • event_time
  • event_location

    At the moment I have defined a filter to add these fields to the content like this:

function add_custom_event_fields_to_content($content)
{
    if ('event' != get_post_type()) {
        return $content;
    }

    $custom_content = $content;

    $unix_timestamp = get_post_meta(get_the_ID(), 'event_date', 'true');
    $custom_content .= '<p><b>Date</b><br/>' . date('F d, Y', $unix_timestamp) . '</p>';

    $time = get_post_meta(get_the_ID(), 'event_time', 'true');
    $custom_content .= '<p><b>Time</b><br/>' . $time . '</p>';

    $location = get_post_meta(get_the_ID(), 'event_location', 'true');
    $custom_content .= '<p><b>Location</b><br/>' . $location . '</p>';

    return $custom_content;
}
add_filter('the_content', 'add_custom_event_fields_to_content');

But this feels like I am taking away the ability for the theme to define how to render an event page by using single-event.php. For example, the theme designer may want to put the event date at the start of the page.

Am I doing the right thing by using the the_content filter? Or is there another more flexible way to provide a default layout of an event page?

Read more here: WordPress Custom Post Types: Where/How do I specify default output?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: