In the past, the approach for creating the BPC is to first create a page template using the page builder. Then copy and paste the ‘text’ of the template to the text box of the BPC Widget.
In this new approach, a shortcode is introduced. It can be entered into the text box of the BPC widget or in the text block of concerned pages. The shortcode points to the BPC page ID so no more copy and paste is required.
Procedure:
- Create the BPC template as usual. Append the title with “- don’t delete”, and publish the page with visibility set to Private.
- In functions.php, add the following codes:
/** ^^^
* Shortcode for Bottom Page Content
*
* Format: [invr_show_bpc pid="999"]
* The page ID of the page which contains the BPC is passed
* to the shortcode as an attribute.
*
*/
function show_bpc($atts) {
$a = shortcode_atts(array('pid' => null), $atts);
$pid = $a['pid'];
if ($pid == null) {
echo "<b>Error: BPC page_id is missing. Required format [invr_bpc pid='page_id']</b><br>";
return;
}
if (get_post_status($pid) == false) {
echo "<b>Error: BPC page_id $pid doesn't exist !!</b><br>";
} else {
$the_post = get_post($pid);
$the_content = apply_filters('the_content', $the_post->post_content);
echo $the_content;
} // end if-else
return;
} // end show_bpc()
add_shortcode('invr_show_bpc', 'show_bpc');
/* End: Shortcode for Bottom Page Content */
- Add the shortcode to the text box of the BPC Widget. The format is [invr_show_bpc pid=”999″], where 999 is to be replaced by the BPC template page ID.
- Enable the widget on pages that require the BPC.
- The shortcode can also be applied at anywhere in the page builder.
Code Overview:
- A pid parameter is passed in from the shortcode [invr_show_bpc pid=”999″].
- The ‘add_shortcode’ statement triggers the callback function ‘show_bpc’.
- The show_bpc( ) function receives an array called $atts. This is an associative array which contains all the passed in parameters. Note: the function can also receive $content and $tag if these are required. See the reference below.
- The ‘shortcode_atts( )’ is a WordPress built-in function. It catches the passed in parameters and assigns default values to them. This function returns an associative array with the passed-in key and value pair. If the key has no value, the default is assigned.
- In order to echo the content on the web page, the apply_filters(‘the_content’, $content) is used. Without this, the web page will display a series of text codes.
Reference:
