Support » Fixing WordPress » Best way of implementing custom post type frontend?

  • Hi,

    I am currently developing a plugin which incorporates a custom post type.

    Upon activation of the plugin and the user adding a new post, there should be a section in the front-end to show it.

    I could use the cpt archive/single page however I need to show some more options in addition to the post and that will require customizing both the archive/single pages.
    The issue is that if I do that, it will not work with all WordPress themes.

    I thought of creating a shortcode and automatically create a page containing that shortcode that will handle everything but it will be like reinventing the wheel. Or is this the only way?

    Or is it Utopian to believe that it can work with all themes?

    Edit: I forgot the use of content filters. That’s another option.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, do not attempt to alter theme templates. At a minimum it’s a very fragile approach. If you want your plugin listed in the WP repository, it will be rejected if it changes theme files.

    If your plugin content can appear at the beginning or end of content, you could use “the_content” filter, but you should give users a way to manage on which posts or pages you add content, unless it’s the sort of thing that should be on every post, like an up/down vote element or something.

    Shortcodes have been the common solution for many plugins which allows users to add your plugin’s output anywhere they like. With the introduction of the new block editor (Gutenberg), we are expected to forego shortcodes for custom block elements. With so many people using the classic editor still, plugin devs should be providing both. In any case, I expect the block editor to support shortcodes for a good long while.

    For users with a child theme who have customized their theme templates, it might be useful to provide a PHP template tag (just a normal function call which generates output) for their use.

    Thanks for your reply, quite informative.

    I think I might go with a shortcode for the main listing of the posts (it will essentially replace the CPT archive page) and will use the default links for the single CPT pages.

    I don’t think a shortcode is the way to go, especially for archives because there is nowhere to put a shortcode in an archive context (where you don’t have to modify the query, and to handle pagination correctly).
    The plugin can easily filter the content to add the custom portion, either transforming the original content or adding on to it. This allows all themes to use their own layout for the page but the content comes from the plugin.

    I have already done this with the_content filter and it works great for single pages.

    I’ll look into doing the same for archive pages as I want to have some customization/flexibility regarding how they are displayed etc.

    Thanks for your input.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Best way of implementing custom post type frontend?’ is closed to new replies.