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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

How to set default unremovable text within Wordpress post title?

  • 28-03-2014 2: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, Registered Users 2 Posts: 16,414 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


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


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 6,571 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 6,571 ✭✭✭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