How to add dynamic content to posts page, archive, taxonomies, search, author, etc?

In your page template your code could be something as simple as this:

do_action('archive_page_html');

The thing is, if you are building a plugin, having your users modify their theme templates to put this in may or may not be so desirable, but since different themes have different templates and you are writing a plugin, it may also be unavoidable…

And in your plugin/theme coding you would add something like this:

add_action('archive_page_html','custom_archive_page_html');
function custom_archive_page_html() {
    // get your theme option value, however you are doing that
    $archivehtml = get_option('archive_page_html'); 
    // allow it to be further customized on a condition basis, 
    // eg. to use different HTML for different archives
    $archivehtml = apply_filters('archive_page_html_filter',$archivehtml); 
    // process the HTML using the_content filter
    // (this will also process any shortcodes)
    $archivehtml = apply_filters('the_content',$archivehtml);
    echo $archivehtml;
}

Then it depends what kind of user interface you want to provide for adding the content to each of the page types.

  1. You could add the different HTML sections in your theme options via the Customizer, I have seen this done for similar blocks. But to get the WYSIWYG editor you may have to add a TinyMCE customizer control in your plugin/theme, such as the Text Editor control from this project:

https://github.com/paulund/Wordpress-Theme-Customizer-Custom-Controls

(For coding the Customizer itself best to find a tutorial, it can be a bit tricky.)

  1. Another option would be to create an admin-only Custom Post Type for handling the templates (see register_post_type()) and have your plugin/theme automatically create all the posts with matching slugs (see wp_insert_post()) on activation (checking they don’t already exist)… then this template content would be available for users to edit via the post writing screen for that custom post type.

The change to the action function code would be instead of using get_option etc. you would get the correct template using get_posts using the custom post type and slug, eg.

$posts = get_posts(array(
    'post_type' => 'templates', // The Custom Post Type registered
    'name' => 'archive' // The slug for the CPT template created
    )
);
$archivehtml = $posts[0]->post_content;
  1. A third option would be to add these interfaces yourself into your own custom theme/plugin settings pages, but of course you’d have to decide how best to do that yourself. 🙂