$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom HTML title based on URL parameter|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom HTML title based on URL parameter

matteradmin9PV0评论

Basically, we have alot of traffic coming to our site from different sources looking for slightly different things.

But instead of creating new pages for each slight change in heading, we were wondering if there was a way to set a custom parameter within the URL and then pull that parameter into a HTML element to display it.

So for example:

User 1 gets sent to:

URL: www.example/something?title=Page_1

Therefor the heading:

Heading one: Page 1

User 2 gets sent to the same page, but with a different parameter:

URL: www.example/something?title=Page_3

Therefore the heading:

Heading Two: Page 3

Thanks guys. I have a rough understanding of how this would be done but i'm not all too familiar with WPs Hooks.

But from my knowledge you'd set a custom parameter, store that parameter into a variable then display that variable. But I know it isnt that simple xD

Basically, we have alot of traffic coming to our site from different sources looking for slightly different things.

But instead of creating new pages for each slight change in heading, we were wondering if there was a way to set a custom parameter within the URL and then pull that parameter into a HTML element to display it.

So for example:

User 1 gets sent to:

URL: www.example/something?title=Page_1

Therefor the heading:

Heading one: Page 1

User 2 gets sent to the same page, but with a different parameter:

URL: www.example/something?title=Page_3

Therefore the heading:

Heading Two: Page 3

Thanks guys. I have a rough understanding of how this would be done but i'm not all too familiar with WPs Hooks.

But from my knowledge you'd set a custom parameter, store that parameter into a variable then display that variable. But I know it isnt that simple xD

Share Improve this question edited Mar 18, 2019 at 21:40 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 18, 2019 at 10:13 RazeyRazey 31 silver badge4 bronze badges 2
  • You just want the page title to display whatever is in the URL parameter, like the <title> tag, or the h1 or both? – Howard E Commented Mar 18, 2019 at 10:40
  • Sorry, I would like to display it in the content so it would be a H1 tag. – Razey Commented Mar 18, 2019 at 10:45
Add a comment  | 

3 Answers 3

Reset to default 1

Another approach that may be a bit more opaque to your users is to look at the referrer_url. This is a server-level variable that, while not 100% reliable, is generally a good indicator of where someone was when they clicked your link.

It would not work if someone copied the link and sent it to someone else.

function wpse331937_custom_referer_title( $title ){

    if ( wp_get_referer() ){
        $host = parse_url( wp_get_referer(), PHP_URL_HOST );

        switch ( $domain ){
            case 'google' : 
            case 'www.google' : 
                    $title = 'Hello Google Users';
                    break;
            case 'cincinnati.craigslist' :
                    $title = 'Hello, Cincinnati!';
                    break;
            default :
                    break;
        }
    }

    return $title;       

}
add_filter( 'the_title', 'wpse331937_custom_referer_title', 10, 1 );

So the big advantage here is that it's automatic, but like I said the referer is not going to capture every case.

The other big benefit is you don't have to have the page title as part of the URL, which looks a bit awkward.

You could get a similar benefit but without the referer piece by using the same switch structure in another way. For instance:

function wpse331937_custom_title( $title ){

    if ( isset( $_GET['ref'] ) && $_GET['ref'] ){
        $ref = $_GET['ref']

        switch ( $ref){
            case 'google' : 
                    $title = 'Hello Google Users';
                    break;
            case 'cin-craig' :
                    $title = 'Hello, Cincinnati Craigslist Users!';
                    break;
            default :
                    break;
        }
    }

    return $title;       

}
add_filter( 'the_title', 'wpse331937_custom_title', 10, 1 );

So now your URL looks like

www.example/something?ref=cin-craig

Instead of

www.example/something?title=Hello,%20Cincinnati%20Craigslist%20Users!

The answer is one thing, but there isn't a specific hook for placing the title, as it's dependent upon your theme, and where it is located in the theme/page template.

First, you'll have to find the the_title() tag as it's used in your template files.

Then it's simply.

<?php 

$title = (isset($_GET['title'])) ? $_GET['title'] : the_title();
echo '<h1>' . $title . '</h1>';

You will need to find the correct location in your theme though. This code will use the page title if the URL param is not set.

In page.php or the php file of currently used template replace the_title() or echo get_the_title() with this

if( isset( $_GET['title'] ) ) echo  $_GET['title'];
else the_title();

or add this to functions.php

function modified_title( $title, $id = null ) {

    if ( is_page( ) && isset( $_GET['title'] ) ) {

         return  $_GET['title'];
    }
    else{
        return $title;
    }

}
add_filter( 'the_title', 'modified_title', 10, 2 );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far