Skip Navigation

[Resolved] Is it possible to insert a content template into a theme PHP template?

This thread is resolved. Here is a description of the problem and solution.

Problem:
Is it possible to insert a Content Template created with Views into a PHP template from the theme or a plugin?

Solution:
You need the ID of the Content Template.

Then you can add the following to the PHP template to output the Content Template:

$template = get_post( 5678 ); // Edit the ID
echo apply_filters( 'the_content', $template->post_content );
This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Jeffrey 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#603367

I read a article, and knew I can override the woocommerce template. woocommerce account pages can not be inserted any content, once added anything, all those will be brought into order, account detail, download pages, but in dashboard page, I would like to place more information, because my site has other more complex information, for example cred front end submission form etc. so, I copied dashboard.php to mysite-child-theme/woocommerce/myaccount/dashboard.php. I have tried that I can hide something on the dashboard page. but in my case, I want to make the page more flexible, so I think if I can insert a content template on this page if possible? 🙂
Thank you so much!

#603434

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeffrey

What you are describing falls outside the expected use of Content Templates which are intended to allow you to customise content without having to edit the PHP templates, rather than embedding them in PHP templates.

A Content Template is stored in wp_posts with a post id (the post_type is view_template).

So in theory, knowing the ID of the template, you could output it in a PHP file by just outputting the content of that post. You will want to make sure the shortcodes are parsed, though, meaning you need to pass it through the_content filter.

I haven't tested it but you could try outputting a Content Template in a PHP template like so:

$template = get_post( $id ); // Edit the ID
echo apply_filters( 'the_content', $template->post_content );

If you encounter problems with what is output I'd say that may be expected, as this is not the intended use of Content Templates.

#603544

Hi Nigel,
Thank you so much for you response, I have tried using the code you provided. just replace the $id to $id=5678 right? note, 5678 is toolset content template id. but it seems not working. I found I can add some html into dashboard.php, and appearing fine. but I am just afraid, all these data I embed into dashboard.php that can not be bilingual by WPML. because I found nowhere to translate the string. I also accept adding some html code and content on the file. but no way to translate them. what I should do?

#603774

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You need to replace $id with the actual content template id number, i.e.

$template = get_post( 5678 );
echo apply_filters( 'the_content', $template->post_content );

I just tested this locally with a very basic CT that outputs some simple HTML, and then I used the same code as above inserted into the dashboard.php file to output the content of the template in the dashboard, and it worked.

So, in principle you can do it, though I can't be sure of what results to expect if you start to add more complex content to the template.

If you edit dashboard.php and want to add text directly to it, for it to be translatable you must output it using gettext, as described in the "Strings for Translation" section of this page: https://codex.wordpress.org/I18n_for_WordPress_Developers

#604576

Hi Nigel, Now it is working fine. I can add some html code and text except Toolset view, but tollset content template working fine! Thank you so much! 🙂