2

is there a way to loop through a shortcode data source created in a custom plugin like followed within a 3d party frontend design plugin?

function bs_test_fnc(){
        $aData = array(
            array('a','b','c'),
            array('a','b','c'),
            array('a','b','c'),
        );
        return $aData;
    }
add_shortcode('bs_resource_table_datasource_test','bs_test_fnc');

Explaination:

The data tables in my plugin are not custom post types though I was wondering if I could somehow generate a propper dynamic table content with this data using some of the design plugins. But these pro plugin versions all cost a lot of money and they seem not to be able to attach to shortcodes in the way I was thinking this might work.

So how do you do this?
Do you force all your plugin data into custom post types in order to have it accessible to other plugins? Or is there another way?

I have of course created plain HTML tables and displayed my data through shortcodes to test backend functionality but this seems more like a misuse of PHP since I normally use templating engine frameworks (with other CMS). On top of that, this would probably be really hard to design and display properly like this. Especially not quick or repeatable and I am trying out WordPress to be able to do quick and pretty.

I'd appreciate it if you could explain or link to me a proper use case, how one would do this "templating" like in a template engine with custom WordPress plugins either in cooperation with 3d party plugins or use of a templating engine or other WordPress tools.

The taxonomies make this really hard to search for and identify the proper way to do this with WordPress.

1 Answer 1

1

I found a solution working for me by following this tutorial

In Short:

  1. Create new file in plugin and copy Gamajo Template Loader Class

  2. Create another file, creating your own class extension of Gamajo

    <?php
    
    /**
     * Template loader for PW Sample Plugin.
     *
     * Only need to specify class properties here.
     *
     */
    class PW_Template_Loader extends Gamajo_Template_Loader {
    
    
    
    /**
     * Prefix for filter names.
     *
     * @since 1.0.0
     * @type string
     */
    protected $filter_prefix = 'pw';
    
    /**
     * Directory name where custom templates for this plugin should be found in the theme.
     *
     * @since 1.0.0
     * @type string
     */
    protected $theme_template_directory = 'pw-templates';
    
    /**
     * Reference to the root directory path of this plugin.
     *
     * @since 1.0.0
     * @type string
     */
    protected $plugin_directory = PW_SAMPLE_PLUGIN_DIR;
    

    }

  3. Add this to your plugin

    <?PHP define( 'PW_SAMPLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    
    require PW_SAMPLE_PLUGIN_DIR . 'class-gamajo-template-loader.php';
    require PW_SAMPLE_PLUGIN_DIR . 'class-pw-template-loader.php';
    
    function pw_sample_shortcode() {
    
    
    
    $templates = new PW_Template_Loader; 
    }
    add_shortcode( 'pw_sample', 'pw_sample_shortcode' );
    
  4. Create templates folder in your plugin root

  5. Create template file.php in your plugins templates folder containing the following example

    <h2><?php echo $data->section_title;?></h2>
    

Now you can access templates as following and load them with shortcode into frontend design plugins or your own custom themes while using your data to fill them

function pw_sample_shortcode() {
 
    $templates = new PW_Template_Loader;
 
    $args = array( 'section_title' => 'hello world' );
    ob_start();
    $templates->set_template_data($args); // assign variable array before calling templates
    $templates->get_template_part( 'content', 'header' );
    $templates->get_template_part( 'content', 'middle' );
    $templates->get_template_part( 'content', 'footer' );
    return ob_get_clean();
 
}
add_shortcode( 'pw_sample', 'pw_sample_shortcode' );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.