0

I'm making a plugin that makes the first letter of the first word in a post title uppercase, if it was lowercase. I'm a beginner, so it's simple, but even so I struggle! The plugin can be activated without errors but it doesn't have the desired effect. Here's the code:

<?php

//Changes the first letter of post titles to uppercase

function shikharfirstletter() {

    $title = get_the_title();
    $title_first_letter = substr($title, 0);

    if($title_first_letter = 'a') {
        return str_replace($title_first_letter, 'A', $title);
    }

    elseif($title_first_letter = 'b') {
        return str_replace($title_first_letter, 'B', $title);
    }

    //So on for the other letters...
}

add_filter( 'wp_title', 'shikharfirstletter' );

?>

Any idea where I'm going wrong?

1
  • I do not see your function accepting any argument as every filter should.
    – maverick
    Jul 20, 2018 at 10:54

1 Answer 1

0

As maverick said in the comments, you need to accept the arguments which come along with the filter.

Try this

function shikharfirstletter($title) {
    $title = ucfirst($title);
    return $title;
}
add_filter( 'wp_title', 'shikharfirstletter', 10, 1 );

https://developer.wordpress.org/reference/hooks/wp_title/

1
  • Thanks! This worked, just wanted to add that 'wp_title' didn't have any effect, but it does if you change it to 'the_title'.
    – user147250
    Jul 23, 2018 at 6:47

Your Answer

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