1

I am trying to dynamically insert a new post when a user reach a 404 page.

If the request is:

https://supportsamsung.pl/forum/1197-pomoc-pytania-problemy/

Then, the post title should be:

1197 pomoc pytania problemy

With a category set to forum, and the user should be redirected to the post.

It only needs to handle 3 categories. topic, profile and forum. (in polish, temat, profil forum).

Would anybody know a plugin that could be used for that ? If not, could anyone please help me with this issue ? I have tried but couldn't figure it out.

amarinediary
  • 4,930
  • 4
  • 27
  • 45
spinecki
  • 33
  • 5

1 Answers1

0

We can insert a new post via wp_insert_post(). We can couple that with, is_404() to know if the current page we're on is a 404 page.

Then we need to study the request to know if it match our criteria. We can fetch the request via $request = $_SERVER['REQUEST_URI'];. We need to isolate the page, which should be the last request /1197-pomoc-pytania-problemy/, and the category /forum/, which is the prior to last one.

We can redirect the user to the post via wp_safe_redirect() and capturing the post ID upon publishing.

Tested and working.

function endsWith( $needle, $haystack ) { //... @credit https://stackoverflow.com/a/834355/3645650
    $length = strlen( $needle );
    if( ! $length ) {
        return true;
    };
    return substr( $haystack, -$length ) === $needle;
};

add_action( 'wp', 'insert_new_post_on_404_request' );

if ( ! function_exists( 'insert_new_post_on_404_request' ) ) {

    function insert_new_post_on_404_request() {

        if ( is_404() ) {

            $request = $_SERVER['REQUEST_URI'];

            if ( strpos( $request, '?' ) !== false ) {
                $cleaner = substr( $request, 0, strpos( $request, '?' ) );
                $request = $cleaner;
            };
            if ( endsWith( '/', $request ) ) {
                $worker = explode( '/', substr( $request, 1, -1 ) );
            } else {
                $worker = explode( '/', substr( $request, 1 ) );
            };

            $current_category = $worker[count( $worker )-2];

            $current_title = urldecode( str_replace( '-', ' ', array_pop( $worker ) ) );  


            if ( ( strpos( $request, '/topic/' ) !== false )
            || ( strpos( $request, '/profile/' ) !== false )
            || ( strpos( $request, '/forum/' ) !== false ) ) {

                if ( ! get_page_by_title( $current_title ) ) {

                    $cat_id = get_category_by_slug( $current_category )->term_id;

                    $postarr = array(
                        'post_title' => $current_title,
                        'post_status' => 'publish',
                        'post_type' => 'post',
                        'post_category' => array(
                            $cat_id,
                        ),
                    );
    
                    $post_id = wp_insert_post( $postarr );

                    wp_safe_redirect( get_post_permalink( $post_id ) );

                    exit;
    
                };

            } else {

                return;

            }; 

        };

    };

};

Some things to take into account:

  • Category terms (topic, forum and profile) need to exist prior to initialize the function for the first time.
  • The function will only insert posts from a fail request, on a 404.php page, if 1 of those 3 category a present in the url.
  • If a request is made for a child page, it will likely create a post not attach to any category. the function can't understand that it is supposed to be a child page, so I don't see any way to handle those type of request.

Additionally, translation should be handle on it's own. either through a plugin or else. You can also rewrite each category name and slug. This is based on your theme initial development; Not much we can do here.

amarinediary
  • 4,930
  • 4
  • 27
  • 45