0

I'm currently deciding whether or not to use WordPress or write my own code for a future website but I need to know one thing before I can make my final decision.

Is it possible, with WordPress, to insert PHP scripts into a single page. About 40% of my pages will have their own PHP script behind it and I need to know if such a thing is possible (and if so, what I should be looking at).

Thanks.

1

3 Answers 3

1

Rather than insert PHP scripts into each page what you could do is execute PHP scripts using action hooks with conditional tags or modify the default output of an existing function using a filter hook with conditional tag for each page.

You could also create template tags or write your own functions in a separate file and execute on any page in a custom function with conditional tag using the page I.D.

Filter Example

add_filter( 'the_content', 'execute_before_content' ); 

function execute_before_content( $content ) { 

if ( is_singular('page') && function_exists('your_function')) {

    $before_content = your_function();

    $content = $before_content . $content;

    }

return $content;
}

Action Hook Example:

add_action( 'loop_start', 'your_function' );
function your_function() {
if ( is_page('007') && function_exists('your_template_tag')): 
        your_template_tag();
endif; 
}
0

Every page can use it's own individual template if needed, have a look at the wordpress template heirarchy image for a bit more info. Basically a page will default to using page.php but if you provide page-{slug}.php where {slug} is the slug of the page, it will use that template instead.

So it's entirely feasible to have a separate 'page' file for each and every page, with code specific to that page on it.

Additionally, if some of the code is shared between multiple pages, just set up a custom page template and assign that template to those pages that use it.

Wordpress template heirarchy: http://codex.wordpress.org/Template_Hierarchy

Hope that helps.

0

Can you elaborate on the use-case? As Brad stated, using hooks is the the best method to accomplish this functionally. You probably would want to create a plugin that you could maintain, upgrade and reuse... especially if this is for a client!

http://codex.wordpress.org/Writing_a_Plugin

2
  • 1
    Hi Tonymazz, this should be a comment and not an answer.
    – Wyck
    Commented Feb 13, 2015 at 16:00
  • Sorry new here, absolutely right.
    – tonymazz
    Commented Feb 13, 2015 at 16:39

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.