WordPress.org

Ready to get started?Download WordPress

Forums

Using a plugin to add a static PHP page (3 posts)

  1. danblaker
    Member
    Posted 3 years ago #

    I'm trying to use plugins to abstract custom features from the theme design and site content, and I've run into a roadblock.

    One such feature is a Location Picker. Here's how it's currently implemented:

    • A "show-selected-location" function is called from header.php
    • A "show-selected-location" action is added in functions.php, with the appropriate code to display the location data from a cookie, along with a link to a page where the user can change their location.
    • The link currently points to a WP Page, which is assigned a custom page template named "location-picker.php"
    • The page template uses get_terms() to build a select list for all of the values in the "Locations" custom taxonomy, and some Javascript to set the cookie based on the user's selection.

    After making it work in the theme, I'm now trying to put the functionality into a plugin. I've got everything working great so far—the current location and link are showing up correctly—but I can't seem to figure out the Location Picker page, which I'm trying to move out of the database and into a static file in the plugin. I put the code from "location-picker.php" into a php file within the plugin folder, and I am able to link to the page and display plain HTML; however, I can't use any WP functions such as get_terms() or get_header() or anything.

    Should I be using a different approach? I really like the idea of keeping the dynamic functionality out of the theme and having it self-contained in a plugin. I could include the location picker code in a hidden div and use a lightbox to display it, but I want to avoid including the get_terms() query on every page.

    I feel like I'm missing something really basic, but it's hard to tell—this is a tough problem to google...

  2. davoscript
    Member
    Posted 3 years ago #

    Hello danblaker:

    Let me see if i got the idea, so basicaly you want to have a php page wich you can code however you want, but you still want to use the WP functions, am I right? If that's the case I definetely think you should use Page Templates, this way you can create a html/php file inside the WP file system, then you can create a new page and use your php file as a template of the page.

    Benefits:
    - You can code yout php file however you want.
    - The page is still in WordPress so you manage it.
    - You can reuse the page template.

    Hope this helps.
    Regards.

  3. danblaker
    Member
    Posted 3 years ago #

    Thanks for the reply, but that's not really what I'm trying to do.

    I don't want to reuse the page template, because it is specific to this custom page content (i.e. there will only be a single Location Picker page on the site). Beyond that, I don't want to store the page in the WP database for a variety of reasons (e.g., I want to enable the entire functionality by clicking "Activate" for this feature in the Plugins list).

    I just finished implementing it using the lightbox approach I defined above—hooking into the 'wp_footer' action to insert a hidden location picker, which I display in a lightbox when the user clicks the link. But this adds an unnecessary "get_terms()" call to every page on the site; I'd prefer to only run that query when the user actually goes to change their location, i.e. only when they go to the Location Picker page.

Topic Closed

This topic has been closed to new replies.

About this Topic