Support » Plugin: Contact Form 7 » Need a method for base_url() or home_url() in additional settings

  • Resolved cravaus

    (@cravaus)


    on_sent_ok: “location.replace(‘https://www.mywebsite.com/pageid’)” Works well in additional settings but I need to make my website more portable. I would like to use a function to call base_url() or home_url(). Instead of writing out http://www.mywebsite.com/pageid, I would rather use on_sent_ok: “function(‘pageid’).”

    I have tried adding a JS to the page template:

    <script type="text/javascript">
    function PgRedirect($pageid) {
             var url=''
             url= "<?php echo home_url($pageid)?>";
             window.location = url;
    </script>

    This does not work. I have tried adding a function to functions.php:

    function wpcf7_pg_redirect($pageid){
        wp_redirect(home_url($pageid));
        exit();
    }
    add_action('wpcf7_mail_sent', 'wpcf7_pg_redirect');

    This does not work either. I am not sure what I am missing here. Any tips would be appreciated.

    https://wordpress.org/plugins/contact-form-7/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter cravaus

    (@cravaus)

    This ended up being my solution:

    on_sent_ok: “location.replace(window.location.href.substring(0, (window.location.href.lastIndexOf(“/”)) + 1) + ‘[postid]’);”

    This gives me the home url and makes it possible to adjust to changes in that url when I try to port the site on other servers.

    Thread Starter cravaus

    (@cravaus)

    This is an even better solution based upon a shourtcode method found here:
    https://wordpress.org/support/topic/linking-to-pages-within-site?replies=12

    In functions.php add this:

    // create shortcode for website's home page address: [url]
    
    function myUrl($atts, $content = null) {
    	extract(shortcode_atts(array(
    		"href" => home_url()
    	), $atts));
    	return $href;
    }
    add_shortcode("url", "myUrl");

    With this [url] works in this form:

    on_sent_ok: “location.replace(‘[url]/pageid/’);”

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need a method for base_url() or home_url() in additional settings’ is closed to new replies.