4

This is fairly simple but I cant work it out.

I'm building a wordpress site, and I want a redirect on my 404 page using PHP instead of javascript.

<?php header("Location: www.myblogurl.com"); ?>

And I use this in my wordrpess websites to get the URL of my blog...

<?php bloginfo('url'); ?>


But I can't seem to merge the two PHP scripts together, please see my poor attempt below.

<?php

    $location = bloginfo('url');

    header( 'Location:' + $location ) ;

?>

This seems to echo my website URL instead of redirecting.

Can any help me please, thanks.

2
  • 3
    . is the concatenate operator in PHP not + Commented Apr 3, 2012 at 12:16
  • Thank you all for your responses Commented Apr 3, 2012 at 13:06

6 Answers 6

8

bloginfo in wordpress echo's the information instead of returning it. You should use get_bloginfo.

The docs says:

This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().

A sample would be:

<?php
header('Location: ' . get_bloginfo('url'));
?>

You are using the + to concatenate 2 strings. This is wrong in PHP the + is a math operator for things like $result = 1 + 1. You need to use the . operator: $text = 'hello' . 'world'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for help and better script :-)
1

Well first correct + with .

String concatenation works in php with .

 <?php

$location = bloginfo('url');

header( 'Location: ' . $location ) ;

?>

Comments

1

try to use get_bloginfo() instead of bloginfo() and replace the + with .

1 Comment

+1 because you are the only one (besides me) who posted the real solution instead of the + operator (which is a side problem)
0

Use this:

<?php

    $location = bloginfo('url');

    header( 'Location:' . $location ) ;

?>

You were using Javascript syntax for concatenation in PHP

Comments

0
<?php

    $location = bloginfo('url');

    header( 'Location:'.$location ) ;

?>

This should be your syntax.

Comments

0

Use a dot (.) not a plus (+).

header( 'Location:' . $location ) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.