I need to execute my personal php code in wordpress to print data from a mysql database. I've to execute it only on pages.

My idea is to create a file page-pagename.php, copy the body of the page.php template and change this code:

<?php while ( have_posts() ): the_post(); ?>

        <article <?php post_class('group'); ?>>

            <?php get_template_part('inc/page-image'); ?>

            <div class="entry themeform">
                <?php the_content(); ?>
                <div class="clear"></div>


        <?php if ( ot_get_option('page-comments') == 'on' ) { comments_template('/comments.php',true); } ?>

    <?php endwhile; ?>

with my personal php code.

I've tried to do this and everything seems to work fine, but I'd like to know from your experience if this could lead to any bug.


  • 1
    Everything can lead to a bug, I'm not sure what you're trying to ask here, if you're looking for reassurance, go ahead and do it and if it fails, ask here, make mistakes, and make them sooner rather than later, that's how you learn
    – Tom J Nowell
    Commented Dec 11, 2014 at 20:15
  • I don't think that I'm the first one that want to have PHP code in pages, so maybe that someone could share his experience to find out which is the best/most common way... Of course, before asking here I've made some reasearches on google. Commented Dec 11, 2014 at 20:23

1 Answer 1


If you have a page, and need to run PHP code that isn't in your standard page template, you can:

  • Use a custom page template
  • Create a template with page-pagename.php
  • Implement and use a shortcode

All three are valid and suitable methods for doing this, however, do not:

  • Try to let users place actual PHP code in the post content ( MASSIVE security risk )

There are some other more esoteric options that would take longer to explain, and don't involve a post ( of type page ). Those aren't superior in any way other than that they require a greater knowledge of WordPress for the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.