tags:

views:

272

answers:

6

I am new to programming and learning with Wordpress.

the_title(); //outputs the title of the page

I want to capture the title of the page into a string variable so I can manipulate it with strtolower and str_replace functions.

The only way I have gotten it to work is with output buffering.

ob_start();
the_title();
$result = ob_get_clean();

echo str_replace(" ","-",strtolower($result));

/*there has got to be an easier way....
i dont fully understand why */

str_replace(" ","-",strtolower(the_title()));

What am I doing wrong?

+3  A: 

There is no easier way. Your function does not return the string, it prints it, therefore you will have to use output buffering if you want to capture the output.

It's the difference between f1() and f2() in the following example.

// Returns a string, prints nothing.
function f1() {
    return "String";
}

// Prints a string, returns nothing.
function f2() {
    echo "String";
}
Ferdinand Beyer
Or use one of the WP functions that *does* return the string value...
Oli
The question's title is "best way to capture data", not "what WP function returns the title".
Ferdinand Beyer
this helped me understand what was going on, thanks
chris
+10  A: 

If what you really are looking for is the wp_title function, the 2nd argument it takes is a boolean on whether or not it should display it or return it. Pass it false so it will return it to the var, then you can do this:

$mytitle = wp_title(null, false);

Otherwise, your only option is to find the function you're looking for and modify the source code.

Paolo Bergantino
sweet wish I could upvote this but im too new thanks
chris
A: 

wow thanks guys!!! my first post on stack...such quick answers, amazing.

chris
+2  A: 

Wordpress is a HORRIBLE app to learn how to program from. It uses these global functions that "just work" but they do very specific tasks "inside 'The Loop'". As I say, this is a horrible example of what good code should be.

Thankfully (for you) there are other functions that just return the part you're looking for. Rather than me just writing what you need, you can read a full listing here. Take care that you note down which must be within the mythical Loop and which you can use anywhere.

As it happens there are even more ways to get the title, but I was really imagining for this example you would do something like:

$this_post = get_post($post); // I *think* $post is the post ID inside the loop though I could be wrong
echo $this_post->post_title;

But as another poster (correctly) says you can use a fairly simple wp_title() function to grab the current loop title.

This brings me back to perhaps wanting to explain why learning programming from Wordpress is a bad idea. They have so many damned way of doing the same damned thing that it's almost impossible to keep on top of things.

A blog is a really simple set of data (even moreso in WP's case because it isn't fully normalised) but rather than just having one way to output a title <?php echo $post->title; ?> you have umpteen ways, all doing subtly different things.

If you really want to learn how to program (instead of hacking your way around the crap that is the WP internals), creating a simple blog engine is fairly quick and fun... It's certainly how a lot of people get into a new language or framework.

And if you really want to have fun, have a look at Django.


Enough of the Wordpress rant. If you're fighting something like this in the future that doesn't have 100 ways of doing it, I really wouldn't recommend output-buffer-capturing. It uses up a whole buttload of resources for something relatively simple.

The easiest way can be as simple as just taking the source for the original function, sticking it in a new function and replacing the echo with return.

Just note there may be some database connectivity to handle that returning prematurely may break... So if the echo isn't the last statement, instead of returning right there, store the string as a variable and return at the end of the function.

Oli
thanks for the info oli, will have a look at Django. I dont know any programmers personally so this site is really getting me excited :)
chris
+1  A: 

just figured Id share my final solution with you guys.

This was to give my body tags unique id's in wordpress.*/

$title =wp_title(null,false);
echo strtolower(str_replace(' ','-',ltrim($title)));


//without the ltrim() 2 dashes are created before the title.
chris
+1  A: 

Almost every 'the_*' function in Wordpress has a 'get_the_*' counterpart. So, you just have to use

echo str_replace(" ","-",get_the_title());

And it's going to work like a charm. there's also get_the_excerpt(), get_the_content() and the_permalink() which somehow breaks the naming convention (God knows how many times I've written "get_the_permalink()" and got frustrated on why it didn't work)

Cheers!