WordPress template: Object not found

I created a new PHP template inside my root directory alongside index.php named 'template-insert-posts.php. However, when I try to access the following URL: http://localhost/wordpress/template-insert-posts.php, I get the following error:

Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

Read More

I am able to access the index.php template by entering the URL http://localhost/wordpress/index.php

Now, both index.php and template-insert-posts.php are located in the same path ie /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen. Then, why is my template-insert-posts.php not accessible? Is there something I’m missing before trying to access a custom template in WordPress? Also, here’s the source code of the file:

template-insert-posts.php

<?php
$postTitleError = '';

if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {

    if ( trim( $_POST['postTitle'] ) === '' ) {
        $postTitleError = 'Please enter a title.';
        $hasError = true;
    }

    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'pending'
    );

    wp_insert_post( $post_information );

}

$post_id = wp_insert_post( $post_information );
if ( $post_id ) {
    wp_redirect( home_url() );
    exit;
}
?>

<?php
get_header(); ?>


<form action="" id="primaryPostForm" method="POST">

    <fieldset>
        <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>

        <input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>"/>
    </fieldset>

    <fieldset>
        <label for="postContent"><?php _e('Post Content:', 'framework') ?></label>

        <textarea name="postContent" id="postContent" rows="8" cols="30" class="required" <?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?>></textarea>
    </fieldset>

    <fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button type="submit"><?php _e('Add Post', 'framework') ?></button>
    </fieldset>

</form>


<?php get_footer(); ?>

Related posts

1 comment

  1. That happened because that’s not how templates work in WordPress. You don’t create a specific file for each page in your website. You create pages, and then you assign templates to them, and let WordPress figure out how to access and create accesses to those pages. Trying to direct access one of those files will yield a 404 because WordPress due to the fact that a page (in wp land) with that name does not exist.

    The fact that it did work when you tried going directly into index.php is because , in the template hierarchy, index.php is the last file WP looks for when searching for a template from which to display your page. As this file is a must-have in every theme, it was found, and thus no 404s.

    There’s something called permalinks which allows you to create friendly URLs to your site without changing any names in your template files. That would be impossible if your URLs were directly attached to the file names.

    WordPress Theme Handbook has a pretty neat article on page templates, and the codex can give you some hints on how to get started with them. Smashing Magazine has an amazing article, written by Nick Schäferhoff, which gives detailed instructions on how to create a page template.

    In a nutshell, and taken from WordPress theme Twentyfourteen, a page template works somewhat like this

    <?php
    /**
     * Template Name: Full Width Page
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header(); ?>
    
    <div id="main-content" class="main-content">
    
    <?php
        if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
            // Include the featured content template.
            get_template_part( 'featured-content' );
        }
    ?>
    
        <div id="primary" class="content-area">
            <div id="content" class="site-content" role="main">
                <?php
                    // Start the Loop.
                    while ( have_posts() ) : the_post();
    
                        // Include the page content template.
                        get_template_part( '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();
                        }
                    endwhile;
                ?>
            </div><!-- #content -->
        </div><!-- #primary -->
    </div><!-- #main-content -->
    
    <?php
    get_sidebar();
    get_footer();
    

    Interestingly enough, the comment part Template Name: Full Width Page makes this template global, which means it can be accessed anywhere within your site (take a look at the docs for more detail on hierarchy). Once you have something like that on your template, create a page, and then assign you template to it. You should be golden!

    EDIT:

    Still in time, check this awesome infographic that shows how templating works in WP land, and how every page eventually renders to index.php, if no other template file is found.

Comments are closed.