0

I’m fairly new to wordpress and all my previous sites have been in .asp or raw .php. and although I can tinker I’m no developer and due to time constraints I was hoping this would be an easy fix - but I'm not getting very far and suspect what I'm trying to do will be too advanced for me in the time I have.

I'm trying to migrate a site presently written in classic .asp. What I’m trying to accomplish – which is simple in the above .asp – is as follows:

I have a site which has a database of towns & postcodes, counties etc. A user can click on a list of towns which have a URL like https://example.com/delivery/?town=London - or click on a link https://example.com/delivery/London that gets re-written to the URL with parameters that calls the same page and passes the same paramaeters

I used to be able to extract the parameter “London” and use it as a parameter to generate a query to list all the supported locations in London, but also use the “London” tag to generate Meta tags, keywords and descriptions for SEO purposes – I then need the URL to be written like https://example.com/delivery/London

Using a plugin that allows me to put PHP code in the page, I can read the “london" parameter and create the sql query and produce the required list OK – but what I can’t work out in WordPress is how to use this paramter to populate metatag data and rewrite the URL “cleanly”

This could result in several 1000 pages - hence the need to dynamically generate the pages.

I apologise if this has been asked before – but given my knowledge of WordPress a lot of what I’ve seen goes over my head. Is what I’m trying to do possible – either by a bit of tinkering or ideally a plug-in of some kind? I've been told I need to invoke some functions, and create plugins - but time constraints prevent me from getting this working as the learning curve is too geat at the moment - I've no idea where these functions are, how to create a plugin etc etc.

Any suggestions, pointers or information would be greatly appreciated. My knowledge of the inner workings of Wordpress isn't great, so anything to technical probably wont help me although if there is something that explains it all I would be able to start learning, but time is against me!!

Regards…

p.s. the site is running on a test server to not “live”

  • What you're trying to do is possible. But if you want to map the URL pattern to a certain query string, just to do a simple database query, using WordPress is may be too much. It might be better to ask this question on general PHP programming question. But, if I may give a recommendation here, using CodeIgniter framework should be enough. – hamdirizal Feb 6 at 23:11
1

First, I recommend against using PHP in post content plugins. Creating your own plugin is simple enough, it's as easy as creating a php file and adding a plugin header to the top of the file:

<?php
/**
 * Plugin Name: Delivery Plugin
 */

Upload the file to the plugins directory, and activate it via the Plugins menu in admin.

From here on, we'll assume you've created a page under Pages menu, with the slug delivery.

Next step is to add a rewrite rule for the "pretty" URLs, and a query var delivery_location to hold the value of whatever location was passed in the URL:

function wpd_delivery_rewrite() {
    add_rewrite_tag(
        '%delivery_location%',
        '([^/]+)'
    );
    add_rewrite_rule(
        '^delivery/([^/]+)/?',
        'index.php?pagename=delivery&delivery_location=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpd_delivery_rewrite' );

After adding this function to your plugin file, go to the Setting > Permalinks page in admin, which will flush rewrite rules and make this rule active.

Now you can visit /delivery/some-location/ and you will see the content of the delivery page.

Next, we'll add a filter to the_content so we can dynamically insert some text into the page content when it's viewed:

function wpd_delivery_content( $content ) {
    if( false !== get_query_var( 'delivery_location', false ) ){
        $content .= 'Delivery location is ' . get_query_var( 'delivery_location' );
    }
    return $content;
}
add_filter( 'the_content', 'wpd_delivery_content' );

This checks if delivery_location was set, and outputs the value if it exists. You can add your code within this function that fetches whatever data you need to output to the page. Note that the function must return its content for the page to render properly, it can't directly output via echo or directly print it.

The last step is to hook the wp_head action, where we can insert our own meta tags. We do the same check for our custom query var, and output whatever tags we want:

function wpd_meta_tags(){
    if( false !== get_query_var( 'delivery_location', false ) ){
        echo '<meta name="description" content="' . get_query_var( 'delivery_location' ) . '" />';
    }
}
add_action( 'wp_head', 'wpd_meta_tags', 0 );
  • Hi - thanks for this detailed reply. Since posting I did some investigations and - as you say - creating a plugin is pretty straightforward.... once you know how! But your reply is exactly what I as after - not just "yes you can do it" but a detailed explanation on how to do it... excellent!! Thank you so much!! – Fozzy279 Feb 7 at 22:17

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.