最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove sidebar on single page using plugin API

programmeradmin1浏览0评论

I’m building a plugin that requires a full width page for a large table. I want to control the sidebar on that page regardless of the theme being used. How do I either, remove the sidebar programmatically OR change the template for that page programmatically?

I’m building a plugin that requires a full width page for a large table. I want to control the sidebar on that page regardless of the theme being used. How do I either, remove the sidebar programmatically OR change the template for that page programmatically?

Share Improve this question asked Sep 6, 2019 at 13:07 Matthew BrewerMatthew Brewer 1 1
  • 2 There's no guaranteed way to remove a sidebar regardless of theme, but you can control the template with a filter. You may want to create your own template that pulls in the theme header, a content area, and the theme footer, so that you can rely on those elements and not worry about what templates each individual theme may or may not have. wordpress.stackexchange/questions/3396/… – WebElaine Commented Sep 6, 2019 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 2

How do I either, remove the sidebar programmatically OR change the template for that page programmatically?

You can't. Different areas on the frontend and how they're styled are at the discretion of the person who coded the theme. There's no standard WP framework for how to put them together and determine the layout. It could be anything from CSS floats, Bootstrap grids, Flexbox, etc

There may be no sidebars at all, there may be 3, you have no way to know

A naive approach might be to do something like this:

.sidebar, #sidebar { display: none; }

But this has lots of problems:

  • the sidebar might be named .right-sidebar
  • there may be a left and right sidebar
  • it would catch a footer sidebar, or a sidebar inside a block
  • it might eliminate the sidebar but the content area is still the same size
  • it might break the layout of the page

But all of this is moot because even if you could do this, your large data wouldn't work on mobile.

Additionally, if you could do what you're wanting to do, the page content would now be full screen width

How Do Other Plugins Get Around The Need For Theme Templates?

WooCommerce for example provides its own templates, but these have to fit as best they can into the theme, and that doesn't always work. A developer can copy these into the theme and customize them

What You Should Do

Large tables on small screens/areas shouldn't require that your plugin hack the theme. Instead there are other alternatives:

  • Open the data in a new page that doesn't use the theme
  • Use CSS to make it work, e.g. tables that are scrollable, or having it pop out of the content area on hover to fill the entire screen width.

Look up responsive HTML tables, and you'll see lots of ideas. Google Docs spreadsheets are essentially massive tables of data too, yet they can be resized and embedded. Data that's too wide or tall is still shown, you just have to scroll

Using a sledgehammer to force it to work by sawing off parts of a theme won't work, and there's no API for what you're trying to do.

发布评论

评论列表(0)

  1. 暂无评论