I'm a new comer to programming and learning with Wordpress.

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

I wish to capture the title from the page right into a string variable in order to manipulate it with strtolower and str_replace functions.

The only method I've become results is by using output loading.

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 shall we be held doing wrong?

just figured Id share my final solution with everyone.

Sony playstation give my body system 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.

There's no simpler way. Your function doesn't return the string, it prints it, therefore you'll have to use output loading if you wish to capture the output.

It is the distinction between f1() and f2() within the following example.

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

// Prints a string, returns nothing.
function f2() {
    echo "String";
}

Wordpress is really a HORRIBLE application to learn to program from. It uses these global functions that "just work" however they do very specific tasks "inside 'The Loop'". As I only say, this can be a horrible illustration of what good code ought to be.

Fortunately (for you personally) you will find other functions that simply return the part you are searching for. Instead of me just writing the thing you need, read a full listing here. Be mindful that you simply make a note of which should be inside the mythical Loop and that can be used anywhere.

In fact you will find much more methods for getting the title, however i really was imagining let's imagine you'd make a move 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 the second poster (properly) states use a quite simple wordpress_title() function to seize the present loop title.

This brings me to possibly attempting to explain why learning programming from Wordpress is an awful idea. They've a lot of damned method of doing exactly the same damned factor it's nearly impossible to help keep on the top of things.

Your blog is a very simple group of data (even moreso in WP's situation since it is not fully normalised) but instead of just getting one method to output a title <?php echo $post->title; ?> you've many ways, all doing subtly various things.

If you want to learn to program (rather than hacking the right path round the garbage that's the Wordpress internals), developing a simple blog engine is rather fast and fun... It's certainly how many people enter into a brand new language or framework.

And when you really wish to have fun, take a look at Django.


An adequate amount of the Wordpress rant. If you are fighting something similar to this later on that does not have 100 methods for doing the work, I truly wouldn't recommend output-buffer-taking. It melts away an entire buttload of assets for something easy.

The simplest way is often as simple as just using the source for that original function, adhering it inside a new function and changing the echo with return.

Just note there might be some database connectivity to deal with that coming back prematurely may break... Therefore if the echo is not the final statement, rather than coming back immediately, keep string like a variable and return in the finish from the function.

If what you're searching for may be the wp_title function, the second argument it requires is really a boolean on whether it will display it or give it back. Pass it false therefore it will give it back towards the var, then this can be done:

$mytitle = wp_title(null, false);

Otherwise, your only option is to locate the function you are searching for and customize the source code.