
Add meta boxes on Front Page only without template
I am rewriting one of my old pages into WordPress theme and I needed some meta boxes. But for front page only. I don’t need them to be show on any other page. It isn’t very hard to do, you just need to check template meta. But I don’t want to use template. I got front-page.php
file to view front page and that’s it, I don’t need specialized template. So, what to do? It’s pretty easy.
First, we need to hook into meta-box action. It’s simple and well known, but… Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace Phylax; class Theme { function __construct() { add_action( 'add_meta_boxes_page', [ $this, 'add_meta_boxes_page' ] ); } function add_meta_boxes_page() { } } |
Next step is also easy as pie. We need to compare current object ID with page id set as front page. We can do this like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Phylax; class Theme { function __construct() { add_action( 'add_meta_boxes_page', [ $this, 'add_meta_boxes_page' ] ); } function add_meta_boxes_page() { global $post; $front_id = get_option( 'page_on_front' ); if ( $post->ID != $front_id ) { return; } # now you may add meta box here! } } |

