Support » Developing with WordPress » custom page template default content

  • Resolved girl_number_5


    I have created a custom page template and id like to add some custom html to it so each page based on this page starts off with this html which i can then edit. The content is being output in the template (part) with the_content() function. How can i intercept or write my html to the_conent() so it gets output with the page template.

    The html is a very simple ‘nested’ unordered-list with some placeholder texts – if i comment-out the_content() function and echo the nested html it appears in the browser ok but not in the content area in the admin > pages to edit the page.

    I believe i can hook into ‘the_content’ event – so maybe i just need to know how to ensure that any custom changes to the_content() only happen in my custom page template. Is this the best way to go or is there an easier way to write to the_content()


Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    You are correct, “the_content” filter is a way to add HTML to what is output by a template call to the_content(). You can easily add static content by having your filter callback do something like return 'Hello world! '. $content;. Obviously this isn’t editable by regular users. If you want to be able to edit “Hello world!” to something else your code would need to grab content from the database instead of simply concatenating the text.

    Where to get it in the DB depends on where it was saved. For example, you could add a custom option value if you want the same content added to all pages. In order to edit and save that value, you’d need a custom option added somewhere in the admin area. For more on adding options, review the Options API section of the Plugin Developers Handbook. Despite being in the Plugin Handbook, the same concepts can be applied to themes equally well.

    Thread Starter girl_number_5


    Hi (@bcworkz)
    Thanks for the tips; this, ive just discovered is a lot easier than i first thought. To give you the bigger picture – when a custom post type (im using as a project post) is saved a new page is created which is related to the post/project. In short im using:
    wp_insert_post so i just assign the post_content key with a value equal to my custom html string, sorted! Only small issue now is i want the new page to use my custom template and i can’t see that there’s a key in wp_insert_post function array. So where the key val is currently:
    'post_type' => 'page' – ill like to have something like:
    'post_type' => 'template_dir/my-template.php'

    Any ideas?

    Thread Starter girl_number_5


    Ok found it … solved

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.