How to create a shortcode for custom page template

I have a custom page template and I want it to display on specific pages. The only way I know to do this is to create a shortcode for this custom page template, so that I can use the custom page template by using shortcode. But I don't know how doing this.

I saw some article about this, but I forgot the website link.

How to create a shortcode for custom page template?

Topic shortcode page-template Wordpress

Category Web


I find this one is the easiest way:

<?php echo do_shortcode("[shortcode]"); ?>

I solved using below method.

Using Plugin (Shortcodes Ultimate) we can create or make shortcode for wordpress custom page template.

  1. create your custom page template without including header and footer.
  2. Install the plugin(find link below) into wordpress site.
  3. Activate it.
  4. Go to plugin Shortcodes Ultimate › Available shortcodes.
  5. You can find the variety of filter options in that click Other will find Template open that.
  6. In shortcode section add the name of your custom template([su_template name="name-of-your-template.php"]) with or without php extentions.
  7. Use template file name (with optional .php extension). If you need to use templates from theme sub-folder, use relative path. Example values: page, page.php, includes/page.php
  8. Now copy the whole shortcode and paste it into the page or post you want.

Plugin download link

Shortcodes Ultimate


I solved now, by adding this snippet in functions.php

function my_form_shortcode() {
   ob_start();
   get_template_part('my_form_template');
   return ob_get_clean();   
} 
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.