Create page (page) only in Wordpress PHP code

Asked

Viewed 4,525 times

4

How to create a page just for the code? Because it is static, IE, you do not need to insert content through the panel, but to have the URL of this page, you need to create a page, even if it is empty, no 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>

2 answers

5


I’ve written plugins that do similar things but using a template inside the plugin’s own folder and intercepting with template_redirect. I believed I had something similar already written but I didn’t 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 the parse_request.

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

Install the plugin and click Upgrade in the Permalinks options (/wp-admin/options-permalink.php). Then visit: http://example.com/virtual. The ideal is to register a flush_rewrite_rules on the activation and deactivation, but in my tests is not working on the deactivation of 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;
    }
}

1

Depending on the use you will have for the file page-estatico.php, can be created a Wordpress page template 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.

Done this, enter Wordpress Admin, create this page you need and, in the option Modelo de Página, should appear the model Página Estática. Ready!

  • In fact, the question seeks exactly to avoid this solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.