Load a page through ajax with Wordpress using different template - wordpress

After the user clicks a link, I want to load the corresponding page within the currentpage. This ajaxpage should be themed differently than it's non-ajax page.
The pages would be added by the client in different languages. So there is no fixed id's and the client shouldnt add the same article twice (1 for normal and 1 for ajax). This rules manually overwriting templates out.
The first part, ajax loading of content is easy to do, but I don't know how to display content in different ways.
So
domain.com/product/car direct visit shows a product (car) using single-product.php as template which includes a header and footer.
ajax loading domain.com/product/car would show a product (car) but without a header and footer and perhaps some small layout changes.
My initial thought would be setting some template dynamicly (template-product-ajax.php) but Im not sure if this is possible or how.
In short, how can I create a different template/layout/view when loading a page with ajax in wordpress

You would have to pass in a series of variables, including the page/post ID you want to load, what kind of object you're trying to load (either 'page' or 'post') and a boolean to ensure that the standard get_header() function isn't loaded (as you only want the content of the page, not the headers).
Somewhere within the template, you can use that boolean to determine the template's behavior. For example:
<?php
/*Template Name: AJAX Content*/
if(!isset($_POST['bool']))
get_header();
?>
<div id="body">
<div class="page_content">
<?php
if(isset($_POST['bool']))
{
$object = $_POST['ob_type'] == 'page' ? get_page($_POST['PID']) : get_post($_POST['PID']);
echo $object->post_content;
/*echo '<pre>';var_dump($object);echo '</pre>';//Uncomment this section if you want to know what you can access with variable $object*/
{
else
{
//STANDARD LOOP
}
?>
</div>
</div>
This is untested, but it should help get you started if you're already familiar with AJAX calls and sending POST data to a separate page.

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; ?>

Woocommerce move notices on account login page

Woocommerce outpuits a number of error/info notices on multiple pages. Normally this is handles by <? wc_print_notices() ?>, and so is nice and easy to reposition within a custom template. However, on the woocommerce account login page (form-login.php), there is no reference to <? wc_print_notices() ?> and therefore I cannot reposition. How are they automatically being output, and is there a way to overide them?
error, success and notice html file exist in templates/notices and include by some function, filter and action. for example woocommerce_before_customer_login_form action for login form.

Drupal - Template for Custom Page

I've got a custom page I created programmatically using hook_menu. I can have it return content without a problem. However, I want it to appear within the content region of my page.tpl.php template so that blocks can be incorporated in the page. Same way that Views allows you to give a view a custom url while still appearing in the page.tpl.php template.
How do I do this for my page? By default, I just get a with only my content.
Thanks,
Howie
In your page callback function, if you just print the content of your page without returning anything, it won't be included within your page.tpl.php. If you return a string containing the contents of your page, it will go into the content region, i.e.
function mymodule_page_callback () {
$content = "Hello, world inside the content region";
return $content;
}

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

WordPress - how to insert html code selectively in posts

I want to display custom html code when a post is rendered (so not when is inserted into the database).
I currently do this with add_filter('the_content', 'my_custom_method'). The only problem is that I want this do be displayed only inside the post (when is viewed in its own page), not when all posts are rendered .
I banged my head against the wall, but couldn't find any method to tell me if i'm currently inside an individual post or not (this has to work for every url rewriting possible, so i can't rely on url)
Is there such a method? I believe it should be, but i can't find it. Thanks.
the function for checking if the post is in its own page is is_single()
add_filter('the_content', 'my_custom_method');
function my_custom_method(){
if(is_single()){
//code for your custom html code
}
}
the function is_single() checks if the page being rendered is a single page or not.
The easiest way to do this would be to modify your templates. Wordpress template sets should have a file named single.php (inside wp-content/themes/<theme name>). This is the page that gets rendered when you are viewing the page for a single post.
You could edit this file and insert whatever you needed to for the posts there.

Resources