Filter fl_builder_global_posts - two issues

Hi,

I’m embedding page builder pages within other page builder pages using the filter ‘fl_builder_global_posts’ and I’ve found a couple of issues.

  1. First issue - All posts (pages) were getting registered when I was only trying to register 1. What I found is if I changed the 1.6.0.1 code from:
// Enqueue assets for posts via the fl_builder_global_posts filter.
  $post_ids = FLBuilderModel::get_global_posts();

  if(count($post_ids) > 0) {

    $posts = get_posts(array('post__in' => $post_ids, 'post_type' => 'any'));

    foreach($posts as $post) {
      self::enqueue_layout_styles_scripts($post->ID);
    }
  }

  // Reset the global post variable.
  $post = $original_post;

TO:

// Enqueue assets for posts via the fl_builder_global_posts filter.
  $post_ids = FLBuilderModel::get_global_posts();

  if(count($post_ids) > 0) {
    foreach($post_ids as $post) {
      self::enqueue_layout_styles_scripts($post->ID);
    }
  }

  // Reset the global post variable.
  $post = $original_post;

I’m not sure what the purpose of this line is, when you already have the post_ids in an array?

$posts = get_posts(array('post__in' => $post_ids, 'post_type' => 'any'));

  1. Second issue - After making the above change, my page builder layouts are working when the page is embedded, but not all of the styles are being applied. For example, the 2 column layout is being applied, but colors specified for headers are showing the default instead of the chosen color.

Here’s my function used to register the posts:

function my_global_builder_posts($post_ids) {
$post_ids[] = ‘129’;
return $post_ids;
}
add_filter(‘fl_builder_global_posts’, ‘my_global_builder_posts’);

Thanks for your help!

Hi Floyd,

We need the $posts = get_posts() line because it sets the global $post variable that is used throughout the code. That’s why we have the $post = $original_post; line, to reset the global $post variable.

FLBuilderModel::get_global_posts is actually only returning an array of post IDs, so $post->ID actually isn’t working in your script since that array doesn’t contain any objects.

Can you revert that change and post the code you are using to embed builder pages so we can take a look at what’s going on there?

Thanks,
Justin

Hi Justin,

Ok, I’ve reverted the change to the class-fl-builder.php file, but it didn’t change anything related to the embed styling.

As for how I’m displaying the posts within a post. It’s a little complicated. I’m using Bootstrap tabs as sub-navigation on parent pages. So, the code checks to see what pages are ‘children’ of the current page, then puts those into tab-content divs for display when a user clicks on the appropriate tab. Here’s a portion of the code. I can give you the entire template if that’s helpful. I can also give you access to the site which is under development.

