1

So I'm having an issue where I'm getting a 'Undefined function' response and I can't seem to figure out why and how to target it.

Relative path to the shortcode file: htdocs/wp-content/mu-plugins/s/shortcodes/profile.php enter image description here

Relative path to the profile card: htdocs/wp-content/mu-plugins/s/templates/people-card.php
- (This is where I have some HTML)


What I'd like to achieve:

  • Apply the shortcode to the page then have the shortcode function call the profile-card.php page and grab all the content within and display it.

So are the very end, the shortcode will show the 'profile-card.php' page.

The code I have inside shortcodes/profile.php:

function clearline_func() {
    test();
}
add_shortcode('test', 'clearline_func');

I'm getting a Fatal error: Uncaught Error: Call to undefined function test() error.

2
  • 1
    Of course you are. You're using test();, but that function doesn't exist. What are you expecting to happen? – Jacob Peattie Jan 23 '20 at 0:45
  • @JacobPeattie, so I had a test function within the profile-card.php directly but I wasn't getting any returns. What's the best method to include all content from profile-card.php and use that shortcode to show it? – SLE Jan 23 '20 at 0:48
1

Not sure what your test() function is meant to achieve, or if you've even defined it, but yes if you haven't defined it then it's undefined.

In reference to your question: "What's the best method to include all content from profile-card.php and use that shortcode to show it?" The answer is actually in your question, i.e. the best method to include it is to include it, literally. If you haven't included that PHP file, and your test() function is actually defined in there, then that's why it's undefined, because that file was never included.

Granted, I'm somewhat extrapolating what I think you're trying to do based on the information provided, but try this:

include(__DIR__.'/profile-card.php');
function clearline_func() {
    return test();
}
add_shortcode('test', 'clearline_func');

Or if your profile-card.php file is directly producing output, you can capture the output in a variable and then return it from the shortcode callback function. A shortcode callback function should not produce any output, it should always return the desired output.

Something like this:

function clearline_func(){
    ob_start();
    include(__DIR__.'/profile-card.php');
    test(); //-> You can call your test function here or just call it in the included php file.
    $output = ob_get_clean();
    return $output;
}
add_shortcode('test', 'clearline_func');
2
  • Thanks a bunch @KFish, this works like a charm! – SLE Jan 23 '20 at 14:01
  • No problem, glad it worked out. – KFish Jan 23 '20 at 19:48
1

You should have the desired code defined within your clearline_func function like this:

function clearline_func() {
    //copy & paste the profile-card.php code here
}
add_shortcode('test', 'clearline_func');

If you need to keep the profile-card.php file and want to avoid duplicate code then you should use PHP include() to call the file as @KFish suggested above.

1
  • Thanks @Tom! I just wanted to keep it clean and separate the profile-card file with the shortcode since I'll be building out a HTML template there. – SLE Jan 23 '20 at 14:01

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.