simbasounds / Dynamic-Template-Switchboard-for-Beaver-Builder-and-Dynamik

Custom module for inserting dynamically-generated WordPress template (eg. single.php, archive.php) loop content into Beaver Builder layout templates.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Note: While still useful as an idea, this method has limitations.
Follow this link to a prefered solution.

Dynamic Template Switchboard for Beaver Builder

A custom Beaver Builder module for inserting dynamic content generated by WordPress templates (eg. single.php, archive.php) via php hook into Beaver Builder layout templates.

Instructions are at the end of this Readme page.

Disclaimer: This somewhat experimental code is free to use and fork. Improving on it is highly encouraged. It is not supported in any way.


##Explanation ###The Requirement Beaver Builder can greatly speed up building a website, but out of the box it's always been impossible to use those layouts for WordPress dynamically generate pages - archives (categories, tags, custom taxonomy archives), posts, custom post types.

This has meant that WordPress templates had to be built and styled seperately, potentially more than duplicating the amount of work already performed creating and styling the Beaver Builder templates.

###The Solution The solution is achieved using a combination of a WordPress plugin, and some minor (in most cases) template modifications. The plugin adds a custom module to the Advanced Modules section of Beaver Builder. Usage is as follows:

  • The Module - The Beaver Builder module Dynamic Template Switchboard is dropped into a Beaver Builder layout template where the dynamically generated content should appear.
  • The Template - WordPress template files must be manually created or modified to:
    • Isolate the main loop into a specific action hook, namely fl_dynamic_switchboard.
    • Provide a reference by ID to the specific Beaver Builder template with the add-on.

If all goes smoothly - for every generated instance of a post, archive, custom post type - your template's dynamic content will be rendered within the Beaver Builder template.


##Considerations As per this suggestion, the plugin needs to use the global $wp_the_query object to obtain the ID of the originating post instead of the Beaver Builder template ID.

Some of your template's secondary or custom loop functions may need to be tweaked to work in this context, especially in the case of template functions from other plugins. For example:

function fldts_template_content() {
	global $wp_the_query;
	$real_post_id = $wp_the_query->post->ID; // Get the real ID
	
	/* Events Manager plugin */
	/* Populate the $EM_Event object. */
	/* Events Manager php template tags will work from here onwards. */
	$EM_Event = em_get_event($real_post_id, 'post_id'); 

	
	/* Advanced Custom Fields */
	/* Repeater or flexible content rows loop */
	if( have_rows('my_rows', $real_post_id) ):
		while ( have_rows('my_rows', $real_post_id) ) : the_row();
		endwhile;
	endif;
	
	wp_reset_postdata();
}

##Limitations and Bugs

  • Pagination doesn't work yet (surprise surprise)
  • To prevent it trying, and making a mess of pulling your dynamic content, the plugin detects while a BB template is being edited with the Page Builder, and displays a message instead. It also tries to do this while a BB template is being viewed which it may not get right every time.
  • Currently the template example given in the Gist is tweaked for use with the Dynamik Website Builder (which in turn requires Genesis). For other themes template files can easily be constructed around this generic example, also at the bottom of this Readme file.

##Currently Researching / Roadmap

  • Better wrapping of template code (possibly via cloning $wp_the_query object). Hoping for a solution which solves Pagination and plugin code execution quirks.
  • Accurate post type, and editor mode detection, perhaps by using native FLBuilderModel:: functions.
  • May look at a template selection interface at some point.
  • Maybe some way of grabbing the content area of a template without modifying it.

##Not yet tested Shortcodes from other plugins.


##Other Solutions


##Instructions

  1. Install the plugin (requires Beaver Builder).
  2. Create a Beaver Builder template.
  3. Use the Dynamic Template Switchboard module (in Advanced Modules) where you want your content to appear.
  4. Find, and take note of the ID of the Beaver Builder template.
  5. Modify your WordPress template files, or create one using the example template Gist for Dynamik, or the generic WordPress barebones version. In short, the template's loop should be attached to the fl_dynamic_switchboard hook.
  6. In your template, use the Beaver Builder layout template ID as indicated in the example Gist.
<?php

add_action('fl_dynamic_switchboard', 'my_template_code');
function my_template_code() {
	/* Your template loop code here */
}

add_action('fldts_fl_template','fldts_get_fl_template');
function fldts_get_fl_template() {
	FLBuilder::render_query( array(
		'post_type' => 'fl-builder-template',
		'p'         => 1234 /* Your BB Template ID here */
	) );
}

get_header();
do_action( 'fldts_fl_template' );
get_footer();

About

Custom module for inserting dynamically-generated WordPress template (eg. single.php, archive.php) loop content into Beaver Builder layout templates.


Languages

Language:PHP 100.0%