Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How to set default unremovable text within Wordpress post title?

Options
  • 28-03-2014 3:45pm
    #1
    Banned (with Prison Access) Posts: 388 ✭✭


    Hi folks,

    Quick question. I have a WP site where I want to add words at the start of the title of post by default, and I also want these words to remain in such a way that the user has to include them in their post.

    In other words, let's say the words in question are 'Once upon a time', and the user has to finish their title with those words as the first four words of the title. Is there a way to do this?

    The way it is right now is that text appears in the title by default, but the user can remove this if they wish. I want to make it so that the text remains and can't be removed.

    Any ideas?


Comments

  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    Probably need to code something up that checks $post->post_title for the prefix you want, or maybe add it when outputting the_title().


  • Registered Users Posts: 241 ✭✭fcrossen


    Use a Wordpress action filter hook:
    - http://codex.wordpress.org/Plugin_API

    You can filter the post title on display or pre-pend the text to the post title or content when the post is saved.


  • Registered Users Posts: 6,487 ✭✭✭daymobrew


    It looks like you can use the 'the_title' filter.


  • Registered Users Posts: 6,487 ✭✭✭daymobrew


    Maybe something like this:
    [php]function add_title_words($title, $id) {
    $phrase_to_add = 'Once upon a time';

    // Add the phrase if it isn't in the title.
    if (false === strpos($title, $phrase_to_add)) {
    $title = $phrase_to_add . ' ' . $title;
    }
    return $title;
    }
    add_filter( 'the_title', 'add_title_words', 10, 2 );
    [/php]


  • Registered Users Posts: 1,213 ✭✭✭was.deevey


    Could you just add this into the template ?
    <h1>Once Upon a Time <?php the_title(); ?><h1>
    


  • Advertisement
  • Registered Users Posts: 6,487 ✭✭✭daymobrew


    was.deevey wrote: »
    Could you just add this into the template ?
    <h1>Once Upon a Time <?php the_title(); ?><h1>
    
    I don't think so - OP said that it is there but can be removed when the post is edited.
    The way it is right now is that text appears in the title by default, but the user can remove this if they wish. I want to make it so that the text remains and can't be removed.


  • Registered Users Posts: 1,213 ✭✭✭was.deevey


    I don't think so - OP said that it is there but can be removed when the post is edited.

    Sorry I read it that he wanted it to be displayed on the front end regardless - my bad

    This works nicely though....

    Edit: /wp-admin/edit-form-advanced.php ... around line 440
    [B]<span style="font-family: sans-serif;
    font-size: 20px;"> Once Upon A Time....</span>[/B]<label class="screen-reader-text" id="title-prompt-text" for="title"><?php echo apply_filters( 'enter_title_here', __( '[B]Enter Story Title[/B]' ), $post ); ?></label>
    <input [B]style="width:65%;"[/B] type="text" name="post_title" size="30" value="<?php echo esc_attr( htmlspecialchars( $post->post_title ) ); ?>" id="title" autocomplete="off" />
    

    Result:

    13523368675_1f53e7357b.jpg

    I'm sure there is a less "dirty" way of doing it via functions as well mind.


  • Registered Users Posts: 6,487 ✭✭✭daymobrew


    was.deevey wrote: »
    This works nicely though....

    Edit: /wp-admin/edit-form-advanced.php ... around line 440
    was.deevey wrote: »
    I'm sure there is a less "dirty" way of doing it via functions as well mind.
    Never never never ever edit core code!
    Hooks and actions are there so that you don't have to do this.


  • Banned (with Prison Access) Posts: 388 ✭✭Atomico


    Thanks for all the replies guys.


Advertisement