2

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.

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

1 Answers1

1
  • 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 ) ) ) );  
    };
};
?>
amarinediary
  • 4,930
  • 4
  • 27
  • 45
  • 1
    Hi, thanks a lot, but I want it to be dynamic (I mean those are just the examples of url syntax in different versions - url is different for topic, different for forum, different for profile). Sorry for misunderstanding... – spinecki Mar 29 '21 at 12:12
  • 1
    I have a small request on top - how to implement this to use it for page title (the one between tags)? I mean probably "if" and "else", but hell with my php knowledge... :/ – spinecki Mar 29 '21 at 13:01
  • I want to put output of this in the page title. If page is 404, the title should be as well the same (the exploded url) and not "404 page not found". Anyway, I have to debug your code as it gives no output at all... – spinecki Mar 29 '21 at 13:16
  • 1
    mod_fcgid: stderr: PHP Fatal error: Uncaught Error: Call to undefined function str_contains() in I guess it's time for php 8.0... – spinecki Mar 29 '21 at 13:25
  • 1
    Any chance for function without php 8.0's str_contains? It seems that my server does not have and I need to put it offline for like 3 hours to make it work (which is impossible because of other things I got there). – spinecki Mar 29 '21 at 14:09
  • 1
    Now it's: Uncaught Error: Call to undefined function str_ends_with() in wp-content/themes/twentyeleven/functions.php:939 – spinecki Mar 29 '21 at 15:10
  • A now it does not give an error, but it gives BLANK output. Nothing there. – spinecki Mar 29 '21 at 16:06
  • 1
    Wait. ECHO. Ok, works :) Thank you very much! – spinecki Mar 29 '21 at 16:10
  • I added an update in the question - should i start new topic? Is it doable at all or it's far more complicated? – spinecki Mar 29 '21 at 16:41
  • I have another problem, that I did not expect. Not of all urls end with "?page", some of them end with forward slash "/". Could you please help? – spinecki Mar 31 '21 at 10:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230586/discussion-between-amarinediary-and-spinecki). – amarinediary Mar 31 '21 at 10:41