0

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.com/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.com/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

  • 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 Mar 18 at 10:40
  • Sorry, I would like to display it in the content so it would be a H1 tag. – Razey Mar 18 at 10:45
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.com' : 
            case 'www.google.com' : 
                    $title = 'Hello Google Users';
                    break;
            case 'cincinnati.craigslist.org' :
                    $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.com/something?ref=cin-craig

Instead of

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

  • Thank you for the quote edits @Jacob – tmdesigned Mar 18 at 14:45
0

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.

  • Tried adding this code however there is an issue on the first line, there is an unexpected ; however when removed it brings up another issue. – Razey Mar 18 at 11:18
  • Sorry i forgot the close paren on the isset – Howard E Mar 18 at 11:20
0

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 );
  • I tried adding this code, however when no parameter is set the page loads just fine however if there is a parameter added the page comes up empty with only the parameter being displayed. Screenshot: link – Razey Mar 18 at 11:14
  • Apparently there is nothing wrong with code, however I have added an alternative to this and updated the answer. – Qaisar Feroz Mar 18 at 11:35
  • 1
    The function would change both the page title and the h1 depending on how the theme implements both. – Howard E Mar 18 at 11:43

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.