Render a Wordpress page as a variable - wordpress

Is it possible to get Wordpress to render a page and return it as HTML ?
I'm thinking about a function that'd look like this :
$html = render_page($id);
echo $html; // prints the page
My goal is to have all my sections' HTML and my corresponding logic in different files and, in my home page file, merge all the different sections in the order I wish. Thus making a one-page kind of website.
Because you define a template for a page, I don't see why it's doesn't seem to be possible to do this.

I have finally found a workaround for doing exactly what I wanted :
I have all my different "pages" (they are Wordpress pages, but I use them as different sections on a one-page site) in different files.
Each file has it's own HTML and corresponding logic.
In my index.php file, I call my files this way :
require_once(locate_template('tpl-name.php'));
I'm pretty sure it's not exactly the best way of doing it, but it works for me. Wish Wordpress would allow API access to it's rendering though.

Related

How to get a WordPress template file to insert different images based on the current page?

I have a number of pages (100) which are all build individually in a visual editor (DIVI). They each have a set of 10-15 different images on, inserted into an identical 'wrapper'. Other than the images, the pages are identical - This was my first website and so I knew no better than this approach when I began. As I now understand, I could use a custom page template for these pages to allow scalability/re-design with much greater ease.
I understand how a template.php page could be created that pulls necessary page metadata to populate the title/subheadings etc for each page. I am thinking of writing a for loop to insert each image for the page into the repeating 'wrapper'. However, I am not sure how the template would know which images to pass into this loop based on the page visited?
I had a few thoughts on how this could be done (I am very inexperienced):
a) Re-structure my wp-uploads folder to match my sitemap and get the template to insert all images in the path that matches the slug
b) Upload all images for each page to the page in the back-end (not sure if this is possible) and get the template to insert all images associated with the page in question
c) Get the template to loop through a database table containing all of the image names matched to the page id on which they should be found and only insert those that match the current page id
If this changes anything, all 100 pages are child-pages of pageX and their slug begins with a common word. eg:
mysite.com/pageX/commonword-page1/
mysite.com/pageX/commonword-page2/
Since you have so many pages, I wouldn't use an if statement. The best way to handle this would be to use Advanced Custom Fields and set up an image field to show on the pages post type. Then, in your code, call the custom field and that way every page dynamically show their respective image. You can find ACF in the Plugins repository on your WordPress site or download it directly from their website.
Here is the image field documentation:
https://www.advancedcustomfields.com/resources/image/
The cleanest way is to set the field to use the URL of the image. Here is what would go in your code:
<?php if( get_field('field_slug') ): ?>
<img src="<?php the_field('field_slug'); ?>" />
<?php endif; ?>

Custom Plugin for wordpress with hierarchy of SEF pages

Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Output CCK items in custom template

I am working with a Drupal 6 site which relies heavily on CCK and views and I want to use the content in a webapp for iPhone. The webapp is built in HTML and CSS and will ultimately be converted to native using PhoneGap or similar and I want to import the formatted HTML using a JQuery ajax call. The plan is to create a view in the website which returns the data that I want for each page and then create a template for that view which outputs the data in the correct format for including.
I need to be very prescriptive with the HTML output which is where I am struggling. I'm not overly familiar with Drupal templating and what I have done seems like a lot of HTML is created when outputting a node, and it is all also contained within the site template. I need to be able to output exactly what I want e.g.
<div class="customclass">
<?php echo($cckfield) ?>
</div>
<?php foreach($cckarray as $key -> $value) {
echo(some html using $key and $value);
}?>
Hopefully the pseudocode above gives an indication of the level of control required. Is this possible using either core or with a module? Or do I need to roll my own pages and use the API rather than going for Views?
The problem with this is that there are many files collected together and merged into the output file. If you want clean HTML, you need to go through the different files that make up the final output (e.g. page.tpl, different modules files,...).
The best way to change the base HTML-output is to filter everything that you don't need out of the $node object that every site gets passed. It also holds all the CCK values in formated and unformated form as well as all other fields. SO creating a custom template with no HTML and the bare values of $node should give you what you want.

Create an Iframe from a Drupal Website

I have a drupal website. I want to generate an Iframe with content from my drupal site, that other sites can embed.
How I think this can be achieved:
Method 1: Create a php script that is autonomous from the drupal engine.
Import the configuration file and hence gain access to the database. Generate the content as a standalone webpage. Distribute the url of this script as the iframe's source url.
Problems: cannot provide drupal functionality within the iframe such as interaction with logged in user.
Method 2: Create the iframe from within drupal.
Create a new module that defines a menu entry using hoom_menu (the url for the iframe). Define the content of the iframe from the callback function of the menu entry. Then Somehow assign a custom page.tpl.php theme for the desired iframe url so that only the content of the iframe is rendered without all the other page elements (blocks, menus, footer, etc.).
Any comments especially for method 2 will be greatly appreciated! :)
I have done exactly this, just this week!
I created a custom module that outputs only the content that I want (using hook_menu()). Then I created a page template (page-mycustommodule.tpl.php) that only has
<?php print $content; ?>
within the <body> tags.
That was basically all. To find out the name that your page template needs to have, use the devel and theme_devel modules, then just click on your page and it will tell you which templates it looked for.
One thing to look out for: any links in the iframe will only change the contents OF THAT FRAME, so when your module creates links, use the proper target to make the parent page jump to the new URL:
l('link text',
'node/' . $mynode->nid,
array('attributes' => array('target' => '_parent')));
I found the answer by Graham very useful. Unfortunately I don't have enough reputation on this site to add a comment so I will have to put my comment in an answer.
5 years on, the information has changed slightly:
The module theme_devel now seems to be called devel_themer instead.
In D7, the template naming pattern is slightly different with 2
hyphens: page--[front|internal/path].tpl.php (see docs)
D7 templates are slightly different based on render arrays, so the template will need to be something like print render($page['content']);
method 3
you can use this module https://www.drupal.org/project/entity_iframe that allows you to create IFRAME READY webpages
install it and go to the display settings of you content type that you want to use as iframe content admin/structure/types/manage/CONTENTTYPE/display
choose the IFRAME view mode and choose the fields you would like to be shown
and then use url like this domain.com/entity_iframe/node/NID and you will have a display with no extra headers footers etc ...
By default a sample EMBED/IFRAME code is provided to the admin under each node the settings
<iframe width="100%" height="300" src="domain.com/entity_iframe/node/96" frameborder="0" class="entity_iframe entity_iframe_node" id="entity_iframe_node_96" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
The settings in admin/config/system/entity_iframe control some of the details of the embed code
For complete control of the theme used you can use in combination with https://www.drupal.org/project/entity_iframe_theme
What do you exactly need to iframe?
A node? A block? Should it be static or dynamic?
You can simply create a node with a php filter and generate the iframe output.
Then you can put this output between <pre> tags to display it as code that users can copy/paste in their site.

Resources