1

Background

What I am attempting to do is create a custom post layout template for a specific post (or even page) that the WordPress user can simply insert content using the WYSIWYG editor without having to worry about formatting (essentially "regions" they place content in). I want to have multiple of these "layout templates" so depending on the post (or page) they are editing in the WP admin area, they have a predefined area that they can place, for example, some text and an image.

What I've tried

add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content ) {
    global $post;
    $id = $post->ID;
    if ( $id == '32') {
        $content = '
          <div>place image here</div>
          <p>place text here</p>
        ';
    }
    
    return $content;
}

Example initially taken from here

What I'm attempting

In the above example (which does not work), I want to essentially do a conditional statement saying if post id equals X, then use this template for the creation of the post

What I found out was the default_content filter is only applied during the creation of a new post (or page) and therefore it has no effect on existing posts/pages, so conditionally running this code based on id is useless.

End goals

There are a few outcomes I think may help me along...

  • If there is a way to run the default_content filter on post save/edit, then conditionally applying the filter based on post id would likely work

  • Figure out a way to create multiple different custom post templates for an end-user. That is, have predefined regions that they can insert content within a post.

  • Or lastly, is there another way to allow a end-user, with no HTML ability, to just put content into a post or page and it will automatically get formatted for them (with my doing the formatting on the backend)?

1 Answer 1

2

Figure out a way to create multiple different custom post templates for an end-user. That is, have predefined regions that they can insert content within a post.

You can do that with custom fields / meta boxes, which you can restrict or display based on post-type or ID. You should try the Advanced Custom Fields plugin.

Or lastly, is there another way to allow a end-user, with no HTML ability, to just put content into a post or page and it will automatically get formatted for them (with my doing the formatting on the backend)?

If the custom fields approach is too complicated, you might try the Quicktag API with buttons on the Visual Editor (check AddQuicktag). This way the user will only click a button on the Editor Toolbar and the HTML/CSS will be applied to the content. And if you edit the editor-style.css the user will be able to see the changes in the editor, just like it would appear on the front-end. I think it's a cleaner approach than to use standard shortcodes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.