BuddyDev

Search

[Resolved] When I set a child page on the members page, it becomes 404

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #24580

    Hi everyone,

    I want to put a child page on “members” just like “fruits”.
    Below is the code for that.

    
    
    /*
    	Specify default child page
    ==========================================*/
    
    // This is the default page to connect to when the child page link is empty
    function redirect_default_child() {
    	$url = explode( '/', $_SERVER["REQUEST_URI"] );
    	// fruits
    	if( ( isset( $url[1]) && $url[1]=='fruits' ) && ( !isset($url[3]) || $url[3]==null ) ){
    		$redirect = get_home_url().'/fruits/'.$url[2].'/comments/';
    		wp_redirect( $redirect );
    		exit;		
    	}
    	// members
    	else if( ( isset($url[1]) && $url[1]=='members' ) && ( !isset($url[3]) || $url[3]==null ) ){
    		$redirect = get_home_url().'/members/'.$url[2].'/timeline/all/';
    		wp_redirect( $redirect );
    		exit;				
    	}	
    }
    add_action( 'wp', 'redirect_default_child' );
    
    /*
    	Generate links to child pages
    ==========================================*/
    
    // fruits has child pages and members has grandchild pages.
    function rewrite_init() {
    	// fruits
    	add_rewrite_rule( 
    		'^fruits/([^/]+)/(comments|participants)?$', 
    		'index.php?fruits=$matches[1]&fruitschild=$matches[2]', 
    		'top'
    	);
    	// members
    	add_rewrite_rule( 
    		'^members/([^/]+)/(profile|friends)?$', 
    		'index.php?pagename=members&membersname=$matches[1]&memberschild=$matches[2]', 
    		'top'
    	);	
    	add_rewrite_rule( 
    		'^members/([^/]+)/profile/(introduce|status)?$', 
    		'index.php?pagename=members&membersname=$matches[1]&memberschild=profile&membersgrandchild=$matches[3]', 
    		'top'
    	);
    	add_rewrite_rule( 
    		'^members/([^/]+)/friends/(followees|followers)?$', 
    		'index.php?pagename=members&membersname=$matches[1]&memberschild=friends&membersgrandchild=$matches[3]', 
    		'top'
    	);
    }
    add_action( 'init', 'rewrite_init' );
    
    function rewrite_query_vars_perf( $query_vars ) {
    	// fruits
    	$query_vars[] = 'fruitschild';
    	// members
    	$query_vars[] = 'membersname';
    	$query_vars[] = 'memberschild';
    	$query_vars[] = 'membersgrandchild';
    	return $query_vars;
    }
    add_action( 'query_vars', 'rewrite_query_vars_perf' );
    
    /*
    	Specify a template for each page
    ==========================================*/
    
    function rewrite_template_include_perf( $template ) {
    	// fruits
    	if( my_is_fruits() ) { 
    		$child = get_query_var( 'fruitschild' );
    		if( $child == 'comments' || $child == 'participants' ) {
    			$template = locate_template( 'single_fruits1.php' );
    		}	
    		else{
    			$template = locate_template( 'single_fruits2.php' );
    		}
    	}
    	// members
    	else if ( my_is_members() ) {	
    		$grandchild = get_query_var( 'membersgrandchild' );
    		if( $grandchild == 'introduce' || $grandchild == 'status'  ){
    			$template = locate_template( 'page_members1.php' );
    		}		
    		else if( $grandchild == 'followees' || $grandchild == 'followers'  ){
    			$template = locate_template( 'page_members2.php' );
    		}		
    	}	
    	return $template; 
    }
    add_filter( 'template_include', 'rewrite_template_include_perf', 99 ); 
    
    

    The “fruits” is normal, but the “members” will show “now 404!” if I write the following in header.php:

    
    <head>
    	<meta charset="<?php bloginfo( 'charset' ); ?>">
    	<meta name="viewport" content="width=device-width">
    	<?php wp_head(); ?>
    </head>
    
    <?PHP
    if( is_404() ) { echo 'now 404!'; }
    else{ echo 'ok'; }
    ?>
    
    <body <?php body_class(); ?>>
    

    By the way, I don’t want to use bp-costom.php because I want to unify the processing with the “fruits” and “members”.

    Where can I fix 404 in the above code?

    Kind regards,
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #24596

    Hi maimaimemain,
    BuddyPress haas a complex url parsing routine( it will most probably be replaced in 5.0).

    My suggestion will be to use BuyddyPress’s api for adding the sub pages

    Please see
    https://codex.buddypress.org/developer/function-examples/core/bp_core_new_nav_item/

    and

    https://codex.buddypress.org/developer/deprecated/bp_core_add_subnav_item/

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #24603

    Hi Brajesh,

    You just said it!
    But I want to unify the processing with the “fruits” and “members”.

    Since “fruits children” are added by add_action ('init', 'rewrite_init');, I want to do the same for “members children”.

    Regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #24635

    Hi,
    I am sorry . I regret my inability to help you with this due to lack of time and the scope of this work.

    If you strongly feel that you need this code, Please feel free to utilize our custom services(It costs $90/hour).

    https://buddydev.com/buddypress-custom-development-service/

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #24642

    Oops!

    Thank you for asking me, butI requested my senior for this work for $200.( ノД`)

    I knew for the first time that I was able to request on this site.
    I look forward to the next opportunity.

    Thanks
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #24643

    No issues.
    Time is the most important commodity. We are glad to provide free help here as long as it within certain limits.

    Otherwise, we suggest the custom service option.

    Marking it resolved.

    Regards
    Brajesh

The topic ‘ [Resolved] When I set a child page on the members page, it becomes 404’ is closed to new replies.

This topic is: resolved