0

Wordpress has a default, for the most-part, non-editable place holder page for your blog. How can I recreate a similar page to this for use with my plugin?

When accessing the page defined as blog page within wp you can edit basic info including the permalink but not the content. The content is blanked out with the warning "You are currently editing the page that shows your latest posts."

I can see the line in the posts table for the blog page but nothing out of the ordinary there. I am aware of the blog.php specific script but struggling to find dev info on replicating such a function

TIA

  • Sounds like you are asking for more than one thing here. Do you want a blank page? Or do you want a default blog? And do you want this for plugin specifically? It is unclear what you're looking for. You can take a look at this codex page for a starter on working with database data for plugins, if that's what you're after. – zoltar Apr 12 '18 at 8:42
  • thanks @zoltar, I want a publicly accessible page that an admin can edit the permalink and page title for (and other non content details). THe content will be defined by my plugin (the content is basically a php script that then loads a page that can be submitted back to the server. I could do this via a normal page with a shortcode but as the page is fixed this seems like a clunky approach which may lead to users causing problems – Shaun Mc Apr 12 '18 at 9:15
0

You can do it by hooking admin_init action:

  1. First, you can create a option to select the page you create for your plugin. I suppose that the option will be save in wp_options table with name your_plugin_special_page.
  2. Remove editor support on that page with this action.

    add_action( 'admin_init', 'your_plugin_hide_editor' );
    function your_plugin_hide_editor() {
    
        // Get the Post ID
        if ( isset( $_GET[ 'post' ] ) ) {
            $post_id = $_GET[ 'post' ];
        } elseif ( isset( $_POST[ 'post_ID' ] ) ) {
            $post_id = $_POST[ 'post_ID' ];
        }
        if ( !isset( $post_id ) ) {
            return;
        }
    
        // Remove editor support
        if ( get_option('your_plugin_speical_page') == $page_id ) {
            remove_post_type_support( 'page', 'editor' );
        }
    }
    
  3. Then you can use admin notices to display your message to user (https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices)

Hope this help!

  • Works a treat. But throws one other question into the mix... Blog and home pages are noted as such in the table on the wp-admin page, is there a simple way of replicating this for a custom definition? – Shaun Mc Apr 13 '18 at 5:57
  • You can see this file: wp-admin/edit-form-advanced.php line 64 - 67 to see how it work. It's similiar to what I did. – Tung Du Apr 14 '18 at 5:50
0

How about using a page template? You can manipulate the content of that page however you want and the users have a simple option to select it or not. The only thing missing is disabling the editor in the backend, which you can solve by adding some custom JS.

wpse-awesome-template.php

<?php
/*
Template Name: My awesome template
Template Post Type: page
*/
?>

Enables the page template functionality (only for pages, none of the other (custom) post types). Use any code you want inside that template.

Now you have the basic functionality, but users can still add content to it (though you may choose not to display it). To solve this confusion, you can e.g. hide the content area with some custom JS. (conditional enqueue taken from this answer)

functions.php

<?php
add_action('admin_enqueue_scripts', 'wpse1204_admin_enqueue');
function wpse1204_admin_enqueue($hook) {
     wp_register_script('wpse-custom-template', plugins_url('assets/custom-template.js', __FILE__), array('jquery'));

    // only enqueue for creating/editing pages
    if ( $hook == 'post.php' && $screen->post_type == 'page' ) {
        wp_enqueue_script('wpse-custom-template');
    }
}

assets/custom-template.js

(function($){
    var $template = $('#page_template');
    var checkAndToggleContent = function() {
        // get currently selected template
        var selectedTemplate = $template.find('option:selected').val();

        // if template is the one, hide content
        if (selectedTemplate == 'wpse-awesome-template.php') {
            $('#postdivrich').hide();
        }
        // otherwise show content (again)
        else {
            $('#postdivrich').show();
        }
    };

    $(document).ready(function(){
        // do initial check if template is selected
        checkAndToggleContent();
        // if template selection changes, check again
        $template.on('change', checkAndToggleContent);
    });
})(jQuery);

This code is untested (as a whole), but should point you in one possible direction.

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.