0

Here's the scenario...

For a local site I have different city specific pages. The first paragraph of those pages will be unique and related to the specific city. But the content after that will be the same with exception of the city names...

So what I want to do is create a content snippet that holds the content that is the same for every city BUT ALSO I will need to be able to pass that content snippet a value (the city names).

For example, if my content snippet would hold this text: "CITYNAME is a beatiful city", then CITYNAME should be replaced by the value that I passed to the content snippet, e.g. Dallas.

What are the different ways to achieve this with wordpress? Plugins or custom code.

1

The best way to achieve this would be using a shortcode or a filter.

I tend to use the Shortcode in this scenario, because you are flexible to add another paragraph behind the autoatically generated content, this is more complicated as a filter. Also, I do not know if your Cities are Custom Post Types, or if there is another way WordPress could know that this is a City and not some other content (using a meta_value, page_template, ...).

The function to create the content

However, the function to generate the content is fairly simple. I added __() to make the content translateable, as you should do with every hardcoded string.

function f711_generate_city_texts( $cityname ) { // function receives cityname as variable
    return $cityname . __( 'is a beautiful city', 'f711_wpse' ); //returns the custom content with the cityname built in.
}

Now, we need to pass add this function to the content.

Adding the shortcode

I create a Shortcode function, and add the shortcode:

add_shortcode( 'f711_citytext', 'f711_citytext_shortcode' );


function f711_citytext_shortcode( $atts ) {

    extract( shortcode_atts( array( 
        'city' => get_the_title(), // define city as a attribute for the shortcode, with the title as standard value
    ), $atts ) );

    return f711_generate_city_texts( $city );
}

In your content, you can now use

[f711_citytext city="yourcityname"]

or you could leave the city-attribute, if you are within the loop, as get_the_title() is used as the standard value in the function.

Adding a filter

Of course, you could also add a filter to add the text automatically, as I mentioned you need a way of knowing that this content belongs to a city in this case.

I will use a meta_value containing a bool with f711_iscity set to true.

add_filter( 'the_content', 'f711_citytext_filter', 10, 1 );

function f711_citytext_filter ( $content ) {
    if ( get_post_meta( get_the_ID(), 'f711_iscity', true ) === true ) 
        return $content . f711_generate_city_texts( get_the_title() );
    return $content;
}

This function checks if you set f711_iscity to true, and returns the content with the appended generated text. Otherwise it leaves the content untouched.

Conclusion

For flexibility reasons, I would prefer the shortcode soluiton in this case.

  • Thanks for that solution, in the meantime I was busy already and made a solution using the 5sec php plugin for wp. Basically I included a php file and then called a function in it with a city as the argument. Then it returns the city modified content for me. But your solutions is native to wordpress (no plugins needed), so that might be better... Question: where do I define the sortcode and the function that displays the content? Is there a specific file/place in Wordpress to define custom things like that? – Jan Paul Mar 17 '14 at 9:22
  • You can just place the code in your functions.php, that is enough. You could also create a Plugin from it. – fischi Mar 17 '14 at 11:19
  • will functions.php not be replaced when i update worpress? – Jan Paul Mar 18 '14 at 3:43
  • No, functions.php is in your theme folder, whiche does not get replaced when updating WordPress. If you update the theme however, it could be overwritten (if you use twentytwelve for example). So your safest way would be to create a small Plugin containing this code. – fischi Mar 18 '14 at 8:25

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.