How can we add a PHP page to WordPress?

How can we add a PHP page to WordPress?

Asked on October 24, 2018 in Wordpress.
Add Comment


  • 4 Answer(s)

    As the initial duplicate the post.php or page.php in our theme folder which will be present at (under /wp-content/themes/themename/).

        We have to rename the new files as the templatename.php (where the templatename at which we want to call our new template). To add our new template to the list of available templates, enter the following at the top of the new file:

    <?php
    /*
    Template Name: Name of Template
    */
    ?>
    
    Answered on October 24, 2018.
    Add Comment

    Try this code:

    <?php
        require_once(dirname(__FILE__) . '/wp-config.php');
        $wp->init();
        $wp->parse_request();
        $wp->query_posts();
        $wp->register_globals();
        $wp->send_headers();
        // Your WordPress Functions here...
        echo site_url();
    ?>
    

    As we can just simply perform any of the wordpress functions . That this could assume that our .php file is within the root of our wordpress site where our wp-config.php file is located.

    Answered on October 24, 2018.
    Add Comment

    As in the contents of our filename.php which, is used for our convenience, is located at the place of the root WP directory:

    <?php
        require( dirname( __FILE__ ) . '/wp-blog-header.php' );
     
        $uri = $_SERVER['REQUEST_URI'];
        $pieces = explode("?", $uri);
        $post_id = intval( $pieces[1] );
     
        // og:title
        $title = get_the_title($post_id);
     
        // og:description
        $post = get_post($post_id);
        $descr = $post->post_excerpt;
     
        // og:image
        $img_data_array = get_attached_media('image', $post_id);
        $img_src = null;
        $img_count = 0;
        foreach ( $img_data_array as $img_data ) {
            if ( $img_count > 0 ) {
                break;
            } else {
                ++$img_count;
                $img_src = $img_data->guid;
            }
        } // end og:image
    ?>
    <!DOCTYPE HTML>
    <html>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=yes" />
    <meta property="og:title" content="<?php echo $title; ?>" />
    <meta property="og:description" content="<?php echo $descr; ?>" />
    <meta property="og:locale" content="en_US" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="<?php echo site_url().'/your_redirect_path'.$post_id; ?>" />
    <meta property="og:image" content="<?php echo $img_src; ?>" />
    <meta property="og:site_name" content="Your Title" />
    </html>
    
    Answered on October 24, 2018.
    Add Comment
    <?php /* Template Name: CustomPageT1 */ ?>
    
    <?php get_header(); ?>
    
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            // Start the loop.
            while ( have_posts() ) : the_post();
    
                // Include the page content template.
                get_template_part( 'template-parts/content', 'page' );
    
                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
    
                // End of the loop.
            endwhile;
            ?>
    
        </main><!-- .site-main -->
    
        <?php get_sidebar( 'content-bottom' ); ?>
    
    </div><!-- .content-area -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    Answered on February 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.