0

I am trying to add content to what would otherwise be a 404 page. For example I want to define post content for mysite.com/existing-page/imaginary-page instead of showing the 404 page. I've tried several different approaches and none work, so I greatly appreciate your help. My actual structure is this: mysite.com/personal-development/class. mysite.com/personal-development exists but mysite.com/personal-development/class does not. mysite.com/personal-development/class will display altered content from mysite.com/personal-development

if seems like add_rewrite_rule would work if the class page actually existed.

add_action( 'init', 'dynamic_classes_class_type_rewrite' );
function dynamic_classes_class_type_rewrite()
{
    global $wp_rewrite;
    add_rewrite_tag( '%personal-development%', '([^&]+)' );
    add_rewrite_rule(
        '^personal-development/([^/]*)/?',
        'index.php?page_id=17318&class_type=$matches[1]',
        'top'
    );    
}

This one catches the page and cancels the 404. It then shows the home page content but does not use the template style-sheets or allow me to change the content. The example below is my attempt to get the appropriate content and display it. I also tried displaying a different template. I was able to display the template, but was still not able to display any content.

add_filter('template_redirect', 'dynamic_classes_class_type_redirect' );
function dynamic_classes_class_type_redirect() 
{
//for best seo I am setting up a redirect for pages.
    global $wp, $wpdb, $wp_query;
    if(!$post_title=get_post($post)->post_title)//looks like we hit a 404
    {
        $url=explode('/',dynamic_classes_page_url());//see if its one of our dynamically created pages.
        if(count($url)>4)//we need to make sure this long enough
        {
            $db_prefix= $wpdb->prefix;
            $sql='SELECT ID
                  FROM '.$db_prefix.'posts
                  WHERE post_name=\''.implode('',array_slice($url,-2,1)).'\' /*get the second to last*/
                  LIMIT 1';
            $rs_class = $wpdb->get_results($sql);
            if($rs_class)
            {
                status_header( 200 );
                $wp_query->is_404=false;
                wp_reset_query();
                setup_postdata(get_post($rs_class[0]->ID));
            }
        }
    }
}

add_action and 'the_post' will allow me to alter content as I need to, but not for 404s - even if I cancelled the 404 as in the above example.

add_action( 'the_post', 'my_the_post_action' );
function my_the_post_action( $post_object ) {
    $post_object->post_content='Programmer test.';
}
0

In your first example, you add the rewrite tag personal-development, but then you use the rewrite tag class_type inside your rewrite rule. That method would work if you fixed that error, and assuming 17318 is the page id of your personal-development page. You could then use get_query_var('personal_development') in the template or a template hook, to check what's been requested.

add_action( 'init', 'dynamic_classes_class_type_rewrite' );
function dynamic_classes_class_type_rewrite()
{
    add_rewrite_tag( '%personal_development%', '([^&]+)' );
    add_rewrite_rule(
        '^personal-development/([^/]*)/?',
        'index.php?page_id=17318&personal_development=$matches[1]',
        'top'
    );    
}
  • I tried your suggestion but only got a 404. Can you elaborate on the template hook? My goals is to either have the url mysite.com/personal-development/class process equivalently to mysite.com/personal-development/?class_type=class where I can change the post content before it displays, or be able to alter the 404 post content before it displays. 17318 is the personal-development page id. Thank you – Praesagus Oct 4 '13 at 21:22

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.