0

I am building a Wordpress plugin, it is kind of a page builder and I want to allow users to manually customize the page layout by adding content/shortcodes to any page within the "Template Hierarchy": (archive.php, home.php, front-page.php, 404.php and search.php).

I have already done this for page.php using a "custom page template" which uses the content editor along with shortcodes to populate the layout and content of pages.

Now, how do I go about allowing those other templates (archive.php, home.php, front-page.php, 404.php and search.php) to accept dynamic content from a WYSIWYG editor to build their own layout in a simple and Wordpress-ish way?

  • 1
    can you add the code you already have in your page.php so it gives an idea of how it could be implemented elsewhere? offhand I'd say using do_action in the templates and add_action in the shortcodes might be the way to go... – majick Jan 8 '16 at 12:25
  • In fact I haven't added any code because it is actually not relevant, I am not doing anything more sophisticated than the_content which gets the content of the page including shortcodes, so far so easy but what about making the other templates behave the same way? I cannot add content through the WYSIWYG editor to a posts page or archive page for instance. Hopefully is more clear now. – human Jan 8 '16 at 15:36
  • I can't see a reason for the downvote, please provide feedback, as I believe providing code in this case will not be relevant at all. – human Jan 8 '16 at 15:37
  • wasn't me who downvoted, I'lll vote you back up, this is a good question as WordPress doesn't provide a native way to do this easily, just expects users to modify the templates themselves rather than having a UI for it. – majick Jan 11 '16 at 2:52
0

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. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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