0

I'm trying to develop a plugin with a custom post type that can be used in different themes thats why creating a single-post_type.php template file is out of the question. The custom post type has a lot of meta fields that i want to place by using placeholders.

At the moment the plugin looks for a default page.php template in de current theme. But often templates have extra settings to design the page that i cant use because every theme works in different ways.

My idea is to use an existing (non published) page as template for my custom post type. By adding shortcodes (placeholders) to the page that get replaced by the content of the custom post type. How would i make this possible.

Example:

post:       Template
post type:  page
url:        --not published--
content:    <h1>[placeholder field="title"]</h1>
            <p>Genre: [placeholder field="genre"]</p>

post:       Terminator 2
post type:  movie
meta genre: action
meta actor: Arnold
url:        site.com/movie/terminator-2/
content:    <h1>Terminator 2</h1>
            <p>Genre: action</p>

I know how to use the placeholders but how could i use the template page as template so that i could use all the functionalities of the used theme.


Edit: Tried solution: I've added a rewrite rule

global $wp,$wp_rewrite;
$wp->add_query_var('movieslug');
$wp_rewrite->add_rule('(.*)movie/([^/]+)', 'index.php?pagename=movie-template&movieslug=$matches[2]', 'top');

This seems to work and i used filter the_title to change the title, but this doesn't change the <title> tag, and this doesnt really feel like a solution, any ideas would be welcome.

7
  • Sorry to say your rewrite solution makes even less sense to me. I have no idea what problem you're trying to solve with that.
    – Milo
    May 12, 2017 at 13:13
  • it's hard to explain, i want to make an easy to use plugin which works in every theme without the need to write a template file in code. My custom post type is imported form a feed and has a lot of meta fields. Users cant edit the posts because they would automatically be overwritten. In the settings page of my plugin they are able to place placeholders in a wysiwyg editor. Then in the frontend i replace the_content from a post with the field from the settings, replacing the placeholders with meta. Works perfectly but only effects the content, many themes have page settings that i cant use.
    – Gijs
    May 12, 2017 at 13:41
  • what are "page settings"? I don't understand the connection you are trying to draw to pages, or how your rewrite rule solution differs from just directly loading the single view that WordPress generates for each cpt post.
    – Milo
    May 12, 2017 at 13:57
  • for example a theme i'm testing has the ability to disable the header for a specific page. These options miss for custom post types see image
    – Gijs
    May 16, 2017 at 7:15
  • You are free to use whatever template you want, but I don't see how you will magically access these theme-specific features that you can't possibly predict.
    – Milo
    May 16, 2017 at 13:13

1 Answer 1

0

If you are considering using a page with shortcodes, it sounds like you are really just creating a custom layout for the actual content of the post, not changing the full layout.

If so, you could just filter on 'the_content' and output your custom fields into that content. WordPress will default to use the standard single.php if no custom post type template is specified, but your fields will appear as part of the content section of the page.

3
  • the important stuff is outside the_content part, thats what i need for my custom post type. (language barrier makes it hard to explain)
    – Gijs
    May 11, 2017 at 12:53
  • @Gijs There is no special connection between a page and the template that ultimately displays it. You're not going to get access to any areas outside the_content without knowing the specifics of how each theme achieves that.
    – Milo
    May 11, 2017 at 15:15
  • I've edited my post with an attempt, if you have any other solution please tell me
    – Gijs
    May 12, 2017 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.