12

So, I followed the accepted answer of this post Possible to add another setting to 'Front page displays' setting for Custom Post Type

but didn't entirely work, had to compare against the accepted answer in this post.

Can't output do_settings_sections . Can't understand why

Now, it actually works using the following code

//Add settings to menu
add_action( 'admin_menu', 'events_options_add_page' );
function events_options_add_page() {
//  add_submenu_page('edit.php?post_type=events', 'Events Admin', 'Events Settings', 'edit_posts', basename(__FILE__), 'events_options_do_page');
    add_submenu_page('edit.php?post_type=events', 'Events Admin', 'Events Settings', 'edit_posts', basename(__FILE__), 'events_options_do_page');
}
//https://wordpress.stackexchange.com/questions/32689/possible-to-add-another-setting-to-front-page-displays-setting-for-custom-post


//Fix Cap to allow editors to edit theme options
function events_get_options_page_cap() {
    return 'edit_posts';    
}
add_filter('option_page_capability_events-options', 'events_get_options_page_cap' );


function events_options_do_page() {
?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2><?php _e( 'Events Options', 'FoundationPress' ); ?></h2>
        <form action="options.php" method="post">
            <?php
            settings_fields( "events_settings_options" );
            do_settings_sections( "edit_posts" );
            ?>
            <?php submit_button( __( "Save changes", "FoundationPress" ), "primary", "submit", true ); ?>
        </form>
    </div>
    <?php
}

//Register Settings
add_action( 'admin_init', 'events_settings_register' );

function events_settings_register() {
    register_setting( "events_settings_options", "events_settings_options", "events_settings_validate" ); //Register main settings
//  add_settings_section( "events_cpt_settings", __( "Events Page Settings", "FoundationPress" ), "events_settings_dummy", "edit_posts"  ); //Make settings text section
//  add_settings_field( "events_settings_cpt_page", __( "Page for events", "FoundationPress" ), "events_settings_page_select", "edit_posts", "events_settings_options" );
    add_settings_section( "events_cpt_settings", __( "Events Page Settings", "FoundationPress" ), "events_settings_dummy", "edit_posts"  ); //Make settings text section
    add_settings_field( "events_settings_cpt_page", __( "Page for events", "FoundationPress" ), "events_settings_page_select", "edit_posts", "events_cpt_settings" );
}

function events_settings_dummy() {}

//Validate Settings
function events_settings_validate ($input) {
    $valid = get_option ( "events_settings_options" );
    $valid['events_page'] = (int) $input['events_page'];
    return $valid;  
}

//Settings Fields
function events_settings_page_select() {
    $options = get_option( 'events_settings_options' );
    wp_dropdown_pages(
        array(
            'name' => 'events_settings_options[events_page]',
            'echo' => 1,
            'show_option_none' => __( '&mdash; Select &mdash;' ),
            'option_none_value' => '0',
            'selected' => $options['events_page']
        )
    ); 
}

To get it to work I noticed differences between the two examples, and changed do_settings_sections to edit_posts... now in my mind the purpose is for permissions or capabilities, I have tried other bits but just comes up with You do not have permissions etc in wordpress.

Could anybody help to make some sense of my code please? I have gone around in circles and would massively appreciate another set of eyes going over it.

After this I then need to make it so that the page actually works just like setting pages in the Reading page, in order to load my custom post type archives.

Many thanks guys!

EDIT:

Having to bump, desperately need some help on this! Happy to paypal a beer over for a fix!

  • Can anybody help at all? – Luke Wale Jul 29 '15 at 16:41
  • it should work. i tested your code. or do you need help with anything else here? – iantsch Mar 16 '16 at 16:53
  • Reading your question I'm not sure what you need help with but here's an answer that allows you to assign a Page Template as an Archive page – Howdy_McGee Jun 16 '16 at 1:38
  • Is you goal simply to have a custom template for a custom post type? – Slam Dec 13 '17 at 16:21
1

There is a dirty (actually dirty as hell) way to attach a ordinary WordPress page as archive page from a custom post type settings page.

You need to unregister post type first, and create again. If you don't use custom arguments or if you brave enough try this code.

Add this code fragment end of your settings page.

if(isset($_GET['settings-updated']) && $_GET['settings-updated'] == true){
    //Dirty as hell
    $esettings = get_option ( "events_settings_options" );
    $getslug = get_post_field('post_name',$esettings['events_page']);

    unregister_post_type('events');
    register_post_type('events',array(
        'public' => true,
        'rewrite' => array('slug'=>'events'),
        'has_archive' => $getslug,
    ));
    //Needed for automated rewrite rules
    flush_rewrite_rules();
    //To test the functionality
    echo get_post_type_archive_link( 'events' );
}
  • 1
    This made me feel dirty just reading it, but I'm impressed. – MikeNGarrett Mar 27 at 15:33

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.