0

I'm using a theme for my WP site that has its own registration page template (register.php). I'm trying to create a shortcode that will load that template wherever I want, but I can't make it work.

This is one thing I've tried:

function my_BP_registration_form_shortcode( $atts ) {
 ob_start();
 get_template_part( 'register' );
 return ob_get_clean();
}
add_shortcode( 'my_BP_registration_form', 'my_BP_registration_form_shortcode');

This is another thing I've tried:

function my_BP_registration_form_shortcode( $atts ) {
 include('/full/path/to/template/register.php');
}
add_shortcode( 'my_BP_registration_form', 'my_BP_registration_form_shortcode');

None of those 2 things work. Maybe I'm going completely wrong about this, I'm just trying to show the registration form additionally to the default WP registration page.

The shortcode works fine btw, I can place it anywhere I want and it will load whatever contents I put in that code, just not the template I want. In case it helps the template if from a theme that uses Buddypress.

EDIT: Something important to point out is that the template I'm trying to use is inside the plugins folder (inside the Buddypress plugin). I've tried copying that template to my theme folder and while the file loads fine, it doesn't work well since I assume the Buddypress dependencies it relies on aren't loading properly from that location

1 Answer 1

0

The firts code call a template register.php this file needs to be in the root of your theme, if not are defined a subdirectory for template parts like /parts /addons /includes /templates /loops , etc. The way to call this shortcode is [my_Bp_registration_form] if you change this to 'register' in the add shortcode line, then you can call it [register]. example

function my_BP_registration_form_shortcode( $atts ) {
 ob_start();
 get_template_part( 'register' );
 return ob_get_clean();
}
add_shortcode( 'register', 'my_BP_registration_form_shortcode');

While learning how to use shortcodes try to use less lines and names shorter

function register_form( $atts ) {
 ob_start();
 get_template_part( 'register_form' );
 return ob_get_clean();
}
add_shortcode( 'register_form', 'register_form');

Note that almost has the same name, so on leaning you have less chance to get a mistake; just avoid to use names that your theme has already active for functions. Also your file can be renamed to register_form.php

2
  • Thank you for your reply, the only thing is that looks like the template I want to use is in the plugins folder (because it belongs to Buddypress I assume), how would that affect the code? Or should I copy that template to the child theme I use and call it the way you suggest?
    – Albert
    Commented Dec 24, 2018 at 18:06
  • Update: I tried copying the template file to the theme directory and while it loads the html it doesn't work well because I assume it's not loading all the plugin dependencies correctly, so looks like I need to find a way of loading the template file from its current folder in plugins, not copying it somewhere else
    – Albert
    Commented Dec 24, 2018 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.