user registration - How to load specific template page in a custom shortcode

admin2025-01-07  3

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

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

Share Improve this question edited Dec 25, 2018 at 19:54 Albert asked Dec 22, 2018 at 18:29 AlbertAlbert 1411 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252965a89.html

最新回复(0)