[wp-hackers] P2 in a subfolder

Nikola Nikolov nikolov.tmw at gmail.com
Fri Sep 13 14:48:59 UTC 2013


You can find the sample code here - http://pastebin.com/fw9d1Ccj . I've
fixed it up a bit, so I might have a syntax error somewhere(haven't tested
it at the moment).

Basically the above code consists of two functions
- print_requested_template_part() and get_main_site_part().

The first one ( print_requested_template_part() ) goes to your main site's
functions.php. It will hook to the "init" action hook and will look for GET
requests that contain two keywords - "including_template_part" and
"load_part" in the request. Additionally only requests coming from the same
IP will be answered(for added security - while testing you can disable the
check of $_SERVER['REMOTE_ADDR'] - you can also simply use wp_remote_post()
instead and only check for POST requests ).
If such a request is caught, the function tries to find a function called
"render_{$_GET['load_part']}" - so what this means is that if
$_GET['load_part'] is "footer_html", the script will look for a function
called "render_footer_html"(note that even if $_GET['load_part'] was
"footer-html", the function name would've been the same, since all "-" are
replaced with "_").
If the function exists, we check for the presence of $_GET['params'] -
which allows you to pass parameters to that function(you can comma-separate
them in order to pass more than one parameter) and we call the function
with or without parameters. Then the script exits, thus ending the output.

The second function( get_main_site_part() ) goes into your second site(in
this case the P2 sub-site). You call this function like so( following the
example from above with "footer_html" ):

echo get_main_site_part( 'http://example.com/', 'footer_html',
'&params=param1,param2,param3' );

The first parameter is the URL of your main site, the second one is the
"part" to be loaded(so part of the name of the function to be called) and
the third optional parameter is any query args that you want to add to the
requested URL( in the example I'm sending the "params" key, but you can
pretty much send anything you want ).

The function would check to see if we have a valid transient corresponding
to this requested part and if so it would return that. Otherwise it uses
wp_remote_get() to get the required HTML.

Currently the function only caches the response for 300 seconds(5 minutes),
but you can change that in the "$cache_lifetime = 300;" line.


That's pretty much it - not super complicated, probably not the best
solution, but it has worked for me in the past :)


On Fri, Sep 13, 2013 at 5:05 PM, Nikola Nikolov <nikolov.tmw at gmail.com>wrote:

> What I've done before(but is most-likely not the best solution) is to ping
> the other site with a request to load only the header, or only the footer(I
> had a couple of functions that do that) and then store the HTML in a
> transient for caching. If you don't have any dynamic things in the
> footer/header, or if they change infrequently, you can probably try this
> approach - otherwise, you'd have to figure-out a different way to do that.
>
> I can give you some sample code if you're interested.
>
>
> On Fri, Sep 13, 2013 at 5:02 PM, David Ernst <david at ernsts.us> wrote:
>
>> Would a symlink from htdocs/subfolder/wp-content/themes/p2/footer.php
>> to htdocs/wp-content/themes/twentytwelve/footer.php work? Seems like it
>> would have trouble knowing which wp_terms to use for menus.
>>
>>
>> On Thu, Sep 12, 2013 at 7:47 AM, David Ernst <david at ernsts.us> wrote:
>>
>> > Seems simple enough, thank you for the suggestion, Shea! Are there also
>> > straightforward strategies to reuse certain theme aspects, e.g. header
>> and
>> > footer?
>> >
>> > Cheers,
>> > David
>> >
>> >
>> > On Thursday, September 12, 2013, Shea Bunge wrote:
>> >
>> >> You should make your P2-powered area a separate WordPress install.
>> Then,
>> >> you can set the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE constants
>> in
>> >> wp-config.php. See
>> >>
>> http://codex.wordpress.org/Editing_wp-config.php#Custom_User_and_Usermeta_Tablesformore details.
>> >> > Date: Thu, 12 Sep 2013 00:44:30 -0400
>> >> > From: david at ernsts.us
>> >> > To: wp-hackers at lists.automattic.com
>> >> > Subject: [wp-hackers] P2 in a subfolder
>> >> >
>> >> > Short question:
>> >> >
>> >> > What's the smartest way to configure a P2 child-theme to a subfolder
>> of
>> >> my
>> >> > root (TwentyTwelve-based) WordPress install? This new subfolder would
>> >> > need its own wp_posts table but use the same wp_users.
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > The details:
>> >> >
>> >> > The current set up uses a child-theme of TwentyTwelve. There's a
>> static
>> >> > homepage and most of the site is organized around Pages. So
>> >> > foo.com/bananais the permalink for the Page 'Banana'. There are
>> about
>> >> > 40 or so Pages at
>> >> > the moment.
>> >> >
>> >> > And foo.com/blog is the current Blog Index, so the Post 'Pineapple'
>> >> lives
>> >> > at foo.com/blog/pineapple . There are only a few posts right now,
>> and
>> >> only
>> >> > admins can post.
>> >> >
>> >> > WooCommerce is also installed, using the /shop/ subfolder. It treats
>> >> > eCommerce orders as a custom post type.
>> >> >
>> >> > Then there's another custom post type 'fish' with some
>> >> > elaborate accompanying wp_postmeta that's rendered and semi-editable
>> on
>> >> > foo.com/fishtank, using a htaccess rewrite + custom Action URL. Each
>> >> user
>> >> > can only see the fish-type posts they created.
>> >> >
>> >> >
>> >> > Now what I'd like to do is create a discussion space at
>> foo.com/talk/.
>> >> > Unlike the current blog index, all users (& maybe guests? Haven't
>> >> decided
>> >> > yet) should be able to post here. The objective is for this to have a
>> >> > real-time chat room feel, but like a forum also allow for longer
>> >> > posts, keep an archive, and provide links to individual threads.
>> Matt's
>> >> > post "How P2 Changed Automattic" <
>> >> > http://ma.tt/2009/05/how-p2-changed-automattic/> inspired me to
>> think
>> >> P2
>> >> > could work well.
>> >> >
>> >> > Because this may generate tons of content, and the current wp_posts
>> is
>> >> > already pretty busy, I'd rather give it its own wp_posts table, if
>> >> possible.
>> >> > Is this an unnecessary concern and over-complicating things? I'd
>> still
>> >> like
>> >> > to use the current wp_users table.
>> >> >
>> >> >
>> >> > Does this setup make sense? Has it been done before? Am I being
>> crazy?
>> >> Is
>> >> > there documentation for these sort of scenarios? Would it be much
>> >> simpler
>> >> > to use a fresh install of WordPress in a subfolder? Is there a sane
>> way
>> >> to
>> >> > share wp_users between these two spaces?
>> >> >
>> >> >
>> >> > I'd appreciate any thoughts, concerns, and suggestions :-)
>> >> >
>> >> >
>> >> > Much thanks,
>> >> >
>> >> > David
>> >> > _______________________________________________
>> >> > wp-hackers mailing list
>> >> > wp-hackers at lists.automattic.com
>> >> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>> >>
>> >> _______________________________________________
>> >> wp-hackers mailing list
>> >> wp-hackers at lists.automattic.com
>> >> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> >>
>> >
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>
>


More information about the wp-hackers mailing list