WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help on presetting HTML content in the editor with function filter (5 posts)

  1. Jaya
    Member
    Posted 3 years ago #

    I was looking for ways to preset html content to the editor upon the creation of new posts. I've found many examples to how to do that for text but i wasn't able to figure out how to do the same thing with html content. (ex: text + images). Here is the example I've found:

    add_filter( 'default_content', 'my_editor_content' );
    
    function my_editor_content( $content ) {
    
        $content = "my-preset-content-here";
    
        return $content;
    }

    Any help in the matter would be appreciated!

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    $content = '<img src="some/path/to/some/image/example.jpg" /><p>Hello World!</p>';

    Simply ensure that if the string contains any single quotes that you escape them, like so..

    $content = '<img src="some/path/to/some/image/example.jpg" /><p>It\'s christmas</p>';

    Of course, you don't need to do that for the single quotes that start and end the string, only those that occur inside the string..

  3. Safirul Alredha
    Member
    Posted 3 years ago #

    I think it's appropriate to use entities instead. Even though it get converted when saved.

    ... <p>It’s christmas</p> ...

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Technically, what you have there is a closing single quote, but if you want to use that, then sure, but use the HTML entity..

    <p>It&#146 ;s christmas</p>

    Without the space before the semi-colon(forum converts the entity - see below)..

    <p>It’s christmas</p>
  5. Jaya
    Member
    Posted 3 years ago #

    Thank you very much!! That worked :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.