• I’m working on a plugin that pulls pet info from Petfinder and displays it in the form of a gallery of photos, each photo being a link to a new page containing additional info on a particular pet (you can see it here – https://www.specialonescatrescue.com/adopt/

    The plugin is currently one single file. What I would like to know is if I can split it into 2 separate files, both residing in the plugin folder as follows:

    1. The main plugin file, to contain the main gallery.
    2. To contain the code to display an individual pet record.

    Is there a way to do this? (The only WP functions I was able to find, that provide access to the plugin folder, provide only absolute paths and thus result in a ‘server not found’ error. )

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    You can break up your code in multiple files for organizational purposes, but your main file needs to reference them, directly or indirectly, with include, require, or similar statements. Thus, as far as the parser is concerned, everything is still one long sequence of code.

    If your separate code page is accessed directly by a browser request, it may not need to be included, provided the code is self contained and is not referenced by any other plugin code. But then such pages often need to load the WP environment by including wp-load.php, which in turn includes your activated plugin. So in a sense, everything is still indirectly referenced.

    Thread Starter Sunny_Balanga

    (@sunny_balanga)

    I don’t think I expressed my problem properly. Here’s another go:

    The plugin’s functionality: The main plugin file displays thumbnails of all the pets. Each pet thumbnail serves as a link to a separate page which contains that pet’s detailed info

    The Problem: This separate page currently resides in the theme folder (and works perfectly,) but I want it to be a proper plugin, so I want this separate page in the plugin folder. The problem is that I can’t figure out how to access it when it’s in the plugin folder.

    Moderator bcworkz

    (@bcworkz)

    More likely I didn’t read your problem properly. Sorry.
    Something like plugins_url(pet-details.php, __FILE__); should have returned the correct url. If you are getting a server not found error, I have to think there is an issue with your installation’s configuration. I’ve used this function for the exact same purpose without issue.

    Thread Starter Sunny_Balanga

    (@sunny_balanga)

    Hey bcworkz.. Thanks for helping.. I tried this code below, but all I got was an html page with some raw php code.

    $gallery .= “id . “\””;

    I want the page that displays the single pet record to appear the same as any other theme page, but I have a feeling it’s just not possible to create a theme page outside of the theme folder. .

    I think my question needs to be – how to get a plugin to create a theme page? The objective being that the plugin user should not have to to do so.

    Thread Starter Sunny_Balanga

    (@sunny_balanga)

    Oops.. I forgot to mark the code as code.. I meant this:

    $gallery .= "<a href=\"" . plugin_dir_path(__FILE__) . "pfg_pet.php?id=" . $pet->id . "\"";

    Moderator bcworkz

    (@bcworkz)

    Well, that’s better than server not found. If php code is sent, there must be an issue with your <?php ?> tags being positioned properly.

    As far as theme pages, there is little a plugin can assume about a theme. Your page could reference the style.css with get_stylesheet_directory(). This should a least style your page with the basic <body> styles. You can’t assume any particular class or id is used, so the results would be very basic. To do more, your plugin will be locked in to only working with a particular theme.

    Honestly, I have never had a need to produce a fully styled page from a plugin, so I’m unsure of the best approach. My initial thought is to create post content, then redirect to the theme’s single content page with the usual ?p=123 url parameter request. The ID could be stored as an option, then reused to generate other content. This is less than ideal, there could be issues on a very busy site with this approach.

    The problem is plugins were not meant to provide styled frontend content. They are really intended to provide extended functionality unrelated to theme styled content.

    Thread Starter Sunny_Balanga

    (@sunny_balanga)

    I can’t imagine that what I want to do is not possible. There must be a way.

    I have seen references to the function wp_insert_page, but it does not appear in the codex. What does appear is wp_insert_post and the description reads as: “This function inserts posts (and pages) in the database.”

    I am not sure if this does what I need, nor how to use it..

    Moderator bcworkz

    (@bcworkz)

    wp_insert_post() takes a $post type array as an argument and after checking various things, inserts the data into the DB posts table. This is how you would create a post or page by script instead of using the post or page editor backend screen. The insert page variant does not exist afaik. If it did, it would probably just set post_type=>’page’ and send it on to the insert post function.

    Perhaps another approach is when the gallery is created or updated, a script generates, deletes, or modifies the necessary related sub-pages as posts. Thus the gallery links are links to other posts. Each sub-page will then be fully styled by theme as any other post will be. You can still style post content further using inline css or tag attribute style, or even (gasp) tables.

    For that matter, the sub-post need not be any more than a title and a shortcode like [petdata id="1234"] and the shortcode handler does all the heavy lifting of retrieving data and formatting content. This would be very close to your original idea, but gets away from theme reliance issues.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Multiple file plugin: Can a plugin access other files in the plugin dir?’ is closed to new replies.