0

I am creating a dynamic page, in a WordPress environment. The page is created by a default standard WordPress page and then using shortcode. Combined with URL parameters in order to retrieve the necessary data.

The problem is, this page, including the URL parameters, is "shareable" to facebook and other social media. Thus the description that comes up is too generic. I assume this is because the default page, on which the shortcode resides, is also generic. That is what I want to change based on the data I am retrieving for display.

I have tried the code below and a few other variations, with no luck. In fact the filter is not even executed. Tried embedding the filter into an add_action call..the action call is done, but the filter remains executed.

 add_filter('wp_title','ChangeTitle', 10, 2);   
 function ChangeTitle($title, $id)
    {
    if ( $title== 'Details' ) 
       {
       $BusinessName = $_GET['Name'];   
       return $BusinessName;
       }
 return $title; 
 }

Does anyone have a clever way to do this? part two of the question, would be how to assign a logo to the share.... but one thing at a time.

5
  • What your shortcode looks like? Do you need to change the title or the description?
    – Max Yudin
    Commented Jun 8, 2019 at 7:28
  • Simple shortcut that just calls a function with just the default parameters. I need to change the title and the description, but I can live with just the title. [LISTING_DETAILS] Commented Jun 8, 2019 at 7:43
  • The title and the description will be based on the data I am retrieving from the database. It is not fixed. In essence, it could be hundreds of different pages, since it is dynamically created. Commented Jun 8, 2019 at 7:48
  • Where are you putting this code? If it's never getting called, then it might be in the wrong place. Also, that filter has to be supported by whatever theme you're using... if it doesn't run the filter, your code will never be run
    – Paul G.
    Commented Jun 8, 2019 at 11:22
  • Paul - if I switch it to a add_action, it is called. I am beginning to suspect the SEO plugins are stopping it. Commented Jun 8, 2019 at 16:42

2 Answers 2

1

So depending on your theme, you'll need to take a different approach. By the sounds of it, you're using a theme that adds WordPress theme support for title-tag e.g. twentynineteen. In this case, your code would look like this:

function add_dynamic_title( $aTitleParts ) {

    if ( isset( $aTitleParts[ 'title' ] ) && $aTitleParts[ 'title' ] == 'Details' ) {
        $aTitleParts[ 'title' ] = $_GET[ 'Name' ];
    }

    return $aTitleParts;
}

add_filter( 'document_title_parts', 'add_dynamic_title', 20 );

This filter uses an array where it contains, amongst other things, a key title and perhaps tagline. So in this case you set the array key value for title to whatever you'd like and return the whole array.

2
  • Brilliant, worked like a charm. The share to facebook is still using the old name, so I will need to explore that. But the tab is now what is passed by the Url param. Commented Jun 8, 2019 at 17:07
  • 1
    Fab, glad it worked for your page titles! Feel free to mark the answer as accepted if it works, but if there's anything else extra needed to get this problem sorted, let me know...
    – Paul G.
    Commented Jun 9, 2019 at 10:56
1

The following code gives you the ability to use [facebook_title] shortcode in the title and [facebook_description] respectively.

If you use any of Yoast SEO, SEOPress or All in One SEO Pack plugins, add these shortcodes primarily there.

Remove lines activating shortcodes in unused plugins.

// get your special title
function my_facebook_title() {
    return get_post_meta( get_the_ID(), 'post_meta_facebook_title', true );
}

// get your special description
function my_facebook_description() {
    return get_post_meta( get_the_ID(), 'post_meta_facebook_description', true );
}

// add hooks to shortcodes
add_shortcode( 'listing_title', 'my_facebook_title' );
add_shortcode( 'listing_description', 'my_facebook_description' );


// activate shortcode in the post headline
add_filter( 'the_title', 'do_shortcode' );

// activate shortcode for the single post <title>
add_filter( 'single_post_title', 'do_shortcode' );

//// Plugins (optional)

// activate shortcodes in SEOPress
add_filter( 'seopress_titles_title', 'do_shortcode' );
add_filter( 'seopress_titles_desc', 'do_shortcode' );


// activate shortcodes in Yoast SEO
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );


// activate shortcodes in All in One SEO Pack
add_filter( 'aioseop_title', 'do_shortcode' );
add_filter( 'aioseop_description', 'do_shortcode' );

Update: SEOPress and All in One SEO Pack support added.

4
  • sadly, I don't use Yoast. I use another SEO plugin-- bit easier for the client to manage than Yoast. But I am going to look to see if there is a way to use these calls you reference. Where is the documentation on it? Commented Jun 8, 2019 at 17:25
  • All-in-One-SEO also has filters. Which plugin do you use? There is no documentation in one block, it's all around.
    – Max Yudin
    Commented Jun 8, 2019 at 18:47
  • SEOPress. Easiest for non-technical owners who want to do it themselves and not pay for it. Commented Jun 8, 2019 at 23:39
  • SEOPress also has similar filters, so the routine is the same. The answer is updated.
    – Max Yudin
    Commented Jun 9, 2019 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.