-1

I have created a wordpress shortcode which outputs a form. The following is my shortcode

function testcode(){
   echo "<h1>TEST</h1>";
}

add_shortcode("test-code","testcode");

What i want is to be able to include the <h1>Test</h1> in a file such that users who want to override the shorcode can simply create a new file.

So something like

  function testcode(){
      //load html from a a file like templates/test.php
  }
 add_shortcode("test-code","testcode");

How do i proceed to make this reusable and extendable?

2
  • which users do you mean would be able to simply create a new file? Commented Apr 1, 2020 at 8:21
  • @gael the above code is located in a plugin and i would like for plugin users to have the capbility to override the file by simply creating a folder with the file in it. Commented Apr 1, 2020 at 8:52

1 Answer 1

2

you can add a file parameter to the shortcodeload a separate file.

function testcode( $atts = [], $content = null, $tag = '' ){
     // normalize attribute keys, lowercase
     $atts = array_change_key_case((array)$atts, CASE_LOWER);
 
     // override default attributes with user attributes
     $atts = shortcode_atts(
        [
            'file' => 'default.php',
        ],
        $atts, 
        $tag
    );
    ob_start();
    
        $contents = '';

    // echo "/some/path/{$atts['file']}";
        if ( str_ends_with( $atts['file'], '.php' ) {
            require "/some/path/{$atts['file']}";
            $contents = ob_get_contents();
            ob_end_clean();
    }

        return $contents;
    
}
add_shortcode("test-code","testcode");

echo do_shortcode( '[test-code file="sagar"] ');

Reference: https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ https://developer.wordpress.org/reference/functions/do_shortcode/

Sign up to request clarification or add additional context in comments.

5 Comments

Including it via require "/some/path/{$atts['file']}"; limits users from overriding the template. My shortcode is located in a plugin and i want users to be able to overide this in the theme later by creating a folder with a specific name and the file which should automatically override it.
@Geoff Then what you are seeking is the template override functionality similar to WooCommerce. Then you need to implement 3 functions similar to WooCommerce, they are wc_get_template_part, wc_get_template and wc_locate_template. You can find those functions here github.com/woocommerce/woocommerce/blob/master/includes/…
@Geoff Let me know if you have any problem.
A word of warning .. This code will require/include ANY valid file. Even none-PHP files. It might be better to have an allowlist of paths / files and/or use basename and also do a check ( if file_exists ) first.
Thanks for the suggestion, updated the code.

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.