0

I am trying to the redirect on a cart page using a bellow code but,I am not able to redirect on cart page .

wp_redirect("'.home_url('cart').'");

2 Answers 2

2

Please use get_page_by_path

wp_redirect( get_page_by_path( 'cart' ) );
exit();

Reference link for get page by path

4
  • You might want to test that weird quoting: echo "'.get_page_by_path('cart').'"; The function never runs. Oct 23, 2013 at 20:25
  • Man, please! Start using the WYSIWYG editor! Click the edit link and fix it. I already commented on your other answers with the exact same notes. Aside from that @CharlesClarkson is right and aside from that, you're missing the die/exit at the end or around the function.
    – kaiser
    Oct 24, 2013 at 4:12
  • 1
    I removed the downvote and fixed your code. "'.foo().'" will break. For the future: Please test your code before posting something that breaks. Else you'll quickly get a lot of downvotes and loose reputation points and the capabilities.
    – kaiser
    Oct 24, 2013 at 14:21
  • get_page_by_path() returns a WP_post object of the page and not a URL to the page. To get the URL to a page by name, use get_permalink(get_page_by_path($pagename))
    – Guss
    Mar 7, 2017 at 12:40
1

Try it like the following. The exit() is necessary to stop other code from running and redirect right there.

exit( wp_redirect( home_url( "cart" ) ) );

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