1

I know it's easy to choose Front Static page from settings>reading>front page. However, I am looking for a way to have this feature in theme options. Can we do this?

0
1

Since wp 3.4 you could use the Theme Customizer to do this. As far as I know "Static Front Page" is one of the default options of the Theme Customizer.

Check the page about the Appearance Customize Screen at codex for a general introduction. And then maybe Customizer tutorials and documentation could be a useful next step.

enter image description here

2
  • Thanks @tillinberlin Customizer will be a good option. However, I would still want that option (as well as option to set menu) available in my theme options which I am building. Anyway I will update this if I work on any such function. – Ayanize Feb 5 '16 at 12:09
  • Sounds promising – maybe you can let me / us know when you managed to implement those options… thnx! – tillinberlin Feb 5 '16 at 12:13
1

WordPress uses the options API to save these settings. All you need to do is find the setting name and its value. In your case it is as follows:

//Option name - Value

//This corresponds to 'Front page displays'
show_on_front - posts/page

//This corresponds to the Static page: Front page name
page_on_front - <page id>

//This corresponds to the Posts page: Posts page name
page_for_posts - <page id>

All you have to do is make a form that displays the options correctly. You can inspect the form in the WordPress settings page and copy it. The names have to match to the ones I have given above (which they will if you copy WordPress's form). Now when you submit the form, just update the settings as follows:

//If you want the page titled 'Home', with the ID 34 to be the homepage.
update_option('show_on_front', 'page');

update_option('page_on_front', 34);

That's it. Hope this helps.

2
  • Cool. I will try this. I took the long route :P.. he he.. Can this logic be applied to call nav-menus as well? – Ayanize Feb 5 '16 at 12:54
  • Everything WordPress saves, is saved as an option. You can modify all of them. But don't do it unless you know what you're doing. You need to start from the respective settings area in WP dashboard, then go through the code to find the appropriate piece of code, and then examine how the database value changes. – Rutwick Gangurde Feb 5 '16 at 13:33
0

UPDATE

I figured this out. I took help from wp-admin/options-reading.php. Here is the sample code. It's only adding the option to select a page to set as a front page on my theme options.

add_menu_page('Menu Name', 'Menu Name', 'manage_options', 'menu-slug', 'menu_manage_options');

function menu_manage_options(){
    ?>

    <div class="wrap">
<h1><?php echo esc_html( $title ); ?></h1>

<form method="post" action="options.php">
<?php
settings_fields( 'reading' );

if ( ! in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) )
    add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) );
?>

<?php if ( ! get_pages() ) : ?>
<input name="show_on_front" type="hidden" value="posts" />
<table class="form-table">
<?php
    if ( 'posts' != get_option( 'show_on_front' ) ) :
        update_option( 'show_on_front', 'posts' );
    endif;

else :
    if ( 'page' == get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) )
        update_option( 'show_on_front', 'posts' );
?>
<table class="form-table">
<tr>
<th scope="row"><?php _e( 'Front page displays' ); ?></th>
<td id="front-static-pages"><fieldset><legend class="screen-reader-text"><span><?php _e( 'Front page displays' ); ?></span></legend>
        <p><label>
        <input name="show_on_front" type="radio" value="page" class="tog" <?php checked( 'page', get_option( 'show_on_front' ) ); ?> />
        <?php printf( __( 'A <a href="%s">static page</a> (select below)' ), 'edit.php?post_type=page' ); ?>
    </label>
    </p>
<ul>
    <li><label for="page_on_front"><?php printf( __( 'Front page: %s' ), wp_dropdown_pages( array( 'name' => 'page_on_front', 'echo' => 0, 'show_option_none' => __( '&mdash; Select &mdash;' ), 'option_none_value' => '0', 'selected' => get_option( 'page_on_front' ) ) ) ); ?></label></li>

</ul>
<?php if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == get_option( 'page_on_front' ) ) : ?>
<div id="front-page-warning" class="error inline"><p><?php _e( '<strong>Warning:</strong> these pages should not be the same!' ); ?></p></div>
<?php endif; ?>
</fieldset></td>
</tr>
<?php endif; ?>


<?php do_settings_fields( 'reading', 'default' ); ?>
</table>

<?php do_settings_sections( 'reading' ); ?>

<?php submit_button(); ?>
</form>
</div>

    <?php

}
1
  • You don't need to do all of this, since you already have the code. It's a matter of 2 lines only. I'm writing an answer. – Rutwick Gangurde Feb 5 '16 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.