Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a template in Wordpress? [closed]

Is there a way to hide a template file in admin?

For example I have a template that should only be available if a specific plugin is installed, and I already know how to check if plugin is active. But how do I hide the template?

For example I want to hide "Blogger Redirection"-template bellow:

Image is just an example...

I have found several links, but all of the solutions seems deprecated.

EDIT: If anyone is interested in how I check if pluin is active I do it with the following function:

function isPluginActive($plugin){
        if ( in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
        {
            return true;
        }
        return false;
    }
like image 352
jamietelin Avatar asked Nov 26 '12 14:11

jamietelin


People also ask

How do I hide a page on WordPress?

Simply edit the post or page that you want to protect. Under the 'Document' setting in your WordPress editor, click on the link next to the 'Visibility' option. This will show the visibility options available in WordPress where you can make a post or page public, private, or password protected.

How do I make my page title invisible in WordPress?

Go to Pages -> All Pages and click Edit under the particular page whose title you want to hide. Press the Settings button and scroll down to the Hide Page and Post Title section. Tick the checkbox to have the page title hidden, then press Update.

What is Template override WordPress?

WordPress templates can be overridden to create a different layout of the content or to add additional content directly on the template. Usually plugins provide basic templates for their content which can be then overridden by themes.


2 Answers

Update - a word of caution as pointed out by andrew in the comments below:

Use this code with caution, If you have any pages using the template that you removed from the select, updating the page will cause it to revert to the default template


I'm not sure if there is a PHP way of doing this - you'll have to look in the WP_Theme class, but from my first look it might not be possible because in order to get all templates, the class utilizes an internal function called scandir() which scans the current theme directory and gets all .php files from there. It then looks for the corresponding Template Name: identifier and if it's present it gets added to the templates list.

So instead I suggest, that you add a little JS that will remove this option from the page template select. Here's a code snippet:

function my_remove_page_template() {
    global $pagenow;
    if ( in_array( $pagenow, array( 'post-new.php', 'post.php') ) && get_post_type() == 'page' ) { ?>
        <script>
            (function($){
                $(document).ready(function(){
                    $('#page_template option[value="sidebar-page.php"]').remove();
                })
            })(jQuery)
        </script>
    <?php 
    }
}
add_action('admin_footer', 'my_remove_page_template', 10);

This will remove the template sidebar-page.php from the dropdown. The conditionals are so that the script is only added on add and edit screens of pages.

Adjust to your case and enjoy :)

like image 80
Nikola Ivanov Nikolov Avatar answered Oct 02 '22 01:10

Nikola Ivanov Nikolov


WordPress doesn't provide any hook, so you can't remove it from PHP.

One alternative that I could think of is that you can remove it dynamically using JavaScript. Include a snippet of JavaScript in the add post page which will remove the option from the select element.

I know this is not a clean solution, but I think this is the only possible way as of now.

Edit:

Another option is to copy the template file to the theme directory when the Plugin is activated and remove the template file when the Plugin is deactivated.

This assumes that the template is used only by this Plugin.

like image 22
Sudar Avatar answered Oct 02 '22 00:10

Sudar