flyntwp / flynt

Component based WordPress starter theme, powered by ACF Pro and Timber, optimized for a11y and fast page load results.
https://flyntwp.com
MIT License
728 stars 83 forks source link

Connecting an application (plugin) to Flynt #249

Closed szepeviktor closed 4 years ago

szepeviktor commented 4 years ago

Hello!

I'm planning implement our application as a WordPress plugin plus some Composer packages and use Flynt.

What would you suggest to send content (HTML output) from the plugin to Flynt? I'd like something better (something OOP) than shortcodes or actions. What do you have to offer?

Cc: @Jony-Shark

domtra commented 4 years ago

Hi @szepeviktor , sorry for the late reply. I tried answering a couple of times before, but was never happy with what i was going to write.

flynt is a starter theme based on timber, as you know. so we use timber's way of rendering routes, not the wordpress loop. besides that we do not make any assumptions. we do not force you to use or facilitate writing more oop or any other style. we still use wordpress actions a lot. with that, you are free to do whatever you want with your plugin.

if you have a specific question, feel free to further elaborate.

szepeviktor commented 4 years ago

@domtra Thank you for your struggle!

My simple question is: I generate a section of the HTML page in my plugin (thus the application), how to send it (as a string) to Flynt with minimal application-specific code in the theme?

domtra commented 4 years ago

ok @szepeviktor, i still think there are a couple of scenarios for that. let me try to cover two of those.

if you want the html from your plugin to be included in a certain wordpress template, you only need to consider timber/twig functionalty. for example, add to templates/page.twig directly a function call like {{ fn('generatePluginHtml') }}. this style you would probably not prefer, so you could also add code to page.php like

$myPlugin = new \My\Plugin();
$context['customHtml'] = $myPlugin->generateHtml();

and then include {{ customHtml }} at some position in template/page.twig.

For a more component based solution, so that the content manager, for example, can include the generated html at any position in the page/post components, you would have to create a new component with a functions.php and an index.twig.

these two files would need to contain something like

functions.php

namespace Flynt\Components\BlockPluginHtml;

add_filter('Flynt/addComponentData?name=BlockPluginHtml', function ($data) {
    $myPlugin = new \My\Plugin();
    $data['customHtml'] = $myPlugin->generateHtml();

    return $data;
});

function getACFLayout()
{
    return [
        'name' => 'blockPluginHtml',
        'label' => 'Block: Plugin Html',
        'sub_fields' => [],
    ];
}

index.twig

{{ customHtml }}

to make this component available for the content editor, you would need to add Components\BlockPluginHtml\getACFLayout() to the respective field group in inc/fieldGroups.

I hope this answers you question. let me know if anything remains unclear.

szepeviktor commented 4 years ago

Thank you for starting me up!