1

I think this is a simple question:

I'm building a plugi, and up until now, if i wanted to include a php file, i would have to know the page_id - i would put the following command in page.php (or whatever page template i'm using):

if (is_page(4552)) {
     include WP_PLUGIN_DIR .'/file_to_include.php';
}

now, A bit more background: i now have a hook that creates a post on activation, and saves the post_id using update_options. so, that's how i know what $page_id is.

So, to summarize - how do i dynamically write the code above - in my plugin - assuming I know the $page_id?

Thanks for helping!

1 Answer 1

0

Make sure to add it to a hook that fires at or after 'wp' ;

Example:

add_action( 'wp', 'your_function_name');

function your_function_name(){
    global $post;

$your_page_id = get_option('your_option_name');

    if( $post->ID == $your_page_id ):
//your include 
endif;
}
7
  • hmm, that's good, but it doesn't work - it loads my html before EVERYTHING, even the DOCTYPE.....
    – raingod
    Apr 8, 2014 at 23:25
  • I didn't realize that your include was html output. Where do you want it to show up? (in the header, right after the body tag?) Apr 8, 2014 at 23:33
  • right after the body tag sounds perfect :)
    – raingod
    Apr 8, 2014 at 23:38
  • Could you let me know what theme you are using, because that would change what the earliest hook after the opening body tag would be Apr 8, 2014 at 23:41
  • You could try 'the_post' in place of 'wp'. that might work for you Apr 8, 2014 at 23:44

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.