2

I am attempting to load a page template into a shortcode so I can easily load the content wherever I want.

I have done some research and many people have said this code has worked for them but for some reason this does not seem to load my template right as I just get a blank page.

I know the shortcode is executing as it does not show as plain text so I'm guessing there is a problem with the way I am loading the template.

Any help is much appreciated .

public function register(){
        add_shortcode( 'sponsor_main_page', array($this,'my_form_shortcode') );
            $RegistrationFormId = esc_attr( get_option( 'ik_form_id' ) );
        }

function my_form_shortcode() {
        ob_start();
        get_template_part( 'template-sponsors.php' );
        return ob_get_clean();
    }
1

get_template_part takes slug as first parameter and not filename.

So it should be:

get_template_part( 'template-sponsors' );

And with more details... This function takes two parameters:

get_template_part( string $slug, string $name = null )

And inside of it, the name of a file is built like this:

if ( '' !== $name )

        $templates[] = "{$slug}-{$name}.php";       $templates[] = "{$slug}.php";

So, as you can see, the .php part is added automatically. So your code will try to load file called template-sponsors.php.php and there is no such file, I guess.

1

It's likely to be occurring because you are adding .php to the parameter in get_template_part(). This is how I would write the code for your question:

Prerequisites for this example to work:

  • Create a folder in your theme directory called template-parts.
  • Create a new file in this directory called template-sponsors.php
  • This is assuming your shortcode is [sponsor_main_page]

Code to put in your functions.php:

function custom_theme_load_sponsors_template() {
  ob_start();
  get_template_part( 'template-parts/template-sponsors' ); 
  return ob_get_clean();
}
add_shortcode( 'sponsor_main_page', 'custom_theme_load_sponsors_template' );
-1

I like using locate_template, rather than get_template_part . The locate_template will do the equivalent of a PHP include() or include_once(), which is what I think you are aiming for.

locate_template("mycustomfunctions.php",true,true);

Put your mycustomfunctions.php file in the active template folder (optimally, a Child Theme). Then put the locate_template() command in your template, at the very top (after the comment block). Any functions in your mysustomfunctions.php file will then be available to your template.

The locate_template function is called by get_template_part. But the parameters allow you to define how the include works. See https://developer.wordpress.org/reference/functions/locate_template/ for more info.

(Added See the accepted answer to this question: Get template part vs locate template function . )

  • Not sure why the downvote; see the last paragraph of my answer. The locate_template is useful when you want to include a functions-type file (a file of functions that are needed, but you don't need to be loaded on every page as they would be with putting the functions in the functions.php file). See the accepted answer to this question: wordpress.stackexchange.com/questions/146134/… . – Rick Hellewell Feb 19 at 18:48

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.