Get ID of the WordPress ‘Front Page’ in Theme’s functions.php

Problem: I wanted to add some Meta Boxes to the homepage only, and have them only visible in WP-Admin when editing that page.

Solution:  Provided that a page has been selected in wp-admin > General Settings > Reading Settings > A Static Page [Front Page], then this code can be used to pull that value out as a Page ID:

1
<?php echo get_option('page_on_front');?>

So to use that in code snippet one might do:

1
2
3
4
5
6
7
function add_page_metaboxes() {
  $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
  if ($post_id == get_option('page_on_front')) {
    add_meta_box('meta_page_features', 'Features', 'meta_page_features', 'page', 'normal', 'low');
  }
}
add_action('add_meta_boxes', 'add_page_metaboxes');

Leave a Reply

Your email address will not be published. Required fields are marked *