// WP_Query arguments
    $args = array (
        'post_parent'            => $post->ID,
        'post_type'              => 'page',
        'orderby'                  => 'menu_order',
        'order'                  =>  'ASC',
    );

    // The Query
    $query = new WP_Query( $args );
    // The Loop
    if ( $query->have_posts() ) {
        $count = 0;
        while ( $query->have_posts() ) {
          $query->the_post();
          if ($count == 0) {
            echo '<div id="' . $query->post->ID .'" class="tab-pane active">';
          }
          else {
            echo '<div id="' . $query->post->ID .'" class="tab-pane">';
          }
          the_content();

Also, the way that I’m registering the child pages with the builder filter (which seems to be working) isn’t as simple as I posted above. Here’s that code (when I echo the $post_ids array here it returns what I expect on a given page):

function my_global_builder_posts( $post_ids ) {
  $args = array(
    'post_type' => 'page',
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-with-child-nav.php',
  );
  
  $parents = get_pages($args);
  
  foreach($parents as $parent) {
    $child_args = array(
      'sort_order' => 'asc',
      'sort_column' => 'post_title',
      'hierarchical' => 0,
      'exclude' => '',
      'include' => '',
      'meta_key' => '',
      'meta_value' => '',
      'authors' => '',
      'child_of' => $parent->ID,
      'parent' => -1,
      'exclude_tree' => '',
      'number' => '',
      'offset' => 0,
      'post_type' => 'page',
      'post_status' => 'publish'
    ); 
    $page_children = get_pages($child_args);

    foreach($page_children as $child){
      $post_ids[] = $child->ID;
    }
  }
  return $post_ids;
}
add_filter( 'fl_builder_global_posts', 'my_global_builder_posts' );

Thanks!

Thanks, Floyd! This is helpful. Looking at the code, there may be a few things going on here…

  1. fl_builder_global_posts loads in the CSS/JS assets for each post in the $post_ids array on every page. You’ll need to add specific logic to your my_global_builder_posts function if you only want the assets loading on specific pages.

  2. Have you checked the contents of your $page_children array to make sure get_pages isn’t returning all pages? If assets for every page are being loaded, it’s possible you have an issue there.

Let me know.

Justin

Hi Justin,

  1. We do only want the assets loading on the parent page, which is where the child page is embedded. How do I add that logic?

  2. I just verified that $page_children is only returning the child pages which are embedded. But, we have a lot of them, and this may get a little big larger before we launch. Currently, there are 56 child pages, which load under 5 different parent pages.

Also, if I comment out ‘return $post_ids;’ from the my_global_builder_posts, then the embedded pages all go to one column (when most are two in the page builder), and the builder divs and classes are missing. When I add it back, I get code like this for my 2 columns (on the embedded page) -

<div class="fl-col fl-col-small fl-node-55897de627432" data-node="55897de627432" style="width: 50%;">

Hi Floyd,

1) We do only want the assets loading on the parent page, which is where the child page is embedded. How do I add that logic?

You can do that by adding a condition that checks for the parent post ID like this…

global $post;

if ( $post->ID != $parent->ID ) {
    return $post_ids;
}
else {
    // Run your logic here.
}

That’s just a quick example, you may need to adjust it for your specific needs.

Also, if I comment out ‘return $post_ids;’ from the my_global_builder_posts, then the embedded pages all go to one column (when most are two in the page builder), and the builder divs and classes are missing. When I add it back, I get code like this for my 2 columns (on the embedded page) –

That’s most likely happening because the styles aren’t loading when $post_ids is commented out.

Justin

Hi Justin,

Thanks again for your help! I got the first part working - I updated my code to only add the filter (and load assets) when I’m on a parent page.

The second part is that I’m getting part of the builder style on the embedded page, not all of it. As I said above, the column width (style=“width: 50%;”) is getting added to the div, but the colors and margins for the embedded elements are not getting applied. It’s like the CSS isn’t loading, but the portion that controls the inline style is. Do you have any ideas on how to fix this?

Thanks!

It sounds like the CSS for the child pages isn’t loading. Can you shoot me a link to a page where they should be?

[Content Hidden]

Hey Floyd,

It looks like assets are loading, but not for the correct posts…

<link rel='stylesheet' id='fl-builder-layout-39-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/39-layout.css?ver=67f0d9db8b833514ff328b947ba372fb' type='text/css' media='all' />
<link rel='stylesheet' id='fl-builder-layout-1733-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/1733-layout.css?ver=2708664aac79eb353cb9396e14c58f07' type='text/css' media='all' />
<link rel='stylesheet' id='fl-builder-layout-1732-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/1732-layout.css?ver=12fc8eece9ece1a2e1702c4f11bf0980' type='text/css' media='all' />
<link rel='stylesheet' id='fl-builder-layout-1731-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/1731-layout.css?ver=eb89cba12127b6a79fe05636200dc907' type='text/css' media='all' />
<link rel='stylesheet' id='fl-builder-layout-1698-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/1698-layout.css?ver=cb13b65915d5e3ab670d281e0f1d650d' type='text/css' media='all' />
<link rel='stylesheet' id='fl-builder-layout-1588-css'  href='http://wonderroot.wpengine.com/wp-content/uploads/bb-plugin/cache/1588-layout.css?ver=1b356cdcf887d1a8ec3e1b6907f2332d' type='text/css' media='all' />

For example, I see the post with the Chris Appleton text has a post ID of 124 but no assets for that post are loading (as shown above). You might need to check your my_global_builder_posts logic to ensure that it’s pulling in the correct post IDs.

Justin

Hi Justin,

I see that 39 is the ID for the about page. The the other IDs - 1733, 1732, 1731, 1698, 1588 are for the most recently added Tabs/Child pages (Awards, Careers, etc.) for the About page, so it seems to be partially working. Is there a limit on the number of embedded stylesheets that will be loaded?

Thanks!

Also, if you go to the Staff tab for Stephanie Kong, her name/header is in blue, so styles are loaded for her page, which uses the same Page Template as the other Staff pages. It does appear to be loading styles just for sub-set of the pages.

Hi Floyd,

Nope, no limit! The post id just needs to exist in the $post_ids array for the assets to be loaded :slight_smile:

You might try logging data in your function to see if it is.

Justin

[Content Hidden]

Hi Floyd,

Thanks for the detailed follow up.

I guess I still don’t understand why we need this if all we are doing is passing the $post->ID to enqueue_layout_styles_scripts, and we already have them from get_global_posts().

We need that since many parts of the builder use WordPress’ global $post variable which is overridden with the current global post being worked with. You can see methods being called in the enqueue_layout_styles_scripts method such as FLBuilderModel::get_asset_info or FLBuilderModel::get_asset_version that relay on that.

It does look like something is causing those posts not to be pulled in the get_posts call but I can’t imagine what that would be. Would you mind providing temporary admin access for me to poke around?

I’m still a little new to WordPress though, so maybe that’s the problem. :)

It looks like you know WordPress pretty well :slight_smile:

Thanks,
Justin

Hi Justin,

I think I figured it out. I Googled “get_posts not returning all posts” and found this:

http://wordpress.stackexchange.com/questions/7830/why-get-posts-are-only-showing-five-posts-retrieved-by-assigning-a-category-to

I then changed the get_posts line to:

$posts = get_posts(array('post__in' => $post_ids, 'post_type' => 'any', 'numberposts'=> -1));

Now, on the about page on staging, you’ll see it’s showing the same post IDs in both debug outputs.

Bingo! I can’t believe I didn’t think of that. Sorry for the hassle and thanks for your perseverance. I’ll go ahead and get that fix in the next update.

Justin

Thanks for your help Justin!