• A portion of my website is used by another non-profit organization. In order to restrict them from editing/deleting at large, I created a custom post type for them, and added a user role to go with it, using the excellent model presented by @webelaine

    It basically works: they can get into their custom post type in the backend and create/edit pages, they are the author, they can’t get into my pages, and basically all good.

    A couple of odd things that may or may not be theme-related:

    1) Although they are the author of the pages they create, there is no “Author” section of the Document tab in the editor, in case I wanted to turn a page over to them or vice versa. When I list all pages of the special type, and do a Quick Edit, there is an author drop-down, but they are not in it, even for pages where they are author.

    2) In the editor, Documents tab, there is no “Layout” section that allows using sidebars or not (for them; there is for me). Maybe because that is controlled by GeneratePress, which is unaware of this post type?

    3) Related to #2, the default layout for posts (real posts) is being used for the custom post types. Is there a way I can make it follow the layout for pages instead (single column)?

    4) I have the following in functions.php to set the footer. Is there a filter I could use to change it based on the custom post type, or on the path including the slug for the custom post type?
    Here is the plugin that does the custom post type and user role.

    // Generate copyright text in footer of each page
    add_filter( 'generate_copyright','fp_custom_copyright' );
    function fp_custom_copyright() {
        ?>
        &copy; <?php echo date( 'Y' ) ?> forestpathology.org
        <?php
    }

    5) Similarly, for the heading, I’d like to change the Site Title for such pages to their organization

    6) I can probably figure this out eventually, but I would need to hide the main navigation menu for their pages also.

    In case it matters, here is the plugin that sets up the custom post type and a user for them restricted to it:

    <?php
    /* Plugin Name: fp custom post types
    */
    
    /********************************************************/
    /*   Function to create the custom post type 'ncpage'   */
    /********************************************************/
    
    function fp_create_post_type() {
        // Capabilities of custom post type
        // First three are meta capabilities
        $capabilities = array(
            'edit_post'                 => 'edit_ncpage', 
            'publish_post'              => 'publish_ncpage', 
            'delete_post'               => 'delete_ncpage', 
            'publish_pages'             => 'publish_ncpages',
            'edit_pages'                => 'edit_ncpages',
            'edit_published_pages'      => 'edit_published_ncpages',
            'edit_others_pages'         => 'edit_others_ncpages',
            'edit_private_pages'        => 'edit_private_ncpages',
            'delete_pages'              => 'delete_ncpages',
            'delete_private_pages'      => 'delete_private_ncpages',
            'delete_published_pages'    => 'delete_published_ncpages',
            'delete_others_pages'       => 'delete_others_ncpages',
            'read_private_pages'        => 'read_private_ncpages'
        );
        
        // Set backend UI labels for Custom Post Type
        $labels = array(
            'name'                => ( 'NCpages' ),
            'singular_name'       => ( 'NCpage' ),
            'menu_name'           => ( 'NCFPW Pages' ),
            'parent_item_colon'   => ( 'Parent NCpage' ),
            'all_items'           => ( 'All NCpages' ),
            'view_item'           => ( 'View NCpage' ),
            'add_new_item'        => ( 'Add New NCpage' ),
            'add_new'             => ( 'Add New' ),
            'edit_item'           => ( 'Edit NCpage' ),
            'update_item'         => ( 'Update NCpage' ),
            'search_items'        => ( 'Search NCpage' ),
            'not_found'           => ( 'Not Found' ),
            'not_found_in_trash'  => ( 'Not found in Trash' )
        );
    
        // Add other CPT arguments
        $args = array(
            'labels'                => $labels,
            'capabilities'          => $capabilities,
            // Features this CPT supports in Post Editor
            'supports'              => array ( 
                      'title', 'editor', 'comments', 'revisions', 
                      'author', 'page-attributes', 
                      'thumbnail', 'custom-fields', 'post-formats' ),
            'description'           => 'NCFPW pages for corresponding user role',
            'map_meta_cap'          => true,
            'capability_type'       => 'ncpage',
            // Set rewrite to desired URL, otherwise
            // it defaults to true and uses $post-type as slug (ncpage)
            'rewrite'               => array('slug' => 'ncfpw'),
            // Other arguments
            // show_ui, show_in_nav_menus, and publicly queryable default to $public, 
            // show_in_menu defaults to $show_ui
            // show_in_admin_bar defaults to $show_in_menu
            'public'                => true,
            'menu_position'         => 4,
            'hierarchical'          => true,  // can have parent and child, like pages
            'show_in_rest'          => true,  // block editor support
            'can_export'            => true,
            'exclude_from_search'   => false
        );
        
        // Actually create the post type.  First is the post-type name ($post_type)
        register_post_type('ncpage', $args);
    }
    
    /************************************/
    /*        Call the function         */
    /************************************/
    
    add_action('init', 'fp_create_post_type');
    
    /*********************************************************/
    /* Add user role, access to ONLY ncpage and own images   */
    /*********************************************************/
    function fp_add_ncfpw_role() {
    	add_role('ncfpw_author', 'NCFPW Author', array(
    		// Custom capabilities
    		'publish_ncpages'          => true,
    		'edit_ncpages'             => true,
    		'edit_others_ncpages'      => true,
    		'edit_private_ncpages'     => true,
    		'edit_published_ncpages'   => true,
    		'delete_ncpages'           => true,
    		'delete_published_ncpages' => true,
    		'delete_others_ncpages'    => true,
    		'delete_private_ncpages'   => true,
    		'read_private_ncpages'     => true,
    
    		// Allow to read and upload files
    		'read' => true,  // access to Dashboard and user profile
    		'upload_files' => true,  // see and add Media files
    		'delete_posts' => true  // allows to delete media files they uploaded
    	));
    }
    // This makes it run only once at plugin activation
    // __FILE__ is the full path to this php file.
    register_activation_hook( __FILE__, 'fp_add_ncfpw_role' );
    
    /********************************************************/
    /*            Admin access to ncpage                    */
    /********************************************************/
    
    function fp_add_admin_caps() {
        $role = get_role( 'administrator' );
        $role -> add_cap( 'publish_ncpages' );
        $role -> add_cap( 'edit_ncpages' );
        $role -> add_cap( 'edit_others_ncpages' );
        $role -> add_cap( 'edit_private_ncpages' );
        $role -> add_cap( 'edit_published_ncpages' );
        $role -> add_cap( 'delete_ncpages' );
        $role -> add_cap( 'delete_published_ncpages' );
        $role -> add_cap( 'delete_others_ncpages' );
        $role -> add_cap( 'delete_private_ncpages' );
        $role -> add_cap( 'read_private_ncpages' );
    }
    
    add_action('admin_init', 'fp_add_admin_caps');

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Theme Author Tom

    (@edge22)

    Hi there,

    1. I believe this would be in the setup for your custom post type. It does look like you’ve added author in the supports section though, so it should be working. The theme itself doesn’t prevent this functionality.

    2. The metabox only displays to users with the edit_theme_options capability. You can change that using a filter:

    add_filter( 'generate_metabox_capability', function() {
        return 'something-else';
    } );

    3. Since WordPress sees them as posts and not pages, it will use the posts option. There’s a filter for this as well: https://docs.generatepress.com/article/sidebar-layout/#using-a-function

    4. Absolutely, you can do this:

    if ( is_singular( 'post-type-name/slug' ) ) {
    
    }

    The above condition will be useful for the sidebar layout filter mentioned above as well.

    5. You could try filtering the core WP option:

    add_filter( 'option_blogname', function( $name ) {
        if ( is_singular( 'post-type-name/slug' ) ) {
            return 'Their name';
        }
    
        return $name;
    } );

    6. You can control your menu items in “Appearance > Menus”. The page should only display if you add them to the menu.

    Hope this helps!

    Thread Starter Jim

    (@jwmc)

    Thanks for the great tips.

    I’m finding that putting this stuff in functions.php isn’t working. Experimenting, I think the post type is only available inside “the loop”. All attempts to display the post type from functions.php failed, but it did work when I put it in a single-ncpage.php template (ncpage being the custom post type).

    functions.php is not getting executed in the loop:

    if ( in_the_loop() ) {
           echo 'in the loop';
           } else {
           echo 'Not in the loop';
        }

    displays ‘Not in the loop’.

    Should I put it in single-ncpage.php instead? Actually, if I did, the test using “if ( is_singular( ‘ncpage’ ) )” would be unnecessary.

    • This reply was modified 5 years, 8 months ago by Jim.
    Theme Author Tom

    (@edge22)

    The functions I mentioned above don’t need to be in the loop. They can all go into your child theme functions.php file.

    If they’re not working, it means the is_singular( 'post-type-name/slug' ) isn’t correct. Just to confirm, we’re trying to target the single posts of this custom post type, right?

    Thread Starter Jim

    (@jwmc)

    That’s odd. If I put the following in the single-ncpage template, it displays ‘ncpage’. If I put it in the child functions.php, nothing happens:

    $post_type = get_post_type();
    echo $post_type;

    I think these are single posts, though I’m not 100% clear what that is. One of the pages of ncpage is served with this:
    <body class="ncpage-template-default single single-ncpage postid-6025 single-format-standard logged-in admin-bar wp-embed-responsive right-sidebar nav-below-header fluid-header one-container active-footer-widgets-0 nav-search-enabled nav-aligned-left header-aligned-left dropdown-hover customize-support" itemtype="https://schema.org/Blog" itemscope="">
    So one of the classes is single-ncpage.

    Theme Author Tom

    (@edge22)

    So this should be your conditional:

    is_singular( 'ncpage' )

    Is that what you’re using in your functions?

    Thread Starter Jim

    (@jwmc)

    Yes, exactly. Here is what I currently have (and doesn’t do anything) in functions.php

    if ( is_singular( 'ncpage' ) ) {
    
    	// Custom copyright in footer
    	add_filter( 'generate_copyright','ncfpw_custom_copyright' );
    	function ncfpw_custom_copyright() {
    		?>
    		&copy; <?php echo date( 'Y' ) ?> North Central Forest Pest Workshop
    		<?php
    	}
    	
    	// Change site name for heading on their pages
    	add_filter( 'option_blogname', function( $name ) {
    		return 'NCFPW';
    	} );
    
    }
    • This reply was modified 5 years, 8 months ago by Jim.
    Theme Author Tom

    (@edge22)

    Ah, that won’t work.

    You need to do this:

    add_filter( 'generate_copyright', function() {
        if ( is_singular( 'ncpage' ) ) {
            $copyright = '&copy; ' . date( 'Y' ) . ' North Central Forest Pest Workshop';
        }
    
        return $copyright;
    } );
    	
    // Change site name for heading on their pages
    add_filter( 'option_blogname', function( $name ) {
        if ( is_singular( 'ncpage' ) ) {
            return 'NCFPW';
        }
    
        return $name;
    } );
    Thread Starter Jim

    (@jwmc)

    Oh my gosh, this stuff is way more mysterious than I thought. You are amazing.

    I added an } else { to the copyright function to set the copyright for the rest of the site. I found if I set it for the whole site, and then set it conditionally for ncpage, the ncpage copyright just gets concatenated to the main copyright instead of replacing it.

    Regarding the nav menu, what I really hope to do is remove that entirely for ncpages. Should I do that with css, “display: none” when it’s in a single-ncpage class, or is there a more efficient way?

    Thanks so much. Although I don’t have a lot of visitors, I’m going to put “Powered by GeneratePress” into the footer and buy GP Premium, maybe it’ll help.

    Theme Author Tom

    (@edge22)

    If hiding it with CSS works for you, you can do this:

    .single-ncpage #site-navigation {
        display: none;
    }

    Otherwise, there is a filter for that too.

    Thank you! Always happy to help πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Customizing Custom Post Type’ is closed to new replies.