Below, you’ll find a detailed guide on how to add the TinyMCE Templates Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the TinyMCE Templates Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the TinyMCE Templates Plugin and the shortcodes it provides:
"TinyMCE Templates is a highly effective WordPress plugin that enhances your editing experience by providing customizable, reusable templates for your TinyMCE editor."
- [template]
TinyMCE Templates [template] Shortcode
The tinymce-templates plugin shortcode, ‘template’, dynamically pulls content from a specific post. This shortcode retrieves content from a post of type ‘tinymcetemplates’ using its ID. It checks if the post is published and if it’s set to be inserted as a shortcode. If both conditions are met, the shortcode will output the post’s content.
Shortcode: [template]
Parameters
Here is a list of all possible template shortcode parameters and attributes:
id
– It is the unique identifier of the template
Examples and Usage
Basic example – A shortcode that uses the ‘template’ shortcode to display the content of a specific post by referencing its ID.
[template id=2 /]
Advanced examples
In this example, we use the ‘template’ shortcode to display the content of a specific post by referencing its ID. If the post is set to be inserted as a shortcode, it will display the post content. If not, it will return an empty string.
[template id=3 insert_as_shortcode=true /]
In this next advanced example, we use the ‘template’ shortcode to display the content of a specific post by referencing its ID. We also pass additional parameters to the shortcode, which will be used in the ‘tinymce_templates_content’ filter. This allows us to further customize the output of the shortcode.
[template id=4 param1="value1" param2="value2" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [template]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'template', array( $this, 'template_shortcode' ) );
Shortcode PHP function:
function template_shortcode( $p, $content )
{
$post_content = '';
if ( isset( $p['id'] ) && intval( $p['id'] ) ) {
$args = array(
'ID' => $p['id'],
'post_status' => 'publish',
'post_type' => 'tinymcetemplates',
);
$post = get_post( $p['id'] );
if ( is_a( $post, 'WP_Post' ) ) {
if ( get_post_meta( $p['id'], 'insert_as_shortcode', true ) ) {
$post_content = $post->post_content;
}
}
}
return apply_filters( 'tinymce_templates_content', $post_content, $p, $content );
}
Code file location:
tinymce-templates/tinymce-templates/tinymce-templates.php
Conclusion
Now that you’ve learned how to embed the TinyMCE Templates Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply