Site logo

Archived topic

How can I check the selected sidebar layout?

10 replies · Started by Bob on April 20, 2021

Viewing posts 1–11 of 11

I see examples of how how to set the sidebar layout through code, but how can I CHECK it? In my template, I need to check to see if "No Sidebars" has been selected in the admin in order to display some things differently.

Hi there,

I see examples of how how to set the sidebar layout through code, but how can I CHECK it? In my template, I need to check to see if “No Sidebars” has been selected in the admin in order to display some things differently.

You can check it on Appearance > Customize > Layout > Sidebar.
https://docs.generatepress.com/article/sidebar-layout-overview/

But this isn't always reliable because there may be a Layout element that changes the sidebar for specific pages so you must check Appearance > Elements for any Layout Element for the sidebar settings as well.
https://docs.generatepress.com/article/layout-element-overview/#sidebar

No, I mean check it through PHP at the template level. So that a post type template displays something differently if the admin has set the post to 'no sidebars'.

No, I mean check it through PHP at the template level. So that a post type template displays something differently if the admin has set the post to ‘no sidebars’.

The things I've mentioned are

You can't. This is the closest you can get. https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/sidebars.php

The values are stored from here - ($stored_meta) - to the database.
https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/meta-box.php#L146-L152

On the database, it's stored on wp_options. option_name = generate_settings.

Perhaps you're meaning to ask for the sidebar filter to change how sidebars display on specific custom post types?

So essentially, I'd want to be able to read:

selected( $stored_meta['_generate-sidebar-layout-meta'][0], 'no-sidebar' );

In order to have the template know that on a particular post, the admin/editor has selected 'no sidebar'

I saw the code to SET the sidebar settings with the PHP/template code:

add_filter( 'generate_sidebar_layout', function( $layout ) {
// If we are on a category, set the sidebar
if ( is_category() ) {
return 'no-sidebar';
}

// Or else, set the regular layout
return $layout;
} );

And was thinking that there would be a quick/easy way to do the opposite - determine what has been set in the WordPress admin.

So essentially, I’d want to be able to read:

selected( $stored_meta[‘_generate-sidebar-layout-meta’][0], ‘no-sidebar’ );

No, that's where it's being set. That's just the options.

What you want to see is what setting is stored which is in the database. It’s stored on wp_options. option_name = generate_settings as I've mentioned earlier.

I saw the code to SET the sidebar settings with the PHP/template code:

That filter is used if you want to bypass the customizer and/or layout element.

And was thinking that there would be a quick/easy way to do the opposite – determine what has been set in the WordPress admin.

The quick and easy way is to actually check the customizer what sidebar setting is set or check all the Layout Elements. Because these settings are the ones directly responsible for what sidebar layout is set. This is exactly what I've mentioned on my first reply.

But perhaps I'm missing your point. Can you explain a bit more?

I don't think my question is clear. So the content editors would use this to select the sidebar setup for a particular post, using this:

https://docs.generatepress.com/wp-content/uploads/2019/03/1.8_LE_Sidebar.png

I want the post template to be able to know if the editor selected 'no sidebar' in order to have the template output something a bit differently.

So I'm looking for a PHP code snippet to check what setting was chosen here. Not checking the the customizer or through the admin; I want to be able to have a post template check to see what had been set in the admin, for the particular post it is displaying.

To clarify: You want some sort of a sidebar layout indicator on the post editor page so the content editors will know what sidebar layout is being set on the post they're currently editing?

The value to fetch that is generate_get_layout(). But I'm not exactly sure it can be integrated to the block editor UI. This isn't something the theme controls. It's beyond theme development. It's a WordPress core function.

Check this if you wish to push through with editing post status info for WordPress.
https://developer.wordpress.org/block-editor/reference-guides/packages/packages-edit-post/#PluginPostStatusInfo

Or, if you don't mind an extra metabox, try this code:

/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'gp_sidebar_layout_meta_boxes_setup' );
add_action( 'load-post-new.php', 'gp_sidebar_layout_meta_boxes_setup' );

/* Meta box setup function. */
function gp_sidebar_layout_meta_boxes_setup() {

  /* Add meta boxes on the 'add_meta_boxes' hook. */
  add_action( 'add_meta_boxes', 'sidebar_layout_check' );
}

/* Create one or more meta boxes to be displayed on the post editor screen. */
function sidebar_layout_check() {

  add_meta_box(
    'gp-sidebar-layout',      // Unique ID
    esc_html__( 'GeneratePress Sidebar Layout Checker', 'generatepress' ),    // Title
    'gp_sidebar_layout_checker_meta_box',   // Callback function
    'post',         // Admin page (or post type)
    'side',         // Context
    'default'         // Priority
  );
}

/* Display the post meta box. */
function gp_sidebar_layout_checker_meta_box( $post ) { ?>

  <div style="display: flex; flex-direction: row; align-items: flex-end ;">
    <label for="gp-sidebar-layout-label"><?php _e( "Sidebar layout: ", 'generatepress' ); ?></label>
	  <p style="color: green; margin: 0 0 0 5px;"> <b> <?php echo ' '.generate_get_layout(); ?> </b> </p>
  </div>
<?php }

After adding this code, this will appear in your edit page for posts.
https://share.getcloudapp.com/L1udWqPW

I'm not fully sure it works accurately though.

No, I simply need the post template PHP file itself to be able to know the sidebar layout, as if there is no sidebar some other elements need to display differently. I don't need to involve the block editor UI. So generate_get_layout() seems to be what I need - I'll try it out!

Yes, generate_get_layout() is what I needed. It works fine. Thanks!

No problem. Glad you found what you needed. :)

This archived topic is closed to new replies.