How to Disable the Content Editor for a Specific Page Template in WordPress

Home » Snippets » How to Disable the Content Editor for a Specific Page Template in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you a WordPress user who wants to disable the content editor for a specific page template? Perhaps you have a page that requires a unique layout or functionality, and you don’t want content editors to have access to modify the layout. In this article, we will guide you through the process of disabling the content editor for a specific page template in WordPress. By the end, you’ll have the knowledge to restrict content editing privileges and achieve a more controlled editing experience for your site.

					function wpturbo_disable_content_editor() {
    // Get the current post
    $post = get_post();

    // Check if the post has a specific page template
    if (is_page_template('template-name.php')) {

        // Remove the content editor
        remove_post_type_support($post->post_type, 'editor');
    }
}
add_action('admin_init', 'wpturbo_disable_content_editor');
				

In this tutorial, we will learn how to disable the content editor for a specific page template in WordPress. This can be useful when you want to have a static page layout and prevent users from modifying the content of that particular page.

Let’s dive into the code snippet:

function wpturbo_disable_content_editor() {
    // Get the current post
    $post = get_post();

    // Check if the post has a specific page template
    if (is_page_template('template-name.php')) {

        // Remove the content editor
        remove_post_type_support($post->post_type, 'editor');
    }
}
add_action('admin_init', 'wpturbo_disable_content_editor');

The wpturbo_disable_content_editor() function is responsible for disabling the content editor. First, we use the get_post() function to retrieve the current post object. This will allow us to check if the post has a specific page template.

Next, we use the is_page_template() conditional function to check if the current page has the desired template. In this example, we are checking for a template named template-name.php. You can replace 'template-name.php' with the actual name of your page template.

If the current page has the specified template, we proceed to remove the content editor. The remove_post_type_support() function is used to remove support for a specific feature from a post type. In our case, we want to remove the editor functionality from the current post type.

We pass two parameters to the remove_post_type_support() function. The first parameter $post->post_type specifies the post type for which we want to remove support. Since we are getting the current post object, $post->post_type will give us the post type of the current page.

The second parameter 'editor' specifies the feature we want to remove. In this case, we want to remove the content editor.

Finally, we hook the wpturbo_disable_content_editor() function into the admin_init action. This ensures that the function is executed during the WordPress admin initialization process.

Once you save and apply this code to your WordPress website, the content editor will be disabled for any page that uses the specified page template. This allows you to have more control over the content displayed on those specific pages.

Register an account to save your snippets or go Pro to get more features.