0

I'm used to creating custom shortcodes through my child theme and Elementor, never a problem.

However this time something is wrong, when I inserted the shortcode widget in Elementor editor my code is correctly showing, but after saving I visit the page, its blank, like the shortcode was never processed.

Anyone have any idea of why this is happening?

This is the code where i create the shortcode:

    <?php 
    add_shortcode('test', function () {
    ob_start();
    require_once (locate_template('views/test.php'));
    return ob_get_clean();
});

Inside the file views/test.php a simple form, nothing crazy.

This is the screen of Elementor editor where the shortcode is showing:

enter image description here

enter image description here

If i visit the page it's completely blank.

5
  • Could you provide more details, the code you used, maybe some screenshots as to what happens. Aug 12, 2021 at 8:22
  • 1
    there is no shortcode to debug in your question. Note that Elementor and other 3rd party dev support is off topic on this stack
    – Tom J Nowell
    Aug 12, 2021 at 8:47
  • @Tom I updated the post, i didn't know about the 3rd party thing.
    – Sorius_45
    Aug 12, 2021 at 8:58
  • is it just the shortcode missing on the page, or is it everything? If it's everything it sounds like the classic white screen of death, check your PHP error log
    – Tom J Nowell
    Aug 12, 2021 at 10:18
  • Can you post the code that you have in views/test.php?
    – Mat
    Aug 20, 2021 at 14:58

2 Answers 2

1

The problem is with the require_once, you are passing a relative path and that path doesn't exist.

Assuming the test.php file you want to require is in this path

wp-content/themes/your-theme/views/test.php

You can use the following

require_once locate_template('views/test.php');

Consider using include instead of require_one, if this shortcode is not 100% necessary for your site to function, its best to use include as this way you will not receive a fatal error because that file doesn't exist.

3
  • @Sorius_45 Could you update your question with the new code? Aug 12, 2021 at 10:44
  • Updated, i see your point but i don't get why the shortcode is showing just fine in Elementor editor.
    – Sorius_45
    Aug 12, 2021 at 10:47
  • Should work, all testing that i have done on my local env worked as expected. I assume the path is somehow incorrect. Aug 12, 2021 at 10:49
0

I find out the solution. Thanks for all the answers.

This is the code now:

add_shortcode('test', function () {
    ob_start();
    include('views/test.php');
    return ob_get_clean();
});

Now it shows up on frontend, i don't know why with require_once was not working, but if i use include it works fine.

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.