Wordpress, Dynamic 404.php title based on url request

Viewed 110

I'm trying to make a dynamic title for my 404.php error page based on the current url request.

Typical URL request

https://URL/topic/4433-app-epic-reader-dla-s8500-s8530?page-3434708

Output

4433 app epic reader dla s8500

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.

1 Answers
  • We get the associated request with $_SERVER\['REQUEST_URI'\].
  • We clean it up to remove any parameters (anything starting from ?).
  • Case handling end of request with or without /.
  • Make a array out of the request by splitting it at each slashes /.
  • Pop out the last value of the array which is our requested page and to some more cleaning to remove any dashes.
  • Call on the front end with wpso66851700().

In your function.php:

<?php 
add_action( 'init', 'wpso66851700' );
function wpso66851700() {
    if ( is_404() ) {
        $request = $_SERVER['REQUEST_URI'];
        if ( str_contains( $request, '?' ) ) {
            $cleaner = substr( $request, 0, strpos( $request, '?' ) );
            $request = $cleaner;
        };
        if ( str_ends_with( $request, '/' ) ) {
            $worker = explode( '/', substr( $request, 1, -1 ) );
        } else {
            $worker = explode( '/', substr( $request, 1 ) );
        };
        return ucfirst( urldecode( str_replace( '-', ' ', array_pop( $worker ) ) ) );  
    };
};
?>

On the front end:

<!-- ... one-liner output-->
<?= wpso66851700(); ?>
<!-- ... one-liner output in a <title> tag-->
<title><?= wpso66851700(); ?></title>
<!-- ... one-liner output in a <h1> tag, with a sentence around it-->
<h1>The "<?= wpso66851700(); ?>" page got lost at sea  and is now swimming with the fishes  !</h1>

<?= means echo. the is_404() condition is passed in the function, no need to repeat it.

As per your example the output should look like:

The "4433 app epic reader dla s8500" page got lost at sea and is now swimming with the fishes !

PHP > 8.0 required due to str_contains() and str_ends_with().


PHP < 8.0, str_contains() and str_ends_with() alternative

<?php
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( 'init', 'wpso66851700' );
function wpso66851700() {
    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 ) );
        };
        return ucfirst( urldecode( str_replace( '-', ' ', array_pop( $worker ) ) ) );  
    };
};
?>
Related