getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
632 stars 94 forks source link

Any plans/thoughts on how to support adding and hooking into posts? #111

Closed dryajov closed 8 years ago

dryajov commented 8 years ago

From what I can see, there is no support for programmatically adding posts with Herbert, are there any plans to add that? Thoughts on how it should be done?

In terms of how it should work, I think panels are exactly what I'm looking for, but obviously they don't work with posts. My use case is basically rendering pages inside an existing theme, for that I believe that the best approach is to use custom posts/pages, and render content from a controller/view.

Any thoughts/suggestions on how to go about it are highly appreciated. I'll also try to come up with a quick implementation based on the existing panels implementation.

dcalvert commented 8 years ago

I've not done anything with posts etc, but I would guess that you would create a route to your page i.e /news, create a controller and method to route to i.e NewsController@list. In the list method you could use Capsule to grab the Posts you need and then push them through to the view.

dryajov commented 8 years ago

I've tried that, and the problem there is that you have to pull in HFS manually, and that ends up messing up the theme. Thats at least the issue I've ran into, maybe I'm missing something and there is another approach, but taking over pages and customizing content is still a pretty common use case that many plugins rely on, it be nice to support that in the framework.

jgarib commented 8 years ago

Since custom post types get handled / rendered by wordpress I solved this by creating a "Template" directory within the "App" folder, then in my definition of custom post types (app/customPostTypes.php) I added this code:

\ replace {posttype} with the name of your custom post type **

function wp39847_load_template($single_template) {
    global $post;

    if ($post->post_type == '{posttype}') {
        $single_template = dirname( __FILE__ ) . '/Templates/single-{posttype}.php';
    }
    return $single_template;
}
add_filter( "single_template", "wp39847_load_template");

I then created a file within the new Template directory which has a call to my shortcode within the loop:

do_shortcode("[SHORTCODE id='$id']");

My controller has $id as a parameter for the method and I query the singular post type there, pass it to the view which allows me to use twig to cleanly structure my markup.

Using this approach I was able to keep my plugin completely decoupled from my theme. You could expand upon the function that loads the template to allow users to override your template from the theme directory, but since it just calls a shortcode to render the view within the theme it's not really necessary.

dryajov commented 8 years ago

@jgarib I ended up using shortcodes as well, except that I simply created pages/posts with the short code as it's contnent, and not messingg with the template, which allows me to integrate functionality with any theme. You can do that for virtually any post, page or custom post type.