Adding a template part as part of a shortcode

I am using "Ultimate Member" plugin to allow users to register because I want some content to be restricted fo

I am using "Ultimate Member" plugin to allow users to register because I want some content to be restricted for not signed-in users. According to their documentation, there are a few shortcodes for that and I'm trying to sue one in a template like this:

<?php
    $out_content = get_template_part( 'templates/template-parts/loggedout', 'content' );
    do_shortcode("[um_loggedout] {$out_content} [/um_loggedout]");
?>

<?php
    $in_content = get_template_part( 'templates/template-parts/loggedout', 'content' );
    do_shortcode("[um_loggedin] {$in_content} [/um_loggedin]");
?>

Actually, both template parts are output this way, is this something that can be done?

I am using "Ultimate Member" plugin to allow users to register because I want some content to be restricted for not signed-in users. According to their documentation, there are a few shortcodes for that and I'm trying to sue one in a template like this:

<?php
    $out_content = get_template_part( 'templates/template-parts/loggedout', 'content' );
    do_shortcode("[um_loggedout] {$out_content} [/um_loggedout]");
?>

<?php
    $in_content = get_template_part( 'templates/template-parts/loggedout', 'content' );
    do_shortcode("[um_loggedin] {$in_content} [/um_loggedin]");
?>

Actually, both template parts are output this way, is this something that can be done?

Share Improve this question asked Oct 30, 2019 at 15:52 SergiSergi 2601 gold badge6 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

get_template_part doesn't return its value, it outputs it, which is how it would normally be used. Think of it this way, how would it know wether to echo the template or return it?

So instead, we use output buffers to catch the output and flush it to a variable, e.g.:

ob_start( );
echo "test";
$output = ob_get_clean();

Further Notes

  • Avoid embedding variables directly in PHP strings like this: "variable: {$variable} " instead use concatenation: " variable:".$variable. It's not possible to sanitise of escape if you embed in a string directly
  • Shortcodes are a fancy way of calling a function, since you're in PHP rather than post content, why not skip the middle man and call the function directly?
  • You've closed a PHP tag, then reopened it but there's nothing between the tags, save yourself the effort of typing it out and keep all your PHP together in one block
  • Since you're only checking if a user is logged in or out, there's no need for shortcodes and output buffers at all, just use is_user_logged_in() with an if (...) { .... } statement.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042607a4607901.html

相关推荐

  • Adding a template part as part of a shortcode

    I am using "Ultimate Member" plugin to allow users to register because I want some content to be restricted fo

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信