• Earlier, I had this problem that I had a private page, which had to have some child pages, which wordpress dont allow by default. So I added the following code to my functions.php to make private pages available as a parent page:

    function admin_private_parent_metabox($output)
    {
    	global $post;
    
    	$args = array(
    		'post_type'			=> $post->post_type,
    		'exclude_tree'		=> $post->ID,
    		'selected'			=> $post->post_parent,
    		'name'				=> 'parent_id',
    		'show_option_none'	=> __('(no parent)'),
    		'sort_column'		=> 'menu_order, post_title',
    		'echo'				=> 0,
    		'post_status'		=> array('publish', 'private'),
    	);
    
    	$defaults = array(
    		'depth'					=> 0,
    		'child_of'				=> 0,
    		'selected'				=> 0,
    		'echo'					=> 1,
    		'name'					=> 'page_id',
    		'id'					=> '',
    		'show_option_none'		=> '',
    		'show_option_no_change'	=> '',
    		'option_none_value'		=> '',
    	);
    
    	$r = wp_parse_args($args, $defaults);
    	extract($r, EXTR_SKIP);
    
    	$pages = get_pages($r);
    	$name = esc_attr($name);
    	// Back-compat with old system where both id and name were based on $name argument
    	if (empty($id))
    	{
    		$id = $name;
    	}
    
    	if (!empty($pages))
    	{
    		$output = "<select name=\"$name\" id=\"$id\">\n";
    
    		if ($show_option_no_change)
    		{
    			$output .= "\t<option value=\"-1\">$show_option_no_change</option>";
    		}
    		if ($show_option_none)
    		{
    			$output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
    		}
    		$output .= walk_page_dropdown_tree($pages, $depth, $r);
    		$output .= "</select>\n";
    	}
    
    	return $output;
    }
    add_filter('wp_dropdown_pages', 'admin_private_parent_metabox');

    This worked swell.
    However after updating to WP 4.4 and flushing the rewrite rules, it does not work anymore.
    And this seems to be because the child pages now get the wrong permalink.
    In example:

    Second page is a child page of first page.
    Its permalink should be

    mydomain.com/first-page/second-page

    However, now its:

    mydomain.com/second-page

    This ofcourse results in a 404 not found.
    If I manually type in the correct permalink in the browser, it finds the page.

    Does anyone know what is changed, and if I can hook into a filter to overwrite this?

    Thans.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m having the same issue, still haven’t been able to find a fix for this.

    Add this to your functions.php:

    /**
     * Change permalink for private subpages
     */
    add_filter( 'the_permalink', 'private_subpages_permalink' );
    function private_subpages_permalink( $permalink ) {
        if ( get_post_status() === 'private' and $parent_id = wp_get_post_parent_id( get_the_ID() ) ) :
            if ( $parent_id ) :
                $new_permalink = substr( get_the_permalink( $parent_id ), 0, -1 ); // substr removes last slash
                return str_replace( site_url(), $new_permalink, $permalink );
            endif;
        endif;
        return $permalink;
    }

    Note that this snippet uses the “the_permalink” filter, so it means that the link to the page is called with the “the_permalink()” function

    Otherwise there was a shorter way to add private parents:

    /**
     * Add private pages to parent dropdown.
     */
    add_filter( 'page_attributes_dropdown_pages_args', 'dropdown_pages_args_add_parents' );
    add_filter( 'quick_edit_dropdown_pages_args', 'dropdown_pages_args_add_parents' );
    function dropdown_pages_args_add_parents( $dropdown_args, $post ) {
        $dropdown_args['post_status'] = array( 'publish', 'private' );
        return $dropdown_args;
    }

    Adding “Change permalink for private subpages” to the function file doesn’t fix the private page parent problem.

    I already have the private parents showing in the dropdown, as I’m assuming so does olehaugset. This problem only occurs on 4.4 – the permalinks set in the back-end (for example when you are editing a page) doesn’t show the parent nor does it show when you try adding the page as a menu item.

    BUT it does work when you type out the whole URL using the private page as a parent.

    For example:
    Transportation – private page
    Bussing – child page using ‘Transporation’ as a parent (as a dropdown option)

    Permalink being spit out by WordPress 4.4: https://www.domain.com/bussing
    This goes to a 404 page because the actual permalink still needs the parent page.

    So if I went in manually and typed in https://www.domain.com/transportation/bussing – it works fine.

    Hi, same issue here ; On my blog I use the default Menu with sub-menus populated from a private section and a public section. When a public user is on the site a got access to public menus, when a private user is logged in he got access to public+private menus.
    Since 4.4, permalinks are broken on the private side.

    The following function does fix the links, if you call it instead of get_permalinks in “nav-menu.php”. But this is not a definitive solution.

    function get_permalink2( $ID ) {
    		/* $permalink = get_post_permalink($ID); */
    		$permalink=get_the_permalink($ID);
    		if ( get_post_status($ID) === 'private' and $parent_id = wp_get_post_parent_id( $ID ) ) :
    			if ( $parent_id ) :
    				$new_permalink = substr( get_the_permalink( $parent_id ), 0, -1 ); // substr removes last slash
    				return str_replace( site_url(), $new_permalink, $permalink );
    			endif;
    		endif;
    		return $permalink;
    	}

    The issue will have to be fixed in the orignal get_permalink one-day … (link-template.php).

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP 4.4 Permalink on private child pages (save_post)’ is closed to new replies.