• Hello, I was wondering if it would be possible to add the ability to add a custom shortcode to the redirect field. For example, my client has a weekly radio show that I want to post a link to the schedule of when she is on (every tuesday) and the url for each day is always changing, so I’ve created this shortcode:

    add_shortcode( ‘radioschedule’, ‘radioschedule’ );
    function radioschedule() {
    $radioschedule = sprintf(‘http://www.hayhouseradio.com/#!/schedule-by-day/%s’, date(“Y-m-d”, strtotime(“this Tuesday”)));
    return $radioschedule;
    }

    So no matter what day it is, it always returns the url of the upcoming tuesday date. It would be really nice to be able to simply add the shortcode [radioschedule] in the redirect field, so that the redirect url will always be dynamic. Thanks!

    https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

Viewing 4 replies - 1 through 4 (of 4 total)
  • MrFent37,
    The plugin cannot do that, unfortunately. That said, since you are proficient enough to create a shortcode, I will tell you how I would go about doing something like this (if it were me).

    • Create a page called ‘Weekly Schedule’ or something similar.
    • Add a custom field to that page, say ‘schedule-url‘ and have the value be the URL pattern you want to use – in this case, something like ‘http://www.hayhouseradio.com/#!/schedule-by-day/[schedule-url]‘.
    • In the functions.php file (where you would have added your shortcode) add the following (after the <?php):
      add_action('template_redirect', 'override_schedule_link_custom', 5);
      function override_schedule_link_custom(){
      	global $post;
      	if( is_admin() || is_404() || !is_singular() || !isset( $post->ID ) )
      		return;
      	$link = get_post_meta( $post->ID, 'schedule-url', true );
      	if( $link != '' ){
      		$redirect_link = str_replace( '[schedule-url]' , date( "Y-m-d", strtotime( "this Tuesday" ) ) , $link );
      		wp_redirect( $redirect_link, 302 );
      		exit;
      	}
      }
    • This will redirect if the post has the custom field called schedule-url (does not matter the page or the post, as long as it has that custom field).

    Then when anyone goes to http://yoursitehere.com/weekly-schedule/, they will always get redirected to the that same URL you posted in your page. You could put the link in any page or anywhere on the web and it will work. You could even throw this code in a plugin and use it that way too.

    Disclaimer – I typed this off the top of my head, on-the-fly, so there could be errors – but is should point you in the correct direction if it does not work out of the box.

    Best of luck,
    Don

    Thread Starter MrFent37

    (@mrfent37)

    Thanks Don! I decided to just go with a custom wp_redirect(); function instead

    zdavis

    (@zdavis)

    Hello MrFent37,

    Could you post your code of how you accomplished this? I’m also looking to put shortcode into the url.

    Thanks!

    Thread Starter MrFent37

    (@mrfent37)

    zdavis, I didn’t actually use my shortcode, nor did I even use this plugin to accomplish what I needed to do. I just created a custom redirect function, and applied it to the one specific page that I needed to dynamically redirect to. Here’s my code:

    function hayhouse_radio_page() {
     $hayhouseradio = sprintf('http://www.hayhouseradio.com/#!/schedule-by-day/%s', date("Y-m-d", strtotime("this Tuesday")));
     if (is_page(57476)) {
        wp_redirect($hayhouseradio);
        exit;
     }
    }
    add_action('template_redirect','hayhouse_radio_page');
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Redirect with a shortcode’ is closed to new replies.