0

I am creating a WordPress plugin that can display parts of a user profile page using shortcodes. This solution would allow creating a user profile page with a page builder shortcode widget.

So far I have understood how to echo simple text with a shortcode.

function simpletext() {
$text = 'simple text';

echo $text;

}

add_shortcode('text-shortcode', 'simpletext');

Thanks to the above function I can use [text-shortcode] to echo "simple text".

Using same strategy, I would like to echo/display div element - <div class="um_profile_container> on the user profile page that is powered by same template that keeps - <div class="um_profile_container> element.

function profilecontainer() {
$profilecontainer = '<div class="um_profile_container">';
echo $profilecontainer;

}

add_shortcode('profile-container', 'profilecontainer');

However, unlike with simple text, the above function does not display this div element on the frontend when [profile-container] shortcode is added to the page via page builder (Elementor in my case).

What am I missing?

2
  • Have you tried return $profilecontainer; instead of echo $profilecontainer;? Commented Mar 6, 2021 at 19:26
  • Yes, I did. return and echo worked the same way. Commented Mar 18, 2021 at 8:16

2 Answers 2

0

A possible solution for you would be to enable output buffering using ob_start, so you can write your html code freely.

<?php
function profilecontainer() {
    ob_start(); ?>
    
    <div class="um_profile_container">Your Content</div>

    <?php return ob_get_clean();
}
add_shortcode('profile-container', 'profilecontainer');
?>
Sign up to request clarification or add additional context in comments.

Comments

0

It turns out that the function actually does display this div element on the frontend when [profile-container] shortcode is added to the page via page builder (Elementor in my case).

The problem was that the div element had no hight or width so it was displayed as a simple line - div element borders close together. To test this out you have to set high and width with CSS.

.um_profile_container {
    width: 100px;
    height: 100px;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.