0

I need to programmatically create a page that uses a specific/custom template as defined programmatically. Both the page and template are created/used by my plugin.

The plugin files include a page template. The plugin code (with a button press) will create the page ('mycustompage') (similar to the technique here Add custom template page programmatically ).

But I need to specify the template used by that page. The page has to use the template in my_plugin_folder/templates/mytemplate.php . The template cannot be in the current theme folder structure.

Once the page is created, then the plugin will open a new tab with www.example.com/mycustompage (assume plugin code creates a unique/unused page name).

How do I specify the template that will be used by the programmatically-created-page when I call the created page (as in www.example.com/mycustompage ? And, the template to use is not in the current theme folder.

Added

Note that I am creating HTML code 'manually' by using The Loop to output post content. The HTML code is not being displayed on a WP page. So there is no WP JS used that converts emoji HMTL characters into the emoji graphic.

If I was to create a Page that uses a simple template to display only posts (the template does not use wp_header/sidebars/footers, etc), and then display that page, the emoji are shown as graphics in the browser. If I copy that page (in the browser) and paste that into Word, the graphics are in Word -- because they are on the browser page.

But, I am creating all of the HTML (of the post content) with PHP code. And I haven't found a good way to convert emoji HTML code to emoji graphics.

So, I figured that (instead of creating HTML code for the entire page) using a simple template that is used by a Page that I create programatically would allow WP to use that Page (and all posts content) that would show emoji graphics. But templates are normally in the theme folder, and as a plugin, I need to use my template (that is stored in the plugin's folders). (I assume that it is not 'polite' for my plugin to write a template file to the theme's folder.)

So, the question: create a Page programatically that uses a template file that is contained in the plugin's folder, not the theme's folder. Using a template allows the emoji graphics to be displayed by WP. Then I can copy/paste the generated page into Word, and the graphics will be there.

  • Can you provide some context as to why you're doing this? What problem does this solve? It will help in determining the best course of action, or if what you're trying to do is even possible or the best action – Tom J Nowell Apr 21 '18 at 0:42
  • My plugin needs to generate a page with all posts, in a browser window, so that emojis (not just emoticons) are rendered. Emoji rendering is handled by the browser. The rendered page is then copied and pasted into Word for ebook creation. Copying just the HTML doesn't copy emojis (as graphics), just their codes. The only way to get a rendered page with emojis into Word is via a copy/paste. – Rick Hellewell Apr 21 '18 at 4:02
  • So your actual problem is that you need to be able to copy paste a post into Word, but the emoji won't copy over? And you've come up with a solution but haven't figured out how to implement it? Maybe you should ask about the original problem instead? This seems like a very overcomplicated way to do it – Tom J Nowell Apr 21 '18 at 15:31
  • No, the raw HTML code, which includes codes for emoji, are not rendered properly when imported into Word. The generated HTML with emoji codes looks fine in browser, because (I think) the browser renders the emoji graphics. If I copy the rendered page into Word, the emojis are visible there. So my question is related to how to have my plugin generate a page that will use a template that is in my plugin folder, not the current theme folder. I think that @Milo's solution might be helpful, but it may also require creating a page and storing the template as part of that page's meta data. – Rick Hellewell Apr 21 '18 at 18:53
  • I don't understand why the additional template is required, the problem still appears to be that you can't copy paste the emoji into Word without issue. Perhaps I misunderstand because you gave a technical response? It still isn't clear what this output is for, my only theory is that you're creating child pages so that you can point the user there if they need to copy paste, because if they copy paste the actual page the emoji don't get copied. Perhaps if you explained what you're actually trying to do rather than describing the how of it then it would make more sense – Tom J Nowell Apr 21 '18 at 21:59
0

The question was about how to use a template that is not in the theme's folder, but in a plugin's folder. The template needs to be used for a specific page. The need for the answer doesn't matter. There is a need; how do I do it?

And the answer is to use the template_include filter (see here) . Here's my explanation of my answer:

The plugin that I am developing creates a page. That page then needs to use a template that the plugin provides. I could do it by

  • creating a whole new theme (too much work - overkill);

  • copying the template to the theme's folder (not polite to the theme);

  • creating a child theme (overkill again);

  • putting the code in the functions.php file (not polite or good to edit a theme's function.php file; better in a child theme, but that's no good);

  • setting the pages meta to specify a template by name (but that requires a template in the theme's folder);

  • or changing the template used by the Page (hence the question).

The code I used to do this is as follows. The template file I want to use is in the plugin's templates folder. My plugin has previously created the "My Custom Page" page.

// include our template
add_filter( 'template_include', 'use_our_page_template', 99 );

function use_our_page_template( $template ) {

    if ( is_page( 'My Custom Page' )  ) {
        $new_template =  plugin_dir_path( __FILE__ ) . 'templates/mycustomtemplate.php';
        return $new_template;
    }
return;

The is_page() function can use the ID# of the page, or it's title, or it's slug (see docs). I use the page title as my parameter.

Note that if you edit the "My Custom Page", you will not see the name of the template being used in the Template field of the editing screen. That doesn't bother me, but could probably be fixed. Doesn't matter; my template is used to output the page.

I put the above code in my plugin's code. The filter is available as long as the plugin is enabled. Yeah, maybe a slight performance hit because the filter is enabled for the whole site, but acceptable to me. And I could probably change the page name to make it fully unique.

So, my solution solves the question: how to set a Page to use a template outside of the theme's folder. The other answers, while containing some useful information, did not answer the original question.

And it works.

(Note: this question from 2010 pointed me to the ultimate solution.)

-1

I'm going to ignore your attempted solution and address the original problem:

Emoji are converted into HTML, preventing them from showing when copy pasted into MS Word

This is because a script turns all the emoji on the page into SVG icons. Creating child pages for every single page, and a dedicated template to solve this problem is completely unnecessary.

If you look at the source of the page, the emoji are there, intact, but get converted into SVG images by javascript:

enter image description here

enter image description here

enter image description here

Here we see they are saved as emoji:

enter image description here

Here they are intact in the pages source:

enter image description here

So The Real Problem

WP Emoji takes the emoji that you can copy into Word, and turns them into SVG icons so that users on browsers and OS's without emoji support can still see them. This breaks copy paste in MS Word.

Of note, it does not have the same effect elsewhere, e.g. here is Sublime Text:

enter image description here

Google Chrome:

😱🔮💧

enter image description here

Solution: Disable the WP Emoji script

You can disable this in form fields using a HTML class:

If you are a developer working on forms, you might want to disable this behaviour in your input fields and textareas. You can use the class attribute with 'wp-exclude-emoji' for this.

But for general content, this code snippet will do the trick:

remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

There are also plugins to do this for you. The end result is that emoji are no longer swapped out for images:

enter image description here

You should now be able to copy paste to Word without issue, and without a complicated setup involving additional templates and child pages

There are other avenues too:

  • Add a copy button that when clicked, fetches the original source from the REST API and puts it in the clipboard
  • When text is selected, convert the image icons back to emoji using their alt text
  • Use a modified wp emoji script that only converts if it detects that the users system is unable to render emojis. That user won't get an emoji in Word anyway no matter what you do.
  • This is great info (thanks for the time) and useful to understand how emojis are processed by WP. But that is *ot the problem: I am not using WP to generate the HTML code, but creating the HTML code manually; only using a wp_query to get the content of all posts. And that doesn't give me emoji graphics, ony emoji characters. I added to the question to clarify that. But your info is educational; thanks for the explanation (which might help others if they search for info about how WP handles emojis). – Rick Hellewell Apr 21 '18 at 23:03
  • There is no difference between emoji graphics and characters, WP simply encodes them in HTML as they should be. Your update to your question also makes little sense. Emoji graphics as you put them are the same as font glyphs, because that's exactly what they are. An emoji is a unicode character, just as the letters in this sentence are. They just occupy values far beyond the latin character space – Tom J Nowell Apr 22 '18 at 2:38
  • Again, the HTML code is not being generated by WP functions. If I create a bare-bones template, I can create emoji-graphic output via a page that uses that template. But templates have to be in the theme folder, and my bare-bones template is in my plugin's folder. Since it is a plugin, I want the features of the plugin to be usable on all sites. So, how do I use a template for a page, with the template being in a plugin folder? – Rick Hellewell Apr 22 '18 at 3:48
  • So you have a 3rd party script that runs outside of WP that generates the markup and inserts the post content directly in the database? – Tom J Nowell Apr 22 '18 at 18:01
  • The question is how to let a page use a template that is not in the theme's folder. The plugin will create a Page, but that page needs to use a template in the plugin's folder, not the theme's folder. – Rick Hellewell Apr 22 '18 at 22:06

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.