Create page (page) only in PHP code of WordPress

4

How to create a page only by code? Because it is static, that is, you do not need to enter content through the panel, but to have the URL of that page, you must create a page, even if it is empty, without content. What I need is to have this page only in the code.

Ex page-estatico.php :

 <?php get_header(); ?>

<main>
    <h1> ESTÁTICO </h1>
    <h3>Página criada somente no código</h3>
</main>
    
asked by anonymous 24.10.2014 / 15:20

2 answers

4

I have already written plugins that do similar things but using a template inside the plugin's own folder and intercepting with template_redirect . I thought there was something like that already written but I did not find it. I did a search and found Virtual Page within Theme Template and even published the same solution there at the time of intercepting parse_request .

A new rewrite_rule is included with the virtual address and when checking request , if it is the virtual address, print a template simulation and stop the rest of the execution.

Install the plugin and click Update in the Permalinks ( /wp-admin/options-permalink.php ) options. Then visit: http://example.com/virtual . The ideal is to register a flush_rewrite_rules on activation and deactivation, but in my tests it is not working on deactivating the plugin, so I prefer to suggest the manual update.

<?php
/*
 * Plugin Name: (SOPT) Página virtual
 */

add_action( 'init', 'init_sopt_38020' );
add_filter( 'query_vars', 'query_vars_sopt_38020' );
add_action( 'parse_request', 'parse_request_sopt_38020');

function init_sopt_38020() {
    add_rewrite_rule( 'virtual/?', 'index.php?virtual=new', 'top' );
}

function query_vars_sopt_38020( $query_vars ) {
    $query_vars[] = 'virtual';
    return $query_vars;
}

function parse_request_sopt_38020( $wp ) {
    if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
        get_header(); ?>
        <div id="primary">
            <div id="content" role="main">
                Olá, mundo!
            </div>
        </div>
        <?php get_footer();
        exit;
    }
}
    
24.10.2014 / 17:08
1

Depending on the usage you will have for the page-estatico.php file, a Wordpress page template can be created by placing at the beginning of the file page-estatico.php the following code:

<?php
/*
Template Name: Pagina Estática
*/
?>

///Seu conteúdo

Save to your template directory.

When you do this, go to Wordpress Admin, create this page you need and, in the Modelo de Página option, the Página Estática template should appear. Soon!

    
24.10.2014 / 18:56