wordpress simple shortcodes not rendered when using Timber plugin

五迷三道 提交于 2019-12-24 06:45:43

问题


I've recently taken over dev of a Wordpress site using Timber (which I wasn't familiar with). I'm attempting to use a new plugin and accompanying shortcode, which of course doesn't work.

I've been researching this for a couple hours and there doesn't seem to be a simple answer. In order to use a simple shortcode like this:

[sp_faq category="7"]

Do I really need to create a custom shortcode function in functions.php, add some sort of template file for it, etc? This seems counterintuitive to Twig's making things 'simple'.

The documentation for this is less than stellar unless I'm missing something obvious.


回答1:


If you want to render a shortcode from a custom field you can do it like this:

{{post.custom_shortcode_field|shortcodes}}

Taken from the docs: https://timber.github.io/docs/guides/filters/#shortcodes

Or if it's comming from the main-editor try:

{{ post.content|wpautop }}



回答2:


Seems like this is too late for OP, but for anyone else coming across this: The correct solution is combined in the Luckyfella's answer and the comments on it. I think OP would have got it working if he had tried Luckyfella's final suggestion.

You need to put {{post.post_content|wpautop|shortcodes}}* into your Twig file(s). This will render both auto paragraphs and shortcodes that are put into the main WYSIWYG editor, by default.

*post is simply the conventional default name in Timber for a TimberPost, you will also need to check in your PHP template files to see what the Timber context and Timber post variables are called. For OP it seems to have been page, not post.




回答3:


You could try this (example using a gravity form shortcode):

{% filter shortcodes %}
  [gravityforms id="1"]
{% endfilter %}



回答4:


Just thought I'd chime in here.

post.post_content contains the raw data that is contained in the database (before any filters have been applied to it) and post.content contains the data after the filters have been applied, so using the filter |shortcodes shouldn't be needed to be run on that.

I was using post.post_content for some reason and found this question because I was trying to figure out why my shortcodes weren't working and hence it led me to do some more research.

Now, reading the answers in this question, I wasn't really satisfied with as I had also been using |e('wp_kses_post') to sanatise the data that I output, but if I used something like this:

{{ post.content|wpautop|e('wp_kses_post') }}

...then obviously I would get the correct filtered data with the shortcodes processed, but it would also at the same time strip out any non-allowed data with the e('wp_kses_post') filter.

Sure, you could add allowable tags within this filter, but obviously that isn't very realistic as you don't know exactly what output the shortcodes will be outputting nor do you want to keep updating it.

So, we had a problem... we want to allow shortcodes to be parsed, but also sanatise the content at the same time - what to do!?

The solution is the below:

{{ post.post_content|wpautop|e('wp_kses_post')|apply_filters('the_content') }}

Here we use post.post_content so we have the content before it has been filtered, then after the wpautop filter it is followed by e('wp_kses_post'); this will sanatise the data, but the important part being it will leave shortcodes alone, so they can be still be filtered!

Lastly, we apply the filters for the content with the apply_filters filter, and this then takes care of all the filters applied to the_content including parsing the shortcodes.

Though if you are already using universal escaping you will have to consider how the above will apply to your situation.



来源:https://stackoverflow.com/questions/47440122/wordpress-simple-shortcodes-not-rendered-when-using-timber-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!