1

I am customizing wordpress blog and I have a need to make custom sidebar widgets. My PHP is rusty at best. What I am trying to do is concatenate a php variable into a string being set as an array element. here is the code I am using, it doesn't seem to work. All it does is print the stylesheet directory at the top of every page:

if ( function_exists("register_sidebar") )
    register_sidebar(array(
        "before_widget" => "<div class=\"rounded_box\"><div class=\"top_curve\"><img src=\"".bloginfo('stylesheet_directory')."/images/top_curve.jpg\" alt=\"Top\" width=\"247\" height=\"9\" /></div><div class=\"middle\">",
        "after_widget" => "</div><div class=\"bottom_curve\"><img src=\"".bloginfo('stylesheet_directory')."/images/bottom_curve.jpg\" alt=\"Bottom\"  /></div></div>",
        "before_title" => "<h2>",
        "after_title" => "</h2>",
    ));

so as you can see here I am trying to concatenate the bloginfo('stylesheet_directory') into 2 of the elements. This doesn't work properly. It just ends up printing it at the top of the page before the doctype.

3
  • I'm not familiar with WordPress, but at a guess, the bloginfo() class is echoing things out rather than returning a string. Commented Nov 20, 2009 at 21:02
  • if this is the case, then how would i get around it? Commented Nov 20, 2009 at 21:04
  • 1
    Aaaaah. R. You are correct. Russ, use get_bloginfo("stylesheet_directory") Commented Nov 20, 2009 at 21:08

4 Answers 4

3

bloginfo('stylesheet_directory') will echo the stylesheet directory. When you declare the array, you are effectively writing to stdout. This is why it will show on top of the page. What you are looking for is get_bloginfo.

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

1 Comment

As an aside, a good example of why 'view' type helpers shouldn't echo on their own, but return the output for the calling script to control.
0

Use implode:

string implode  ( string $glue  , array $pieces  )
string implode ( array $pieces )

Join array elements with a glue string.

1 Comment

i am not trying to join the array elements. I am trying to concatenante the bloginfo('stylesheet_directory') into 2 of the elements but it is not working properly.
0

It looks like you have a comma at the end. It might be that. Remove it and test. I've also replace \" with a singe '.

UPDATE replaced bloginfo() with get_bloginfo().

if ( function_exists("register_sidebar") )
{
  $args =array(
  "before_widget" => "<div class='rounded_box'><div class='top_curve'><img src='".get_bloginfo('stylesheet_directory')."/images/top_curve.jpg' alt='Top' width='247' height='9' /></div><div class='middle'>",
  "after_widget" => "</div><div class='bottom_curve'><img src='".get_bloginfo('stylesheet_directory')."/images/bottom_curve.jpg' alt='Bottom' /></div></div>",
  "before_title" => "<h2>",
  "after_title" => "</h2>");'

  register_sidebar($args);
}

2 Comments

this has the same effect of printing the stylesheet directory at the top of the page
What if you try $before_wid = "string 1".bloginfo('stylesheet_directory')."string 2"; And then you put "before_widget" => $before_wid ?
0

I know that this is not technically the answer to your question, but have you considered:

if ( function_exists("register_sidebar") )
    $ssheet_dir = bloginfo('stylesheet_directory');
    register_sidebar(array(
            "before_widget" => "<div class=\"rounded_box\"><div class=\"top_curve\"><img src=\"$ssheet_dir/images/top_curve.jpg\" alt=\"Top\" width=\"247\" height=\"9\" /></div><div class=\"middle\">",
            "after_widget" => "</div><div class=\"bottom_curve\"><img src=\"$ssheet_dir/images/bottom_curve.jpg\" alt=\"Bottom\"  /></div></div>",
            "before_title" => "<h2>",
            "after_title" => "</h2>",
    ));

It would be easier and faster -- it would only involve making the bloginfo function call once.

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.